Skip to content

Commit

Permalink
test(sbb-radio-button): enhance ssr test
Browse files Browse the repository at this point in the history
  • Loading branch information
TomMenga committed Oct 23, 2024
1 parent 5331b3d commit 56d5ba4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/elements/core/mixins/form-associated-radio-button-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const SbbFormAssociatedRadioButtonMixin = <T extends Constructor<LitEleme
public accessor checked: boolean = false;

protected abort = new SbbConnectedAbortController(this);
private _didLoad: boolean = false;

protected constructor() {
super();
Expand Down Expand Up @@ -171,6 +172,12 @@ export const SbbFormAssociatedRadioButtonMixin = <T extends Constructor<LitEleme
}
}

protected override firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);
this._didLoad = true;
this.updateFocusableRadios();
}

/**
* Called on `value` change
* If I'm checked, update the value. Otherwise, do nothing.
Expand All @@ -195,6 +202,9 @@ export const SbbFormAssociatedRadioButtonMixin = <T extends Constructor<LitEleme
* - the first non-disabled radio in DOM order;
*/
protected updateFocusableRadios(): void {
if (!this._didLoad) {
return;
}
const radios = this._orderedGrouperRadios();
const checkedIndex = radios.findIndex((r) => r.checked && !r.disabled && !r.formDisabled);
const focusableIndex =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,37 @@ import { ssrHydratedFixture } from '../../core/testing/private.js';
import { SbbRadioButtonPanelElement } from './radio-button-panel.js';

describe(`sbb-radio-button-panel ssr`, () => {
let root: SbbRadioButtonPanelElement;

beforeEach(async () => {
root = await ssrHydratedFixture(
it('renders', async () => {
const root = await ssrHydratedFixture(
html`<sbb-radio-button-panel value="Value">Value label</sbb-radio-button-panel>`,
{
modules: ['./radio-button-panel.js'],
},
);
assert.instanceOf(root, SbbRadioButtonPanelElement);
});

it('renders', () => {
it('renders checked', async () => {
const root = await ssrHydratedFixture(
html`<sbb-radio-button-panel value="Value" checked>Value label</sbb-radio-button-panel>`,
{
modules: ['./radio-button-panel.js'],
},
);
assert.instanceOf(root, SbbRadioButtonPanelElement);
});

it('renders standalone group', async () => {
const root = await ssrHydratedFixture(
html`
<sbb-radio-button-panel name="group" value="value 1">Value 1</sbb-radio-button-panel>
<sbb-radio-button-panel name="group" value="value 2">Value 2</sbb-radio-button-panel>
<sbb-radio-button-panel name="group" value="value 3">Value 3</sbb-radio-button-panel>
`,
{
modules: ['./radio-button-panel.js'],
},
);
assert.instanceOf(root, SbbRadioButtonPanelElement);
});
});
29 changes: 24 additions & 5 deletions src/elements/radio-button/radio-button/radio-button.ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,37 @@ import { ssrHydratedFixture } from '../../core/testing/private.js';
import { SbbRadioButtonElement } from './radio-button.js';

describe(`sbb-radio-button ssr`, () => {
let root: SbbRadioButtonElement;

beforeEach(async () => {
root = await ssrHydratedFixture(
it('renders', async () => {
const root = await ssrHydratedFixture(
html`<sbb-radio-button value="Value">Value label</sbb-radio-button>`,
{
modules: ['./radio-button.js'],
},
);
assert.instanceOf(root, SbbRadioButtonElement);
});

it('renders', () => {
it('renders checked', async () => {
const root = await ssrHydratedFixture(
html`<sbb-radio-button value="Value" checked>Value label</sbb-radio-button>`,
{
modules: ['./radio-button.js'],
},
);
assert.instanceOf(root, SbbRadioButtonElement);
});

it('renders standalone group', async () => {
const root = await ssrHydratedFixture(
html`
<sbb-radio-button name="group" value="value 1">Value 1</sbb-radio-button>
<sbb-radio-button name="group" value="value 2">Value 2</sbb-radio-button>
<sbb-radio-button name="group" value="value 3">Value 3</sbb-radio-button>
`,
{
modules: ['./radio-button.js'],
},
);
assert.instanceOf(root, SbbRadioButtonElement);
});
});

0 comments on commit 56d5ba4

Please sign in to comment.