Skip to content

Commit 270ca80

Browse files
committed
[material] Rewrite test based on enzyme
1 parent 32c5986 commit 270ca80

18 files changed

+1183
-1607
lines changed

packages/material/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"react-redux": "^6.0.0"
6868
},
6969
"devDependencies": {
70+
"@types/enzyme": "^3.1.15",
71+
"@types/enzyme-adapter-react-16": "^1.0.3",
7072
"@jsonforms/core": "^2.2.1-alpha.2",
7173
"@jsonforms/react": "^2.2.1-alpha.2",
7274
"@jsonforms/test": "^2.2.1-alpha.2",

packages/material/test/renderers/MaterialArrayControl.test.tsx

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import {
26-
Actions,
27-
ControlElement,
28-
jsonformsReducer,
29-
JsonFormsState,
30-
JsonSchema
31-
} from '@jsonforms/core';
25+
import { Actions, ControlElement, jsonformsReducer, JsonFormsState, JsonSchema } from '@jsonforms/core';
3226
import * as React from 'react';
33-
import * as _ from 'lodash';
3427
import { Provider } from 'react-redux';
35-
import * as TestUtils from 'react-dom/test-utils';
3628

3729
import MaterialArrayControlRenderer from '../../src/complex/MaterialArrayControlRenderer';
3830
import { combineReducers, createStore, Store } from 'redux';
3931
import { materialFields, materialRenderers } from '../../src';
32+
import Enzyme, { mount, ReactWrapper } from 'enzyme';
33+
import Adapter from 'enzyme-adapter-react-16';
34+
35+
Enzyme.configure({ adapter: new Adapter() });
4036

4137
export const initJsonFormsStore = (customData?: any): Store<JsonFormsState> => {
4238
const s: JsonFormsState = {
@@ -93,35 +89,38 @@ const fixture: {
9389

9490
describe('Material array control', () => {
9591

92+
let wrapper: ReactWrapper;
93+
94+
afterEach(() => wrapper.unmount());
95+
9696
it('should render', () => {
9797
const store = initJsonFormsStore();
98-
const tree: React.Component<any> = TestUtils.renderIntoDocument(
98+
wrapper = mount(
9999
<Provider store={store}>
100100
<MaterialArrayControlRenderer schema={fixture.schema} uischema={fixture.uischema}/>
101101
</Provider>
102-
) as React.Component<any>;
102+
);
103103

104-
const rows = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr');
104+
const rows = wrapper.find('tr');
105105
// 2 header rows + 2 data entries
106106
expect(rows.length).toBe(4);
107107
});
108108

109109
it('should render empty', () => {
110110
const store = initJsonFormsStore([]);
111111

112-
const tree: React.Component<any> = TestUtils.renderIntoDocument(
112+
wrapper = mount(
113113
<Provider store={store}>
114114
<MaterialArrayControlRenderer schema={fixture.schema} uischema={fixture.uischema}/>
115115
</Provider>
116-
) as React.Component<any>;
116+
);
117117

118-
const rows: HTMLTableRowElement[] =
119-
TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr') as HTMLTableRowElement[];
118+
const rows = wrapper.find('tr');
120119
// two header rows + no data row
121120
expect(rows.length).toBe(3);
122-
const headerColumns = rows[1].cells;
123-
// 3 columsn = message & done properties + column for delete button
124-
expect(headerColumns.length).toBe(3);
121+
const headerColumns = rows.at(1).children();
122+
// 3 columns: message & done properties + column for delete button
123+
expect(headerColumns).toHaveLength(3);
125124
});
126125

127126
it('should render empty primitives', () => {
@@ -143,20 +142,19 @@ describe('Material array control', () => {
143142
};
144143
store.dispatch(Actions.init(data, schema, uischema));
145144

146-
const tree: React.Component<any> = TestUtils.renderIntoDocument(
145+
wrapper = mount(
147146
<Provider store={store}>
148147
<MaterialArrayControlRenderer schema={schema} uischema={uischema}/>
149148
</Provider>
150-
) as React.Component<any>;
149+
);
151150

152-
const rows: HTMLTableRowElement[] =
153-
TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr') as HTMLTableRowElement[];
151+
const rows = wrapper.find('tr');
154152
// header + no data row
155-
expect(rows.length).toBe(2);
156-
const emptyDataCol = rows[1].cells;
157-
expect(emptyDataCol.length).toBe(1);
153+
expect(rows).toHaveLength(2);
154+
const emptyDataCol = rows.at(1).find('td');
155+
expect(emptyDataCol).toHaveLength(1);
158156
// selection column + 1 data column
159-
expect(emptyDataCol[0].colSpan).toBe(2);
157+
expect(emptyDataCol.first().props().colSpan).toBe(2);
160158
});
161159

162160
it('should render primitives', () => {
@@ -178,39 +176,40 @@ describe('Material array control', () => {
178176
};
179177
store.dispatch(Actions.init(data, schema, uischema));
180178

181-
const tree: React.Component<any> = TestUtils.renderIntoDocument(
179+
wrapper = mount(
182180
<Provider store={store}>
183181
<MaterialArrayControlRenderer schema={schema} uischema={uischema}/>
184182
</Provider>
185-
) as React.Component<any>;
183+
);
186184

187-
const rows = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr');
185+
const rows = wrapper.find('tr');
188186
// header + 2 data entries
189-
expect(rows.length).toBe(3);
187+
expect(rows).toHaveLength(3);
190188
});
191189

192190
it('should delete an item', () => {
193191
const store = initJsonFormsStore();
194-
const tree: React.Component<any> = TestUtils.renderIntoDocument(
192+
wrapper = mount(
195193
<Provider store={store}>
196194
<MaterialArrayControlRenderer schema={fixture.schema} uischema={fixture.uischema} />
197195
</Provider>
198-
) as React.Component<any>;
196+
);
199197

200-
const buttons = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'button');
198+
const buttons = wrapper.find('button');
201199
// 5 buttons
202200
// add row
203201
// delete row
204202
// delete row
205203
// two dialog buttons (no + yes)
206-
const deleteButton = buttons[1];
207-
const nrOfRowsBeforeDelete = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr').length;
204+
const nrOfRowsBeforeDelete = wrapper.find('tr').length;
205+
206+
const deleteButton = buttons.at(1);
207+
deleteButton.simulate('click');
208208

209-
TestUtils.Simulate.click(deleteButton);
210-
const confirmButton = buttons[4];
211-
TestUtils.Simulate.click(confirmButton);
209+
const confirmButton = buttons.at(4);
210+
confirmButton.simulate('click');
212211

213-
const nrOfRowsAfterDelete = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'tr').length;
212+
const nrOfRowsAfterDelete = wrapper.find('tr').length;
214213

215214
expect(nrOfRowsBeforeDelete).toBe(4);
216215
expect(nrOfRowsAfterDelete).toBe(3);

packages/material/test/renderers/MaterialArrayLayout.test.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import { Actions, ControlElement, jsonformsReducer, JsonFormsState } from '@jsonforms/core';
2626
import * as React from 'react';
2727
import { Provider } from 'react-redux';
28-
import * as TestUtils from 'react-dom/test-utils';
2928

3029
import { combineReducers, createStore, Store } from 'redux';
3130
import { materialFields, materialRenderers } from '../../src';
3231
import { MaterialArrayLayout, materialArrayLayoutTester } from '../../src/layouts';
32+
import Enzyme, { mount } from 'enzyme';
33+
import Adapter from 'enzyme-adapter-react-16';
34+
35+
Enzyme.configure({ adapter: new Adapter() });
3336

3437
export const initJsonFormsStore = (): Store<JsonFormsState> => {
3538
const s: JsonFormsState ={
@@ -154,49 +157,52 @@ describe('Material array layout', () => {
154157

155158
it('should render two by two children', () => {
156159
const store = initJsonFormsStore();
157-
const tree = TestUtils.renderIntoDocument(
160+
const wrapper = mount(
158161
<Provider store={store}>
159162
<MaterialArrayLayout
160163
schema={schema}
161164
uischema={uischema}
162165
/>
163166
</Provider>
164-
) as React.Component<any, any, any>;
167+
);
165168

166-
const controls = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'input');
169+
const controls = wrapper.find('input');
167170
// 2 data entries with each having 2 controls
168-
expect(controls.length).toBe(4);
171+
expect(controls).toHaveLength(4);
172+
wrapper.unmount();
169173
});
170174

171175
it('should generate uischema when options.detail=GENERATE', () => {
172176
const store = initJsonFormsStore();
173-
const tree = TestUtils.renderIntoDocument(
177+
const wrapper = mount(
174178
<Provider store={store}>
175179
<MaterialArrayLayout
176180
schema={schema}
177181
uischema={uischemaOptions.generate}
178182
/>
179183
</Provider>
180-
) as React.Component<any, any, any>;
184+
);
181185

182-
const controls = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'input');
186+
const controls = wrapper.find('input');
183187
// 2 data entries with each having 2 controls
184-
expect(controls.length).toBe(4);
188+
expect(controls).toHaveLength(4);
189+
wrapper.unmount();
185190
});
186191

187192
it('should use inline options.detail uischema', () => {
188193
const store = initJsonFormsStore();
189-
const tree = TestUtils.renderIntoDocument(
194+
const wrapper = mount(
190195
<Provider store={store}>
191196
<MaterialArrayLayout
192197
schema={schema}
193198
uischema={uischemaOptions.inline}
194199
/>
195200
</Provider>
196-
) as React.Component<any, any, any>;
201+
);
197202

198-
const controls = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'input');
203+
const controls = wrapper.find('input');
199204
// 2 data entries with each having 1 control
200-
expect(controls.length).toBe(2);
205+
expect(controls).toHaveLength(2);
206+
wrapper.unmount();
201207
});
202208
});

0 commit comments

Comments
 (0)