Skip to content

Commit bc664cc

Browse files
committed
Increase tests coverage
Signed-off-by: guido <[email protected]>
1 parent 90fa41e commit bc664cc

File tree

3 files changed

+136
-10
lines changed

3 files changed

+136
-10
lines changed

sources/js/src/components/entities-toggle-control.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React, { JSX } from 'react';
55
import { NoOptionsMessage } from './no-options-message';
66

77
export function EntitiesToggleControl(
8-
props: EntitiesSearch.EntitiesControl<number>
8+
props: EntitiesSearch.EntitiesControl<string>
99
): JSX.Element {
1010
const className = classnames(
1111
props.className,
@@ -20,18 +20,12 @@ export function EntitiesToggleControl(
2020
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
2121
const { target } = event;
2222

23-
if (!(target instanceof HTMLInputElement)) {
23+
if (target.checked && !props.value?.has(target.value)) {
24+
props.onChange(props.value?.add(target.value));
2425
return;
2526
}
2627

27-
const value = Number(target.value);
28-
29-
if (target.checked && !props.value?.has(value)) {
30-
props.onChange(props.value?.add(value));
31-
return;
32-
}
33-
34-
props.onChange(props.value?.delete(value));
28+
props.onChange(props.value?.delete(target.value));
3529
};
3630

3731
return (
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`EntitiesToggleControl renders correctly 1`] = `
4+
<div
5+
class="test-class wz-toggle-control wz-toggle-control--entities"
6+
>
7+
<div
8+
class="wz-toggle-control-item"
9+
>
10+
<label
11+
for="wz-toggle-control-item__input-1"
12+
>
13+
<input
14+
id="wz-toggle-control-item__input-1"
15+
type="checkbox"
16+
value="1"
17+
/>
18+
Option 1
19+
</label>
20+
</div>
21+
<div
22+
class="wz-toggle-control-item"
23+
>
24+
<label
25+
for="wz-toggle-control-item__input-2"
26+
>
27+
<input
28+
id="wz-toggle-control-item__input-2"
29+
type="checkbox"
30+
value="2"
31+
/>
32+
Option 2
33+
</label>
34+
</div>
35+
<div
36+
class="wz-toggle-control-item"
37+
>
38+
<label
39+
for="wz-toggle-control-item__input-3"
40+
>
41+
<input
42+
id="wz-toggle-control-item__input-3"
43+
type="checkbox"
44+
value="3"
45+
/>
46+
Option 3
47+
</label>
48+
</div>
49+
</div>
50+
`;
51+
52+
exports[`EntitiesToggleControl renders the NoOptionsMessage when there are no options 1`] = `
53+
<p
54+
class="wz-entities-search-no-option-message"
55+
>
56+
No options
57+
</p>
58+
`;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { OrderedSet } from 'immutable';
2+
import React from 'react';
3+
4+
import { describe, expect, it, jest } from '@jest/globals';
5+
6+
import { render, screen, fireEvent } from '@testing-library/react';
7+
8+
import { EntitiesToggleControl } from '../../../../sources/js/src/components/entities-toggle-control';
9+
10+
const options = OrderedSet([
11+
{ label: 'Option 1', value: '1' },
12+
{ label: 'Option 2', value: '2' },
13+
{ label: 'Option 3', value: '3' },
14+
]);
15+
16+
describe('EntitiesToggleControl', () => {
17+
it('renders correctly', () => {
18+
const { container } = render(
19+
<EntitiesToggleControl
20+
className="test-class"
21+
options={options}
22+
onChange={() => {}}
23+
/>
24+
);
25+
26+
expect(container.firstChild).toMatchSnapshot();
27+
});
28+
29+
it('renders the NoOptionsMessage when there are no options', () => {
30+
const { container } = render(
31+
<EntitiesToggleControl
32+
className="test-class"
33+
options={OrderedSet([])}
34+
onChange={() => {}}
35+
/>
36+
);
37+
38+
expect(container.firstChild).toMatchSnapshot();
39+
});
40+
41+
it('updates the value when one or more option are selected', () => {
42+
const onChange = jest.fn();
43+
44+
render(
45+
<EntitiesToggleControl
46+
className="test-class"
47+
options={options}
48+
value={OrderedSet()}
49+
onChange={onChange}
50+
/>
51+
);
52+
53+
fireEvent.click(screen.getByLabelText('Option 1'));
54+
55+
expect(onChange).toHaveBeenCalledWith(OrderedSet(['1']));
56+
});
57+
58+
it('updates the value when one or more option are unselected', () => {
59+
const onChange = jest.fn();
60+
61+
render(
62+
<EntitiesToggleControl
63+
className="test-class"
64+
options={options}
65+
value={OrderedSet(['1', '2'])}
66+
onChange={onChange}
67+
/>
68+
);
69+
70+
fireEvent.click(screen.getByLabelText('Option 1'));
71+
72+
expect(onChange).toHaveBeenCalledWith(OrderedSet(['2']));
73+
});
74+
});

0 commit comments

Comments
 (0)