Skip to content

Commit 37e8931

Browse files
committed
Transform Action jest tests into playwright (#592)
1 parent 185768c commit 37e8931

File tree

514 files changed

+2101
-678
lines changed

Some content is hidden

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

514 files changed

+2101
-678
lines changed

src/components/Alert/__tests__/Alert.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AlertForTest } from './Alert.story';
99
test.describe('Alert', () => {
1010
test.describe('visual', () => {
1111
[
12+
...propTests.defaultComponentPropTest,
1213
...propTests.feedbackColorPropTest,
1314
...propTests.neutralColorPropTest,
1415
...propTests.iconPropTest,
@@ -19,6 +20,7 @@ test.describe('Alert', () => {
1920
].forEach(({
2021
name,
2122
onBeforeTest,
23+
onBeforeSnapshot,
2224
props,
2325
}) => {
2426
test(name, async ({
@@ -35,6 +37,10 @@ test.describe('Alert', () => {
3537
/>,
3638
);
3739

40+
if (onBeforeSnapshot) {
41+
await onBeforeSnapshot(page, component);
42+
}
43+
3844
const screenshot = await component.screenshot();
3945
expect(screenshot).toMatchSnapshot();
4046
});

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ import {
1515
test.describe('Button', () => {
1616
test.describe('visual', () => {
1717
[
18+
...propTests.defaultComponentPropTest,
1819
...propTests.afterLabelPropTest,
1920
...propTests.beforeLabelPropTest,
2021
...propTests.blockPropTest,
22+
...propTests.endCornerPropTest,
23+
...propTests.feedbackIconPropTest,
24+
...propTests.labelPropTest,
25+
...propTests.labelVisibilityPropTest,
26+
...propTests.sizePropTest,
27+
...propTests.startCornerPropTest,
2128
...mixPropTests([
2229
[
2330
...propTests.actionColorPropTest,
@@ -27,15 +34,10 @@ test.describe('Button', () => {
2734
propTests.disabledPropTest,
2835
propTests.priorityPropTest,
2936
]),
30-
...propTests.endCornerPropTest,
31-
...propTests.feedbackIconPropTest,
32-
...propTests.labelPropTest,
33-
...propTests.labelVisibilityPropTest,
34-
...propTests.sizePropTest,
35-
...propTests.startCornerPropTest,
3637
].forEach(({
3738
name,
3839
onBeforeTest,
40+
onBeforeSnapshot,
3941
props,
4042
}) => {
4143
test(name, async ({
@@ -52,6 +54,10 @@ test.describe('Button', () => {
5254
/>,
5355
);
5456

57+
if (onBeforeSnapshot) {
58+
await onBeforeSnapshot(page, component);
59+
}
60+
5561
const screenshot = await component.screenshot();
5662
expect(screenshot).toMatchSnapshot();
5763
});
@@ -60,13 +66,18 @@ test.describe('Button', () => {
6066

6167
test.describe('non-visual', () => {
6268
test('id', async ({ mount }) => {
69+
const testId = 'testId';
70+
const testLabel = 'testLabel';
71+
6372
const component = await mount(
6473
<ButtonForTest
65-
id="test-id"
74+
id={testId}
75+
label={testLabel}
6676
/>,
6777
);
6878

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

7283
test('ref', async ({ mount }) => {
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import React from 'react';
2+
import {
3+
expect,
4+
test,
5+
} from '@playwright/experimental-ct-react';
6+
import {
7+
mixPropTests,
8+
propTests,
9+
} from '../../../../tests/playwright';
10+
import {
11+
CheckboxFieldForTest,
12+
CheckboxFieldForRefTest,
13+
CheckboxForFormLayoutTests,
14+
} from './CheckboxField.story';
15+
import type { CheckboxForFormLayoutTestsProps } from './CheckboxField.story';
16+
17+
test.describe('CheckboxField', () => {
18+
test.describe('base', () => {
19+
test.describe('visual', () => {
20+
[
21+
...propTests.defaultComponentPropTest,
22+
...propTests.helpTextAndValidationTextPropType,
23+
...propTests.isLabelVisiblePropTest,
24+
...propTests.labelPositionPropTest,
25+
...propTests.labelPropTest,
26+
...propTests.renderAsRequiredPropTest,
27+
...mixPropTests([
28+
propTests.disabledPropTest,
29+
propTests.checkedPropTest,
30+
propTests.validationStatePropTest,
31+
]),
32+
...mixPropTests([
33+
propTests.checkedPropTest,
34+
propTests.requiredPropTest,
35+
propTests.validationStatePropTest,
36+
]),
37+
...mixPropTests([
38+
propTests.checkedPropTest,
39+
propTests.renderAsRequiredPropTest,
40+
propTests.validationStatePropTest,
41+
]),
42+
...mixPropTests([
43+
propTests.checkedPropTest,
44+
propTests.requiredPropTest,
45+
]),
46+
].forEach(({
47+
name,
48+
onBeforeTest,
49+
onBeforeSnapshot,
50+
props,
51+
}) => {
52+
test(name, async ({
53+
mount,
54+
page,
55+
}) => {
56+
if (onBeforeTest) {
57+
await onBeforeTest(page);
58+
}
59+
60+
const component = await mount(
61+
<CheckboxFieldForTest
62+
{...props}
63+
/>,
64+
);
65+
66+
if (onBeforeSnapshot) {
67+
await onBeforeSnapshot(page, component);
68+
}
69+
70+
const screenshot = await component.screenshot();
71+
expect(screenshot).toMatchSnapshot();
72+
});
73+
});
74+
});
75+
76+
test.describe('non-visual', () => {
77+
test('id', async ({ mount }) => {
78+
const idValue = 'checkbox-id';
79+
const label = 'checkbox-label';
80+
const helpText = 'checkbox-helpText';
81+
const validationText = 'checkbox-validationText';
82+
83+
const component = await mount(
84+
<CheckboxFieldForTest
85+
helpText={helpText}
86+
id={idValue}
87+
label={label}
88+
validationText={validationText}
89+
/>,
90+
);
91+
92+
await expect(component.getByRole('checkbox')).toHaveAttribute('id', idValue);
93+
await expect(component).toHaveAttribute('id', `${idValue}__label`);
94+
await expect(component.getByText(label)).toHaveAttribute('id', `${idValue}__labelText`);
95+
await expect(component.getByText(helpText)).toHaveAttribute('id', `${idValue}__helpText`);
96+
await expect(component.getByText(validationText)).toHaveAttribute('id', `${idValue}__validationText`);
97+
});
98+
99+
test('ref', async ({ mount }) => {
100+
const component = await mount(
101+
<CheckboxFieldForRefTest
102+
testRefAttrName="test-ref"
103+
testRefAttrValue="test-ref-value"
104+
/>,
105+
);
106+
107+
await expect(component.getByRole('checkbox')).toHaveAttribute('test-ref', 'test-ref-value');
108+
});
109+
});
110+
111+
test.describe('functionality', () => {
112+
test('calls synthetic event onChange()', async ({ mount }) => {
113+
let changeCalled = false;
114+
115+
const component = await mount(
116+
<CheckboxFieldForTest
117+
onChange={() => {
118+
changeCalled = true;
119+
}}
120+
/>,
121+
);
122+
123+
await component.click({ force: true });
124+
expect(changeCalled).toBeTruthy();
125+
});
126+
127+
test('check on space press when focused', async ({ mount }) => {
128+
const component = await mount(
129+
<CheckboxFieldForTest />,
130+
);
131+
132+
const input = component.getByRole('checkbox');
133+
await input.focus();
134+
await input.press('Space');
135+
await expect(input).toBeChecked();
136+
});
137+
});
138+
});
139+
140+
test.describe('formLayout', () => {
141+
test.describe('visual', () => {
142+
[
143+
...propTests.layoutPropTest,
144+
].forEach(({
145+
name,
146+
onBeforeTest,
147+
onBeforeSnapshot,
148+
props,
149+
}) => {
150+
test(name, async ({
151+
mount,
152+
page,
153+
}) => {
154+
if (onBeforeTest) {
155+
await onBeforeTest(page);
156+
}
157+
158+
const component = await mount(
159+
<CheckboxForFormLayoutTests
160+
{...props as unknown as CheckboxForFormLayoutTestsProps}
161+
/>,
162+
);
163+
164+
if (onBeforeSnapshot) {
165+
await onBeforeSnapshot(page, component);
166+
}
167+
168+
const screenshot = await component.screenshot();
169+
expect(screenshot).toMatchSnapshot();
170+
});
171+
});
172+
});
173+
});
174+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, {
2+
useEffect,
3+
useMemo,
4+
useRef,
5+
} from 'react';
6+
import type { LabelHTMLAttributes } from 'react';
7+
import { FormLayoutContext } from '../../FormLayout';
8+
import { CheckboxField } from '..';
9+
10+
// Types for story component will be improved when we have full TypeScript support
11+
type CheckboxFieldForTestProps = LabelHTMLAttributes<HTMLLabelElement>;
12+
type CheckboxFieldForRefTestProps = CheckboxFieldForTestProps & {
13+
testRefAttrName: string;
14+
testRefAttrValue: string;
15+
};
16+
export type CheckboxForFormLayoutTestsProps = CheckboxFieldForTestProps & {
17+
layout: 'vertical' | 'horizontal'
18+
};
19+
20+
const defaultLabel = 'test-label';
21+
22+
export const CheckboxFieldForTest = ({
23+
...props
24+
} : CheckboxFieldForTestProps) => (
25+
<CheckboxField
26+
label={defaultLabel}
27+
{...props}
28+
/>
29+
);
30+
31+
export const CheckboxFieldForRefTest = ({
32+
testRefAttrName,
33+
testRefAttrValue,
34+
...props
35+
} : CheckboxFieldForRefTestProps) => {
36+
const ref = useRef<HTMLLabelElement>(undefined);
37+
38+
useEffect(() => {
39+
ref.current?.setAttribute(testRefAttrName, testRefAttrValue);
40+
}, [testRefAttrName, testRefAttrValue]);
41+
42+
return (
43+
<CheckboxField
44+
label={defaultLabel}
45+
{...props}
46+
ref={ref}
47+
/>
48+
);
49+
};
50+
51+
export const CheckboxForFormLayoutTests = ({
52+
layout,
53+
...props
54+
} : CheckboxForFormLayoutTestsProps) => {
55+
const values = useMemo(() => ({ layout }), [layout]);
56+
57+
return (
58+
<FormLayoutContext.Provider
59+
value={values}
60+
>
61+
<CheckboxField
62+
label={defaultLabel}
63+
{...props}
64+
/>
65+
<CheckboxField
66+
label="another-test-label"
67+
{...props}
68+
/>
69+
</FormLayoutContext.Provider>
70+
);
71+
};

0 commit comments

Comments
 (0)