Skip to content

Commit 038d51e

Browse files
- Responded to reviewer feedback with a few consolidation refactors
1 parent 3c42569 commit 038d51e

File tree

6 files changed

+84
-115
lines changed

6 files changed

+84
-115
lines changed

packages/antd/test/Form.test.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
import renderer from 'react-test-renderer';
22
import validator from '@rjsf/validator-ajv8';
33
import { RJSFSchema } from '@rjsf/utils';
4-
import { formTests, SELECT_CUSTOMIZE } from '@rjsf/snapshot-tests';
4+
import { formTests } from '@rjsf/snapshot-tests';
55

66
import '../__mocks__/matchMedia.mock';
77
import Form from '../src';
8+
import { FORM_RENDER_OPTIONS } from './snapshotConstants';
89

9-
formTests(Form, {
10-
[SELECT_CUSTOMIZE]: {
11-
createNodeMock: (element) => {
12-
if (element.type === 'span' && element.props['aria-hidden']) {
13-
// the `rc-select` MultipleSelector code expects a ref for this span to exist, so use the feature of
14-
// react-test-renderer to create one
15-
// See: https://reactjs.org/docs/test-renderer.html#ideas
16-
return { scrollWidth: 100 };
17-
}
18-
return null;
19-
},
20-
},
21-
});
10+
formTests(Form, FORM_RENDER_OPTIONS);
2211

