Skip to content

Commit b91767f

Browse files
authored
Merge pull request #2563 from SpareBank1/rm-ontext-offtext
feat(ffe-form-react): legg til role=switch på toggleswitch for bedre skjermleser-støtte
2 parents 5a72f08 + 478e664 commit b91767f

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

packages/ffe-form-react/src/ToggleSwitch.spec.tsx

+18-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const renderToggleSwitch = (props?: Partial<ToggleSwitchProps>) =>
1010
);
1111

1212
describe('<ToggleSwitch />', () => {
13-
it('should render a checkbox', () => {
13+
it('should render a switch', () => {
1414
renderToggleSwitch();
15-
expect(screen.getByRole('checkbox')).toBeInTheDocument();
15+
expect(screen.getByRole('switch')).toBeInTheDocument();
1616
});
1717

1818
it('should render a default value if passed', () => {
@@ -21,32 +21,34 @@ describe('<ToggleSwitch />', () => {
2121
Hello world
2222
</ToggleSwitch>,
2323
);
24-
const checkbox = screen.getByRole('checkbox');
25-
expect(checkbox).not.toBeChecked();
24+
const switchElement = screen.getByRole('switch');
25+
expect(switchElement).not.toBeChecked();
2626
rerender(
2727
<ToggleSwitch onChange={() => {}} checked={true}>
2828
Hello world
2929
</ToggleSwitch>,
3030
);
31-
expect(checkbox).toBeChecked();
31+
expect(switchElement).toBeChecked();
3232
});
3333

3434
it('should apply the same id to <label> and <input>', () => {
3535
const { container } = renderToggleSwitch({
3636
name: 'Some text goes here',
3737
});
3838
const label = container.querySelector('label');
39-
const checkbox = screen.getByRole('checkbox');
39+
const switchElement = screen.getByRole('switch');
4040

41-
expect(label?.getAttribute('for')).toBe(checkbox?.getAttribute('id'));
41+
expect(label?.getAttribute('for')).toBe(
42+
switchElement?.getAttribute('id'),
43+
);
4244
});
4345

4446
it('should apply a unique id for each instance', () => {
4547
renderToggleSwitch({ name: 'Some text goes here' });
4648
renderToggleSwitch({ name: 'Some other text goes here' });
4749

4850
const [id1, id2] = screen
49-
.getAllByRole('checkbox')
51+
.getAllByRole('switch')
5052
.map(it => it.getAttribute('id'));
5153

5254
expect(id1).not.toBe(id2);
@@ -58,15 +60,15 @@ describe('<ToggleSwitch />', () => {
5860
Hello world
5961
</ToggleSwitch>,
6062
);
61-
const checkbox = screen.getByRole('checkbox');
62-
const idFirstRender = checkbox.getAttribute('id');
63+
const switchElement = screen.getByRole('switch');
64+
const idFirstRender = switchElement.getAttribute('id');
6365

6466
rerender(
6567
<ToggleSwitch onChange={() => {}} checked={undefined}>
6668
Hello world
6769
</ToggleSwitch>,
6870
);
69-
const idSecondRender = checkbox.getAttribute('id');
71+
const idSecondRender = switchElement.getAttribute('id');
7072

7173
expect(idFirstRender).toBe(idSecondRender);
7274
});
@@ -79,10 +81,10 @@ describe('<ToggleSwitch />', () => {
7981
});
8082

8183
const label = container.querySelector('label');
82-
const checkbox = screen.getByRole('checkbox');
84+
const switchElement = screen.getByRole('switch');
8385

8486
expect(label?.getAttribute('for')).toBe(id);
85-
expect(checkbox.getAttribute('id')).toBe(id);
87+
expect(switchElement.getAttribute('id')).toBe(id);
8688
});
8789

8890
it('should set arbitrary props (rest) on input', () => {
@@ -91,9 +93,9 @@ describe('<ToggleSwitch />', () => {
9193
tabIndex: -1,
9294
});
9395

94-
const checkbox = screen.getByRole('checkbox');
95-
expect(checkbox.getAttribute('name')).toBe('toggle');
96-
expect(checkbox.getAttribute('tabIndex')).toBe('-1');
96+
const switchElement = screen.getByRole('switch');
97+
expect(switchElement.getAttribute('name')).toBe('toggle');
98+
expect(switchElement.getAttribute('tabIndex')).toBe('-1');
9799
});
98100

99101
it('should render additional props if passed', () => {

packages/ffe-form-react/src/ToggleSwitch.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const ToggleSwitch: React.FC<ToggleSwitchProps> = ({
4848
<input
4949
className="ffe-toggle-switch__input"
5050
type="checkbox"
51+
role="switch"
5152
id={id}
5253
value={value}
5354
checked={!!checked}

0 commit comments

Comments
 (0)