Skip to content

Commit c97c3cc

Browse files
committed
fixup! fixup! fixup! fixup! Transform Action jest tests into playwright (#592)
1 parent deeddc1 commit c97c3cc

File tree

74 files changed

+176
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+176
-103
lines changed

src/components/Button/__tests__/Button.spec.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ test.describe('Button', () => {
6666

6767
test.describe('non-visual', () => {
6868
test('id', async ({ mount }) => {
69+
const testId = 'testId';
70+
const testLabel = 'testLabel';
71+
6972
const component = await mount(
7073
<ButtonForTest
71-
id="test-id"
74+
id={testId}
75+
label={testLabel}
7276
/>,
7377
);
7478

75-
await expect(component).toHaveAttribute('id', 'test-id');
79+
await expect(component).toHaveAttribute('id', testId);
80+
await expect(component.getByText(testLabel)).toHaveAttribute('id', `${testId}__labelText`);
7681
});
7782

7883
test('ref', async ({ mount }) => {

src/components/Radio/__tests__/Radio.spec.tsx

+140-100
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
propTests,
99
} from '../../../../tests/playwright';
1010
import { partialDisabledOptionsPropTest } from './_propTests/partialDisabledOptionsPropTest';
11-
import { RadioForTest } from './Radio.story';
11+
import {
12+
RadioForTest,
13+
RadioForFormLayoutTests,
14+
} from './Radio.story';
15+
import type { RadioForFormLayoutTestsProps } from './Radio.story';
1216

1317
const options = [
1418
{
@@ -30,119 +34,155 @@ const options = [
3034
},
3135
];
3236

33-
test.describe('CheckboxField', () => {
34-
test.describe('visual', () => {
35-
[
36-
...propTests.defaultComponentPropTest,
37-
...propTests.requiredPropTest,
38-
...propTests.labelPropTest,
39-
...propTests.isLabelVisiblePropTest,
40-
...propTests.renderAsRequiredPropTest,
41-
...propTests.layoutPropTest,
42-
...mixPropTests([
43-
propTests.validationStatePropTest,
44-
propTests.validationTextPropTest,
45-
propTests.disabledPropTest,
46-
]),
47-
...mixPropTests([
48-
propTests.validationStatePropTest,
49-
propTests.helpTextPropTest,
50-
]),
51-
...mixPropTests([
52-
partialDisabledOptionsPropTest,
53-
propTests.validationStatePropTest,
54-
]),
55-
].forEach(({
56-
name,
57-
onBeforeTest,
58-
onBeforeSnapshot,
59-
props,
60-
}) => {
61-
test(name, async ({
62-
mount,
63-
page,
37+
test.describe('Radio', () => {
38+
test.describe('base', () => {
39+
test.describe('visual', () => {
40+
[
41+
...propTests.defaultComponentPropTest,
42+
...propTests.requiredPropTest,
43+
...propTests.labelPropTest,
44+
...propTests.isLabelVisiblePropTest,
45+
...propTests.renderAsRequiredPropTest,
46+
...propTests.layoutPropTest,
47+
...mixPropTests([
48+
propTests.validationStatePropTest,
49+
propTests.validationTextPropTest,
50+
propTests.disabledPropTest,
51+
]),
52+
...mixPropTests([
53+
propTests.validationStatePropTest,
54+
propTests.helpTextPropTest,
55+
]),
56+
...mixPropTests([
57+
partialDisabledOptionsPropTest,
58+
propTests.validationStatePropTest,
59+
]),
60+
].forEach(({
61+
name,
62+
onBeforeTest,
63+
onBeforeSnapshot,
64+
props,
6465
}) => {
65-
if (onBeforeTest) {
66-
await onBeforeTest(page);
67-
}
66+
test(name, async ({
67+
mount,
68+
page,
69+
}) => {
70+
if (onBeforeTest) {
71+
await onBeforeTest(page);
72+
}
73+
74+
const component = await mount(
75+
<RadioForTest
76+
{...props}
77+
/>,
78+
);
79+
80+
if (onBeforeSnapshot) {
81+
await onBeforeSnapshot(page, component);
82+
}
83+
84+
const screenshot = await component.screenshot();
85+
expect(screenshot).toMatchSnapshot();
86+
});
87+
});
88+
});
89+
90+
test.describe('non-visual', () => {
91+
test('id', async ({ mount }) => {
92+
const radioId = 'radioId';
93+
const label = 'radioLabel';
6894

6995
const component = await mount(
7096
<RadioForTest
71-
{...props}
97+
id={radioId}
98+
label={label}
99+
options={options}
72100
/>,
73101
);
74102

75-
if (onBeforeSnapshot) {
76-
await onBeforeSnapshot(page, component);
77-
}
78-
79-
const screenshot = await component.screenshot();
80-
expect(screenshot).toMatchSnapshot();
103+
await expect(component).toHaveAttribute('id', radioId);
104+
await expect(component.getByText(label).first()).toHaveAttribute('id', `${radioId}__label`);
105+
await expect(component.getByText(label).last()).toHaveAttribute('id', `${radioId}__displayLabel`);
106+
await expect(component.getByText(options[0].label)).toHaveAttribute('id', `${radioId}__item__${options[0].key}__labelText`);
107+
await expect(component.getByText(options[1].label)).toHaveAttribute('id', `${radioId}__item__${options[1].key}__labelText`);
108+
await expect(component.getByText(options[2].label)).toHaveAttribute('id', `${radioId}__item__${options[2].value}__labelText`);
109+
await expect(component.locator(`input[id=${radioId}__item__${options[0].key}]`)).not.toBeEmpty();
110+
await expect(component.locator(`input[id=${radioId}__item__${options[1].key}]`)).not.toBeEmpty();
111+
await expect(component.locator(`input[id=${radioId}__item__${options[2].value}]`)).not.toBeEmpty();
112+
await expect(component.locator(`label[id=${radioId}__item__${options[0].key}__label]`)).not.toBeEmpty();
113+
await expect(component.locator(`label[id=${radioId}__item__${options[1].key}__label]`)).not.toBeEmpty();
114+
await expect(component.locator(`label[id=${radioId}__item__${options[2].value}__label]`)).not.toBeEmpty();
81115
});
82116
});
83-
});
84117

85-
test.describe('non-visual', () => {
86-
test('id', async ({ mount }) => {
87-
const radioId = 'radioId';
88-
const label = 'radioLabel';
89-
90-
const component = await mount(
91-
<RadioForTest
92-
id={radioId}
93-
label={label}
94-
options={options}
95-
/>,
96-
);
97-
98-
await expect(component).toHaveAttribute('id', radioId);
99-
await expect(component.getByText(label).first()).toHaveAttribute('id', `${radioId}__label`);
100-
await expect(component.getByText(label).last()).toHaveAttribute('id', `${radioId}__displayLabel`);
101-
await expect(component.getByText(options[0].label)).toHaveAttribute('id', `${radioId}__item__${options[0].key}__labelText`);
102-
await expect(component.getByText(options[1].label)).toHaveAttribute('id', `${radioId}__item__${options[1].key}__labelText`);
103-
await expect(component.getByText(options[2].label)).toHaveAttribute('id', `${radioId}__item__${options[2].value}__labelText`);
104-
await expect(component.locator(`input[id=${radioId}__item__${options[0].key}]`)).not.toBeEmpty();
105-
await expect(component.locator(`input[id=${radioId}__item__${options[1].key}]`)).not.toBeEmpty();
106-
await expect(component.locator(`input[id=${radioId}__item__${options[2].value}]`)).not.toBeEmpty();
107-
await expect(component.locator(`label[id=${radioId}__item__${options[0].key}__label]`)).not.toBeEmpty();
108-
await expect(component.locator(`label[id=${radioId}__item__${options[1].key}__label]`)).not.toBeEmpty();
109-
await expect(component.locator(`label[id=${radioId}__item__${options[2].value}__label]`)).not.toBeEmpty();
118+
test.describe('functionality', () => {
119+
test('calls synthetic event onChange()', async ({ mount }) => {
120+
let changeCalled = false;
121+
122+
const component = await mount(
123+
<RadioForTest
124+
onChange={() => {
125+
changeCalled = true;
126+
}}
127+
options={options}
128+
/>,
129+
);
130+
131+
await component.getByText(options[1].label).click({ force: true });
132+
expect(changeCalled).toBeTruthy();
133+
});
134+
test('check on space press when focused', async ({ mount }) => {
135+
let changeCalled = false;
136+
const testId = 'testId';
137+
138+
const component = await mount(
139+
<RadioForTest
140+
id={testId}
141+
onChange={() => { changeCalled = true; }}
142+
options={options}
143+
/>,
144+
);
145+
146+
const input = component.locator(`input[id=${testId}__item__${options[1].key}]`);
147+
await input.focus();
148+
await input.press('Space');
149+
expect(changeCalled).toBeTruthy();
150+
});
110151
});
111152
});
112153

113-
test.describe('functionality', () => {
114-
test('calls synthetic event onChange()', async ({ mount }) => {
115-
let changeCalled = false;
116-
117-
const component = await mount(
118-
<RadioForTest
119-
onChange={() => {
120-
changeCalled = true;
121-
}}
122-
options={options}
123-
/>,
124-
);
125-
126-
await component.getByText(options[1].label).click({ force: true });
127-
expect(changeCalled).toBeTruthy();
128-
});
154+
test.describe('formLayout', () => {
155+
test.describe('visual', () => {
156+
[
157+
...propTests.layoutPropTest,
158+
].forEach(({
159+
name,
160+
onBeforeTest,
161+
onBeforeSnapshot,
162+
props,
163+
}) => {
164+
test(name, async ({
165+
mount,
166+
page,
167+
}) => {
168+
if (onBeforeTest) {
169+
await onBeforeTest(page);
170+
}
129171

130-
test('check on space press when focused', async ({ mount }) => {
131-
let changeCalled = false;
132-
const testId = 'testId';
133-
134-
const component = await mount(
135-
<RadioForTest
136-
id={testId}
137-
onChange={() => { changeCalled = true; }}
138-
options={options}
139-
/>,
140-
);
141-
142-
const input = component.locator(`input[id=${testId}__item__${options[1].key}]`);
143-
await input.focus();
144-
await input.press('Space');
145-
expect(changeCalled).toBeTruthy();
172+
const component = await mount(
173+
<RadioForFormLayoutTests
174+
{...props as unknown as RadioForFormLayoutTestsProps}
175+
/>,
176+
);
177+
178+
if (onBeforeSnapshot) {
179+
await onBeforeSnapshot(page, component);
180+
}
181+
182+
const screenshot = await component.screenshot();
183+
expect(screenshot).toMatchSnapshot();
184+
});
185+
});
146186
});
147187
});
148188
});

src/components/Radio/__tests__/Radio.story.tsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import type { InputHTMLAttributes } from 'react';
33
import { Radio } from '..';
4+
import { FormLayoutContext } from '../../FormLayout';
45

56
// Types for story component will be improved when we have full TypeScript support
67
type RadioForTestProps = InputHTMLAttributes<HTMLInputElement>;
8+
export type RadioForFormLayoutTestsProps = RadioForTestProps & {
9+
layout: 'vertical' | 'horizontal'
10+
};
711

812
const defaultLabel = 'test-group-label';
913
const defaultOptions = [
@@ -33,3 +37,27 @@ export const RadioForTest = ({
3337

3438
/>
3539
);
40+
41+
export const RadioForFormLayoutTests = ({
42+
layout,
43+
...props
44+
} : RadioForFormLayoutTestsProps) => {
45+
const values = useMemo(() => ({ layout }), [layout]);
46+
47+
return (
48+
<FormLayoutContext.Provider
49+
value={values}
50+
>
51+
<Radio
52+
label={defaultLabel}
53+
options={defaultOptions}
54+
{...props}
55+
/>
56+
<Radio
57+
label="another-test-label"
58+
options={defaultOptions}
59+
{...props}
60+
/>
61+
</FormLayoutContext.Provider>
62+
);
63+
};

0 commit comments

Comments
 (0)