Skip to content

Commit dd3a541

Browse files
committed
Add more tests
Signed-off-by: guido <[email protected]>
1 parent bc664cc commit dd3a541

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'jsdom',
55
moduleDirectories: ['node_modules'],
6+
setupFilesAfterEnv: ['<rootDir>/tests/js/setup-tests.ts'],
67
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@faker-js/faker": "^8",
1313
"@jest/globals": "^29.4.3",
1414
"@testing-library/dom": "^9.3.1",
15+
"@testing-library/jest-dom": "^6.2.0",
1516
"@testing-library/react": "^14.0.0",
1617
"@testing-library/react-hooks": "^8.0.1",
1718
"@testing-library/user-event": "^14.5.1",

tests/js/setup-tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom/jest-globals';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`KindRadioControl renders the NoOptionsMessage when there are no options 1`] = `
4+
<div
5+
class="test-class wz-radio-control wz-radio-control--kind"
6+
/>
7+
`;
8+
9+
exports[`KindRadioControl renders the component 1`] = `
10+
<input
11+
checked=""
12+
id="wz-radio-control-item__input-option-one"
13+
type="radio"
14+
value="option-one"
15+
/>
16+
`;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Write a test for the KindRadioControl component here
2+
// - the test must ensure the correctness of the component's rendering
3+
// - the test expects the value change accordingly to the selection
4+
// - the test expects no rendering when the options are empty
5+
import { OrderedSet } from 'immutable';
6+
import React from 'react';
7+
8+
import { expect, jest, describe, it } from '@jest/globals';
9+
10+
import { render, screen, fireEvent } from '@testing-library/react';
11+
12+
import { KindRadioControl } from '../../../../sources/js/src/components/kind-radio-control';
13+
14+
describe('KindRadioControl', () => {
15+
it('renders the component', () => {
16+
const props = {
17+
className: 'test-class',
18+
options: OrderedSet([
19+
{
20+
label: 'Option 1',
21+
value: 'option-one',
22+
},
23+
]),
24+
value: 'option-one',
25+
onChange: jest.fn(),
26+
};
27+
render(<KindRadioControl {...props} />);
28+
29+
expect(screen.getByLabelText('Option 1')).toMatchSnapshot();
30+
});
31+
32+
it('renders the NoOptionsMessage when there are no options', () => {
33+
const props = {
34+
className: 'test-class',
35+
options: OrderedSet([]),
36+
value: 'option-one',
37+
onChange: jest.fn(),
38+
};
39+
const { container } = render(<KindRadioControl {...props} />);
40+
41+
expect(container.firstChild).toMatchSnapshot();
42+
});
43+
44+
it('check the input based on the value given', () => {
45+
const props = {
46+
className: 'test-class',
47+
options: OrderedSet([
48+
{
49+
label: 'Option 1',
50+
value: 'option-one',
51+
},
52+
{
53+
label: 'Option 2',
54+
value: 'option-2',
55+
},
56+
]),
57+
value: 'option-one',
58+
onChange: jest.fn(),
59+
};
60+
render(<KindRadioControl {...props} />);
61+
62+
expect(screen.getByLabelText('Option 1')).toBeChecked();
63+
});
64+
65+
it('changes the value when an option is selected', () => {
66+
const props = {
67+
options: OrderedSet([
68+
{
69+
label: 'Option 1',
70+
value: 'option-one',
71+
},
72+
{
73+
label: 'Option 2',
74+
value: 'option-2',
75+
},
76+
]),
77+
value: 'option-one',
78+
onChange: jest.fn(),
79+
};
80+
render(<KindRadioControl {...props} />);
81+
82+
fireEvent.click(screen.getByLabelText('Option 2'));
83+
84+
expect(props.onChange).toHaveBeenCalledWith('option-2');
85+
});
86+
});

yarn.lock

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf"
88
integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==
99

10+
"@adobe/css-tools@^4.3.2":
11+
version "4.3.2"
12+
resolved "https://registry.yarnpkg.com/@adobe/css-tools/-/css-tools-4.3.2.tgz#a6abc715fb6884851fca9dad37fc34739a04fd11"
13+
integrity sha512-DA5a1C0gD/pLOvhv33YMrbf2FK3oUzwNl9oOJqE4XVjuEtt6XIakRcsd7eLiOSPkp1kTRQGICTA8cKra/vFbjw==
14+
1015
"@ampproject/remapping@^2.2.0":
1116
version "2.2.1"
1217
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.1.tgz#99e8e11851128b8702cd57c33684f1d0f260b630"
@@ -2328,6 +2333,20 @@
23282333
lz-string "^1.5.0"
23292334
pretty-format "^27.0.2"
23302335

2336+
"@testing-library/jest-dom@^6.2.0":
2337+
version "6.2.0"
2338+
resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-6.2.0.tgz#b572bd5cd6b29314487bac7ba393188e4987b4f7"
2339+
integrity sha512-+BVQlJ9cmEn5RDMUS8c2+TU6giLvzaHZ8sU/x0Jj7fk+6/46wPdwlgOPcpxS17CjcanBi/3VmGMqVr2rmbUmNw==
2340+
dependencies:
2341+
"@adobe/css-tools" "^4.3.2"
2342+
"@babel/runtime" "^7.9.2"
2343+
aria-query "^5.0.0"
2344+
chalk "^3.0.0"
2345+
css.escape "^1.5.1"
2346+
dom-accessibility-api "^0.6.3"
2347+
lodash "^4.17.15"
2348+
redent "^3.0.0"
2349+
23312350
"@testing-library/react-hooks@^8.0.1":
23322351
version "8.0.1"
23332352
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
@@ -4102,7 +4121,7 @@ [email protected]:
41024121
dependencies:
41034122
deep-equal "^2.0.5"
41044123

4105-
aria-query@^5.1.3:
4124+
aria-query@^5.0.0, aria-query@^5.1.3:
41064125
version "5.3.0"
41074126
resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-5.3.0.tgz#650c569e41ad90b51b3d7df5e5eed1c7549c103e"
41084127
integrity sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A==
@@ -5158,6 +5177,11 @@ css-what@^6.1.0:
51585177
resolved "https://registry.yarnpkg.com/css-what/-/css-what-6.1.0.tgz#fb5effcf76f1ddea2c81bdfaa4de44e79bac70f4"
51595178
integrity sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==
51605179

5180+
css.escape@^1.5.1:
5181+
version "1.5.1"
5182+
resolved "https://registry.yarnpkg.com/css.escape/-/css.escape-1.5.1.tgz#42e27d4fa04ae32f931a4b4d4191fa9cddee97cb"
5183+
integrity sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg==
5184+
51615185
cssesc@^3.0.0:
51625186
version "3.0.0"
51635187
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee"
@@ -5507,6 +5531,11 @@ dom-accessibility-api@^0.5.9:
55075531
resolved "https://registry.yarnpkg.com/dom-accessibility-api/-/dom-accessibility-api-0.5.16.tgz#5a7429e6066eb3664d911e33fb0e45de8eb08453"
55085532
integrity sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg==
55095533

5534+
dom-accessibility-api@^0.6.3:
5535+
version "0.6.3"
5536+
resolved "https://registry.yarnpkg.com/dom-accessibility-api/-/dom-accessibility-api-0.6.3.tgz#993e925cc1d73f2c662e7d75dd5a5445259a8fd8"
5537+
integrity sha512-7ZgogeTnjuHbo+ct10G9Ffp0mif17idi0IyWNVA/wcwcm7NPOD/WEHVP3n7n3MhXqxoIYm8d6MuZohYWIZ4T3w==
5538+
55105539
dom-scroll-into-view@^1.2.1:
55115540
version "1.2.1"
55125541
resolved "https://registry.yarnpkg.com/dom-scroll-into-view/-/dom-scroll-into-view-1.2.1.tgz#e8f36732dd089b0201a88d7815dc3f88e6d66c7e"
@@ -8220,7 +8249,7 @@ lodash.uniq@^4.5.0:
82208249
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
82218250
integrity sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==
82228251

8223-
lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21:
8252+
lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21:
82248253
version "4.17.21"
82258254
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
82268255
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)