2312
describe('antd specific tests', () => {
2413
test('descriptionLocation tooltip in formContext', () => {

packages/antd/test/GridSnap.test.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { gridTests } from '@rjsf/snapshot-tests';
1+
import { gridTests, SELECT_CUSTOMIZE } from '@rjsf/snapshot-tests';
22

33
import '../__mocks__/matchMedia.mock';
44
import Form from '../src';
5+
import { FORM_RENDER_OPTIONS } from './snapshotConstants';
56

67
gridTests(
78
Form,
@@ -234,15 +235,5 @@ gridTests(
234235
},
235236
},
236237
},
237-
{
238-
createNodeMock: (element) => {
239-
if (element.type === 'span' && element.props['aria-hidden']) {
240-
// the `rc-select` MultipleSelector code expects a ref for this span to exist, so use the feature of
241-
// react-test-renderer to create one
242-
// See: https://reactjs.org/docs/test-renderer.html#ideas
243-
return { scrollWidth: 100 };
244-
}
245-
return null;
246-
},
247-
}
238+
FORM_RENDER_OPTIONS[SELECT_CUSTOMIZE]
248239
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { SELECT_CUSTOMIZE } from '@rjsf/snapshot-tests';
2+
3+
export const FORM_RENDER_OPTIONS = {
4+
[SELECT_CUSTOMIZE]: {
5+
createNodeMock: (element: any) => {
6+
if (element.type === 'span' && element.props['aria-hidden']) {
7+
// the `rc-select` MultipleSelector code expects a ref for this span to exist, so use the feature of
8+
// react-test-renderer to create one
9+
// See: https://reactjs.org/docs/test-renderer.html#ideas
10+
return { scrollWidth: 100 };
11+
}
12+
return null;
13+
},
14+
},
15+
};

packages/mui/test/Form.test.tsx

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,9 @@
1-
import { formTests, SLIDER_CUSTOMIZE, TEXTAREA_CUSTOMIZE } from '@rjsf/snapshot-tests';
1+
import { formTests } from '@rjsf/snapshot-tests';
22

33
import Form from '../src';
4+
import { COMPUTED_STYLE_MOCK, FORM_RENDER_OPTIONS } from './snapshotConstants';
45

56
// The `TextareaAutosize` code reads the following data from the `getComputedStyle()` function in a useEffect hook
6-
jest.spyOn(window, 'getComputedStyle').mockImplementation(() => {
7-
return {
8-
width: 100,
9-
'box-sizing': 10,
10-
'padding-bottom': 1,
11-
'padding-top': 1,
12-
'border-bottom-width': 1,
13-
'border-top-width': 1,
14-
} as unknown as CSSStyleDeclaration;
15-
});
7+
jest.spyOn(window, 'getComputedStyle').mockImplementation(() => COMPUTED_STYLE_MOCK);
168

17-
formTests(Form, {
18-
[TEXTAREA_CUSTOMIZE]: {
19-
createNodeMock: (element) => {
20-
if (element.type === 'textarea') {
21-
// the `TextareaAutosize` code expects a ref for two textareas to exist, so use the feature of
22-
// react-test-renderer to create one
23-
// See: https://reactjs.org/docs/test-renderer.html#ideas
24-
if (element.props['aria-hidden']) {
25-
// The hidden one reads the following values
26-
return {
27-
style: { width: 10 },
28-
scrollHeight: 100,
29-
};
30-
}
31-
// The other one needs to look like an input node with focus and style elements
32-
return {
33-
nodeName: 'INPUT',
34-
focus: jest.fn(),
35-
style: {},
36-
};
37-
}
38-
return null;
39-
},
40-
},
41-
[SLIDER_CUSTOMIZE]: {
42-
createNodeMock: (element) => {
43-
// the `Slider` code expects a ref for a span.root to exist, so use the feature of
44-
// react-test-renderer to create one
45-
// See: https://reactjs.org/docs/test-renderer.html#ideas
46-
if (element.type === 'span' && element.props.id === 'root') {
47-
// Pretend to be an event listening component inside of an event listening document
48-
return {
49-
addEventListener: jest.fn(),
50-
removeEventListener: jest.fn(),
51-
ownerDocument: {
52-
addEventListener: jest.fn(),
53-
removeEventListener: jest.fn(),
54-
},
55-
};
56-
}
57-
return null;
58-
},
59-
},
60-
});
9+
formTests(Form, FORM_RENDER_OPTIONS);

packages/mui/test/GridSnap.test.tsx

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import { gridTests } from '@rjsf/snapshot-tests';
1+
import { gridTests, TEXTAREA_CUSTOMIZE } from '@rjsf/snapshot-tests';
22

33
import Form from '../src';
4+
import { COMPUTED_STYLE_MOCK, FORM_RENDER_OPTIONS } from './snapshotConstants';
45

56
// The `TextareaAutosize` code reads the following data from the `getComputedStyle()` function in a useEffect hook
6-
jest.spyOn(window, 'getComputedStyle').mockImplementation(() => {
7-
return {
8-
width: 100,
9-
'box-sizing': 10,
10-
'padding-bottom': 1,
11-
'padding-top': 1,
12-
'border-bottom-width': 1,
13-
'border-top-width': 1,
14-
} as unknown as CSSStyleDeclaration;
15-
});
7+
jest.spyOn(window, 'getComputedStyle').mockImplementation(() => COMPUTED_STYLE_MOCK);
168

179
gridTests(
1810
Form,
@@ -252,27 +244,5 @@ gridTests(
252244
},
253245
},
254246
},
255-
{
256-
createNodeMock: (element) => {
257-
if (element.type === 'textarea') {
258-
// the `TextareaAutosize` code expects a ref for two textareas to exist, so use the feature of
259-
// react-test-renderer to create one
260-
// See: https://reactjs.org/docs/test-renderer.html#ideas
261-
if (element.props['aria-hidden']) {
262-
// The hidden one reads the following values
263-
return {
264-
style: { width: 10 },
265-
scrollHeight: 100,
266-
};
267-
}
268-
// The other one needs to look like an input node with focus and style elements
269-
return {
270-
nodeName: 'INPUT',
271-
focus: jest.fn(),
272-
style: {},
273-
};
274-
}
275-
return null;
276-
},
277-
}
247+
FORM_RENDER_OPTIONS[TEXTAREA_CUSTOMIZE]
278248
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { SLIDER_CUSTOMIZE, TEXTAREA_CUSTOMIZE } from '@rjsf/snapshot-tests';
2+
3+
export const COMPUTED_STYLE_MOCK = {
4+
width: 100,
5+
'box-sizing': 10,
6+
'padding-bottom': 1,
7+
'padding-top': 1,
8+
'border-bottom-width': 1,
9+
'border-top-width': 1,
10+
} as unknown as CSSStyleDeclaration;
11+
12+
export const FORM_RENDER_OPTIONS = {
13+
[TEXTAREA_CUSTOMIZE]: {
14+
createNodeMock: (element: any) => {
15+
if (element.type === 'textarea') {
16+
// the `TextareaAutosize` code expects a ref for two textareas to exist, so use the feature of
17+
// react-test-renderer to create one
18+
// See: https://reactjs.org/docs/test-renderer.html#ideas
19+
if (element.props['aria-hidden']) {
20+
// The hidden one reads the following values
21+
return {
22+
style: { width: 10 },
23+
scrollHeight: 100,
24+
};
25+
}
26+
// The other one needs to look like an input node with focus and style elements
27+
return {
28+
nodeName: 'INPUT',
29+
focus: jest.fn(),
30+
style: {},
31+
};
32+
}
33+
return null;
34+
},
35+
},
36+
[SLIDER_CUSTOMIZE]: {
37+
createNodeMock: (element: any) => {
38+
// the `Slider` code expects a ref for a span.root to exist, so use the feature of
39+
// react-test-renderer to create one
40+
// See: https://reactjs.org/docs/test-renderer.html#ideas
41+
if (element.type === 'span' && element.props.id === 'root') {
42+
// Pretend to be an event listening component inside of an event listening document
43+
return {
44+
addEventListener: jest.fn(),
45+
removeEventListener: jest.fn(),
46+
ownerDocument: {
47+
addEventListener: jest.fn(),
48+
removeEventListener: jest.fn(),
49+
},
50+
};
51+
}
52+
return null;
53+
},
54+
},
55+
};

0 commit comments

Comments
 (0)