forked from rjsf-team/react-jsonschema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance_test.js
154 lines (124 loc) · 3.96 KB
/
performance_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sinon from "sinon";
import React from "react";
import { scryRenderedComponentsWithType } from "react-addons-test-utils";
import { getDefaultRegistry } from "../src/utils";
import SchemaField from "../src/components/fields/SchemaField";
import {
createComponent,
createFormComponent,
createSandbox,
setProps,
} from "./test_utils";
describe("Rendering performance optimizations", () => {
var sandbox;
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("Form", () => {
it("should not render if next props are equivalent", () => {
const schema = { type: "string" };
const uiSchema = {};
const { comp } = createFormComponent({ schema, uiSchema });
sandbox.stub(comp, "render").returns(<div />);
comp.componentWillReceiveProps({ schema });
sinon.assert.notCalled(comp.render);
});
it("should not render if next formData are equivalent", () => {
const schema = { type: "string" };
const formData = "foo";
const { comp } = createFormComponent({ schema, formData });
sandbox.stub(comp, "render").returns(<div />);
comp.componentWillReceiveProps({ formData });
sinon.assert.notCalled(comp.render);
});
it("should only render changed object properties", () => {
const schema = {
type: "object",
properties: {
const: { type: "string" },
var: { type: "string" },
},
};
const { comp } = createFormComponent({
schema,
formData: { const: "0", var: "0" },
});
const fields = scryRenderedComponentsWithType(comp, SchemaField).reduce(
(fields, fieldComp) => {
sandbox.stub(fieldComp, "render").returns(<div />);
fields[fieldComp.props.idSchema.$id] = fieldComp;
return fields;
}
);
setProps(comp, { schema, formData: { const: "0", var: "1" } });
sinon.assert.notCalled(fields.root_const.render);
sinon.assert.calledOnce(fields.root_var.render);
});
it("should only render changed array items", () => {
const schema = {
type: "array",
items: { type: "string" },
};
const { comp } = createFormComponent({
schema,
formData: ["const", "var0"],
});
const fields = scryRenderedComponentsWithType(comp, SchemaField).reduce(
(fields, fieldComp) => {
sandbox.stub(fieldComp, "render").returns(<div />);
fields[fieldComp.props.idSchema.$id] = fieldComp;
return fields;
}
);
setProps(comp, { schema, formData: ["const", "var1"] });
sinon.assert.notCalled(fields.root_0.render);
sinon.assert.calledOnce(fields.root_1.render);
});
});
describe("SchemaField", () => {
const onChange = () => {};
const onBlur = () => {};
const onFocus = () => {};
const registry = getDefaultRegistry();
const uiSchema = {};
const schema = {
type: "object",
properties: {
foo: { type: "string" },
},
};
const idSchema = { $id: "root", foo: { $id: "root_plop" } };
it("should not render if next props are equivalent", () => {
const props = {
registry,
schema,
uiSchema,
onChange,
idSchema,
onBlur,
onFocus,
};
const { comp } = createComponent(SchemaField, props);
sandbox.stub(comp, "render").returns(<div />);
setProps(comp, props);
sinon.assert.notCalled(comp.render);
});
it("should not render if next formData are equivalent", () => {
const props = {
registry,
schema,
formData: { foo: "blah" },
onChange,
idSchema,
onBlur,
};
const { comp } = createComponent(SchemaField, props);
sandbox.stub(comp, "render").returns(<div />);
setProps(comp, { ...props, formData: { foo: "blah" } });
sinon.assert.notCalled(comp.render);
});
});
});