How to use onloadFn method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

simpleSpec.js

Source: simpleSpec.js Github

copy
1/*jshint browser: true, laxbreak: true, plusplus: false, undef: true, unused: true, smarttabs: true */
2/*global require */
3
4define(
5	[
6		'src/conditional'
7	], function (conditional) {
8		'use strict';
9
10		describe('conditional.js', function() {
11			it('should load', function() {
12				expect(conditional).toBeDefined();
13			});
14
15			function mockNormalize(moduleName) {
16				return moduleName;
17			}
18
19			describe('normalize()', function () {
20
21				it('should turn "load" string into an array of one string', function () {
22					var obj = {
23						test: "boolVar",
24						load: "lib/some_polyfill"
25					};
26					var input = JSON.stringify(obj);
27					var normInput = conditional.normalize(input, mockNormalize);
28					expect(typeof normInput).toBe('string');
29					var parsed = JSON.parse(normInput);
30					expect(typeof parsed.load).toBeDefined('object');
31					expect(parsed.load.length).toBe(1);
32					expect(parsed.load[0]).toBe(obj.load);
33				});
34
35				it('should call provided normalize function on every load entry', function () {
36					var spyNormalize, obj, input, normInput, parsed;
37
38					spyNormalize = jasmine.createSpy('normalize');
39
40					spyNormalize.and.callFake(function (instr) {
41						return instr.toUpperCase();
42					});
43
44					obj = {
45						test: "numberFn",
46						load: ["lib/mod1", "lib/mod2", 'lib/mod3']
47					};
48					input = JSON.stringify(obj);
49					normInput = conditional.normalize(input, spyNormalize);
50					expect(typeof normInput).toBe('string');
51					parsed = JSON.parse(normInput);
52					expect(typeof parsed.load).toBeDefined('object');
53					expect(parsed.load.length).toBe(3);
54					expect(parsed.load[0]).toBe(obj.load[0].toUpperCase());
55					expect(parsed.load[1]).toBe(obj.load[1].toUpperCase());
56					expect(parsed.load[2]).toBe(obj.load[2].toUpperCase());
57					expect(spyNormalize).toHaveBeenCalledWith("lib/mod1");
58					expect(spyNormalize).toHaveBeenCalledWith("lib/mod2");
59					expect(spyNormalize).toHaveBeenCalledWith("lib/mod3");
60				});
61			});
62
63			describe('load()', function () {
64				it('should pick from an array of choices when given a function to invoke', function() {
65					var onLoadFn, loadedModule, requireFn, obj, input, numberToReturn, requireConfig;
66
67					obj = {
68						test: "numberFn",
69						load: ["lib/mod1", "lib/mod2", 'lib/mod3']
70					};
71					input = JSON.stringify(obj);
72					numberToReturn = 0;
73					requireConfig = {
74						config: {
75							conditional: {
76								numberFn: jasmine.createSpy('numberFn').and.callFake(
77									function () {
78										return numberToReturn;
79									}
80								)
81							}
82						}
83					};
84
85					requireFn = jasmine.createSpy('requireFn').and.callFake(
86						function (deps, callbackFn) {
87							callbackFn(loadedModule);
88						}
89					);
90
91					loadedModule = { foo: 'bar' };
92
93					onLoadFn = jasmine.createSpy('onLoadFn');
94
95					conditional.load(input, requireFn, onLoadFn, requireConfig);
96
97					expect(requireConfig.config.conditional.numberFn).toHaveBeenCalled();
98					expect(requireFn).toHaveBeenCalled();
99					expect(requireFn.calls.mostRecent().args[0]).toEqual([ obj.load[numberToReturn] ]);
100					expect(onLoadFn).toHaveBeenCalledWith(loadedModule);
101
102					requireFn.calls.reset();
103					onLoadFn.calls.reset();
104					requireConfig.config.conditional.numberFn.calls.reset();
105					numberToReturn = 2;
106					loadedModule = [ 'brick', 'bat' ];
107
108					conditional.load(input, requireFn, onLoadFn, requireConfig);
109
110					expect(requireConfig.config.conditional.numberFn).toHaveBeenCalled();
111					expect(requireFn).toHaveBeenCalled();
112					expect(requireFn.calls.mostRecent().args[0]).toEqual([ obj.load[numberToReturn] ]);
113					expect(onLoadFn).toHaveBeenCalledWith(loadedModule);
114				});
115
116				it('should load/not load a file in the "load" key when given a boolean var', function() {
117					var onLoadFn;
118					var loadedModule;
119					var requireFn;
120					var obj, input, requireConfig;
121
122					obj = {
123						test: 'boolProp',
124						load: ["lib/mod1"]
125					};
126					input = JSON.stringify(obj);
127					requireConfig = {
128						config: {
129							conditional: {
130								boolProp: false
131							}
132						}
133					};
134
135					requireFn = jasmine.createSpy('requireFn1').and.callFake(
136						function (deps, callbackFn) {
137							callbackFn(loadedModule);
138						}
139					);
140
141					loadedModule = { foo: 'bar' };
142
143					onLoadFn = jasmine.createSpy('onLoadFn1');
144					onLoadFn.error = function () {
145						throw arguments;
146					};
147
148					conditional.load(input, requireFn, onLoadFn, requireConfig);
149
150					expect(requireFn).not.toHaveBeenCalled();
151					expect(onLoadFn).toHaveBeenCalledWith();
152
153					requireFn.calls.reset();
154					onLoadFn.calls.reset();
155					requireConfig.config.conditional.boolProp = true;
156					input = JSON.stringify(obj);
157
158					conditional.load(input, requireFn, onLoadFn, requireConfig);
159
160					expect(requireFn).toHaveBeenCalled();
161					expect(requireFn.calls.mostRecent().args[0]).toEqual( obj.load );
162					expect(onLoadFn).toHaveBeenCalledWith(loadedModule);
163				});
164
165				it('should treat a test function returning boolean as a 0/1 index if a load array is given', function () {
166					var onLoadFn, loadedModule, requireFn, obj, input, boolToReturn, requireConfig;
167
168					obj = {
169						test: "boolFn",
170						load: ["lib/mod1", "lib/mod2", 'lib/mod3']
171					};
172					input = JSON.stringify(obj);
173					boolToReturn = false;
174					requireConfig = {
175						config: {
176							conditional: {
177								boolFn: jasmine.createSpy('boolFn').and.callFake(
178									function () {
179										return boolToReturn;
180									}
181								)
182							}
183						}
184					};
185
186					requireFn = jasmine.createSpy('requireFn').and.callFake(
187						function (deps, callbackFn) {
188							callbackFn(loadedModule);
189						}
190					);
191
192					loadedModule = { foo: 'bar' };
193
194					onLoadFn = jasmine.createSpy('onLoadFn');
195
196					conditional.load(input, requireFn, onLoadFn, requireConfig);
197
198					expect(requireConfig.config.conditional.boolFn).toHaveBeenCalled();
199					expect(requireFn).toHaveBeenCalled();
200					expect(requireFn.calls.mostRecent().args[0]).toEqual([ obj.load[0] ]);
201					expect(onLoadFn).toHaveBeenCalledWith(loadedModule);
202
203					requireFn.calls.reset();
204					onLoadFn.calls.reset();
205					requireConfig.config.conditional.boolFn.calls.reset();
206					boolToReturn = true;
207					loadedModule = [ 'brick', 'bat' ];
208
209					conditional.load(input, requireFn, onLoadFn, requireConfig);
210
211					expect(requireConfig.config.conditional.boolFn).toHaveBeenCalled();
212					expect(requireFn).toHaveBeenCalled();
213					expect(requireFn.calls.mostRecent().args[0]).toEqual([ obj.load[1] ]);
214					expect(onLoadFn).toHaveBeenCalledWith(loadedModule);
215				});
216
217				it('should not do anything if isBuild is true', function () {
218					var requireFn = jasmine.createSpy('requireFn1');
219					var onLoadFn = jasmine.createSpy('onLoadFn1');
220					conditional.load('{"test": "thing","load":"bogus"}', requireFn, onLoadFn, {isBuild:true});
221					expect(requireFn).not.toHaveBeenCalled();
222					expect(onLoadFn).toHaveBeenCalledWith();
223				});
224			});
225		});
226	}
227);
228
Full Screen

list-data-manager-props.test.js

Source: list-data-manager-props.test.js Github

copy
1/**
2 * Copyright (C) 2021 Unicorn a.s.
3 *
4 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6 * version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at
10 * <https://gnu.org/licenses/> for more details.
11 *
12 * You may obtain additional information at <https://unicorn.com> or contact Unicorn a.s. at address: V Kapslovne 2767/2,
13 * Praha 3, Czech Republic or at the email: info@unicorn.com.
14 */
15
16import React from "react";
17import UU5 from "uu5g04";
18import regeneratorRuntime from "regenerator-runtime";
19
20const { mount, shallow, wait } = UU5.Test.Tools;
21
22const loadTest = async (key, wrapper, childrenFn, onLoadFn, dataProp, dataReturn) => {
23  // load render
24  expect(childrenFn).toHaveBeenCalledTimes(1);
25  let childrenParams1 = childrenFn.mock.calls[0][0];
26  expect(childrenParams1).toMatchObject({
27    viewState: "load",
28    errorState: null,
29    errorData: null,
30    data: null,
31  });
32  expect(typeof childrenParams1.handleLoad).toBe("function");
33  expect(typeof childrenParams1.handleReload).toBe("function");
34  expect(typeof childrenParams1.handleCreate).toBe("function");
35  expect(typeof childrenParams1.handleUpdate).toBe("function");
36  expect(typeof childrenParams1.handleDelete).toBe("function");
37  expect(typeof childrenParams1.handleBulkCreate).toBe("function");
38  expect(typeof childrenParams1.handleBulkUpdate).toBe("function");
39  expect(typeof childrenParams1.handleBulkDelete).toBe("function");
40
41  // props.onLoad
42  expect(onLoadFn).toHaveBeenCalledTimes(1);
43  let onLoadFnParams1 = onLoadFn.mock.calls[0][0];
44  let onLoadFnResult1 = onLoadFn.mock.results[0].value;
45  dataProp === null ? expect(onLoadFnParams1).toBeNull() : expect(onLoadFnParams1).toMatchObject(dataProp);
46  expect(onLoadFnResult1).toBeInstanceOf(Promise);
47
48  // waiting for loading
49  await onLoadFnResult1;
50  wrapper.update();
51
52  // ready render
53  expect(childrenFn).toHaveBeenCalledTimes(2);
54  let childrenParams2 = childrenFn.mock.calls[1][0];
55  expect(childrenParams2).toMatchObject({
56    viewState: "ready",
57    errorState: null,
58    errorData: null,
59    data: dataReturn,
60  });
61  expect(typeof childrenParams2.handleLoad).toBe("function");
62  expect(typeof childrenParams2.handleReload).toBe("function");
63  expect(typeof childrenParams1.handleCreate).toBe("function");
64  expect(typeof childrenParams1.handleUpdate).toBe("function");
65  expect(typeof childrenParams1.handleDelete).toBe("function");
66  expect(typeof childrenParams1.handleBulkCreate).toBe("function");
67  expect(typeof childrenParams1.handleBulkUpdate).toBe("function");
68  expect(typeof childrenParams1.handleBulkDelete).toBe("function");
69
70  onLoadFn.mockClear();
71  childrenFn.mockClear();
72  return childrenParams2;
73};
74
75const testHandle = async (
76  key,
77  wrapper,
78  childrenFn,
79  fn,
80  childrenFnCalledTimes,
81  paramsData,
82  resultData1,
83  resultData2
84) => {
85  expect(fn).toHaveBeenCalledTimes(1);
86  let fnParams1 = fn.mock.calls[0][0];
87  let fnParams2 = fn.mock.calls[0][1];
88  let fnResult = fn.mock.results[0].value;
89  expect(fnResult).toBeInstanceOf(Promise);
90
91  if (key === "load" || key === "reload" || key === "create") {
92    expect(fnParams1).toMatchObject(paramsData);
93  } else if (key === "update") {
94    expect(fnParams1).toBe(paramsData.id);
95    expect(fnParams2).toMatchObject(paramsData);
96  } else if (key === "delete") {
97    expect(fnParams1).toBe(paramsData);
98  } else if (key === "bulkCreate") {
99    expect(fnParams1).toMatchObject(paramsData);
100  } else if (key === "bulkUpdate") {
101    expect(fnParams1).toMatchObject(paramsData.map((it) => it.id));
102    expect(fnParams2).toMatchObject(paramsData);
103  } else if (key === "bulkDelete") {
104    expect(fnParams1).toMatchObject(paramsData);
105  }
106
107  // waiting for fn
108  await fnResult;
109  wrapper.update();
110
111  // ready || load
112  expect(childrenFn).toHaveBeenCalledTimes(childrenFnCalledTimes);
113
114  let childrenParams3 = childrenFn.mock.calls[0][0];
115
116  expect(childrenParams3).toMatchObject({
117    viewState: wrapper.props().pessimistic ? key : "ready",
118    errorState: null,
119    errorData: null,
120    data: resultData1,
121  });
122  expect(typeof childrenParams3.handleLoad).toBe("function");
123  expect(typeof childrenParams3.handleReload).toBe("function");
124  expect(typeof childrenParams3.handleCreate).toBe("function");
125  expect(typeof childrenParams3.handleUpdate).toBe("function");
126  expect(typeof childrenParams3.handleDelete).toBe("function");
127  expect(typeof childrenParams3.handleBulkCreate).toBe("function");
128  expect(typeof childrenParams3.handleBulkUpdate).toBe("function");
129  expect(typeof childrenParams3.handleBulkDelete).toBe("function");
130
131  if (resultData2) {
132    // load -> ready
133    let childrenParams4 = childrenFn.mock.calls[1][0];
134    expect(childrenParams4).toMatchObject({
135      viewState: "ready",
136      errorState: null,
137      errorData: null,
138      data: resultData2,
139    });
140    expect(typeof childrenParams4.handleLoad).toBe("function");
141    expect(typeof childrenParams4.handleReload).toBe("function");
142    expect(typeof childrenParams4.handleCreate).toBe("function");
143    expect(typeof childrenParams4.handleUpdate).toBe("function");
144    expect(typeof childrenParams4.handleDelete).toBe("function");
145    expect(typeof childrenParams4.handleBulkCreate).toBe("function");
146    expect(typeof childrenParams4.handleBulkUpdate).toBe("function");
147    expect(typeof childrenParams4.handleBulkDelete).toBe("function");
148  }
149
150  childrenFn.mockClear();
151  fn.mockClear();
152};
153
154const data = [
155  { id: "1", name: "A" },
156  { id: "2", name: "B" },
157  { id: "3", name: "C" },
158];
159const dataProp = { dtoIn: "data" };
160
161const loadFn = () => new Promise((resolve, reject) => resolve(data));
162const getChildrenFn = () => "test";
163
164describe(`UU5.Common.ListDataManager onLoad`, () => {
165  const loadDataParams = { param: "load" };
166
167  it("optimistic", async () => {
168    let onLoadFn = jest.fn().mockImplementation(loadFn);
169    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
170
171    const wrapper = mount(
172      <UU5.Common.ListDataManager onLoad={onLoadFn} data={dataProp}>
173        {childrenFn}
174      </UU5.Common.ListDataManager>
175    );
176
177    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
178
179    // handleLoad
180    childrenParams.handleLoad(loadDataParams);
181
182    // onLoad
183    await testHandle("load", wrapper, childrenFn, onLoadFn, 1, loadDataParams, data);
184  });
185
186  it("pessimistic", async () => {
187    let onLoadFn = jest.fn().mockImplementation(loadFn);
188    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
189
190    const wrapper = mount(
191      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} data={dataProp}>
192        {childrenFn}
193      </UU5.Common.ListDataManager>
194    );
195
196    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
197
198    // handleLoad
199    childrenParams.handleLoad(loadDataParams);
200
201    // onLoad
202    await testHandle("load", wrapper, childrenFn, onLoadFn, 2, loadDataParams, data, data);
203  });
204});
205
206describe(`UU5.Common.ListDataManager onReload`, () => {
207  const dataReload = [{ id: "2", name: "B" }];
208  const reloadDataParams = { param: "reload" };
209  const reloadFn = () => new Promise((resolve, reject) => resolve(dataReload));
210
211  it("optimistic", async () => {
212    let onLoadFn = jest.fn().mockImplementation(loadFn);
213    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
214    let onReloadFn = jest.fn().mockImplementation(reloadFn);
215
216    const wrapper = mount(
217      <UU5.Common.ListDataManager onLoad={onLoadFn} onReload={onReloadFn} data={dataProp}>
218        {childrenFn}
219      </UU5.Common.ListDataManager>
220    );
221
222    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
223
224    // handleReload
225    childrenParams.handleReload(reloadDataParams);
226
227    // onReload
228    await testHandle("reload", wrapper, childrenFn, onReloadFn, 1, reloadDataParams, dataReload);
229  });
230
231  it("pessimistic", async () => {
232    let onLoadFn = jest.fn().mockImplementation(loadFn);
233    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
234    let onReloadFn = jest.fn().mockImplementation(reloadFn);
235
236    const wrapper = mount(
237      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onReload={onReloadFn} data={dataProp}>
238        {childrenFn}
239      </UU5.Common.ListDataManager>
240    );
241
242    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
243
244    // handleReload
245    childrenParams.handleReload(reloadDataParams);
246
247    // onReload
248    await testHandle("reload", wrapper, childrenFn, onReloadFn, 2, reloadDataParams, data, dataReload);
249  });
250});
251
252describe(`UU5.Common.ListDataManager onUpdate`, () => {
253  const dataUpdate = { id: "1", name: "C" };
254  const dataUpdateFromServer = { ...dataUpdate, name: dataUpdate.name + "X" };
255  const updateFn = () => new Promise((resolve, reject) => resolve(dataUpdateFromServer));
256
257  it("optimistic", async () => {
258    let onLoadFn = jest.fn().mockImplementation(loadFn);
259    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
260    let onUpdateFn = jest.fn().mockImplementation(updateFn);
261
262    const wrapper = mount(
263      <UU5.Common.ListDataManager onLoad={onLoadFn} onUpdate={onUpdateFn}>
264        {childrenFn}
265      </UU5.Common.ListDataManager>
266    );
267
268    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
269
270    // update
271    childrenParams.handleUpdate(dataUpdate.id, dataUpdate);
272
273    // onUpdate
274    let expectedData = data.map((it) => (it.id === dataUpdate.id ? dataUpdate : it));
275    let expectedFinalData = data.map((it) => (it.id === dataUpdateFromServer.id ? dataUpdateFromServer : it));
276    await testHandle("update", wrapper, childrenFn, onUpdateFn, 2, dataUpdate, expectedData, expectedFinalData);
277  });
278
279  it("pessimistic", async () => {
280    let onLoadFn = jest.fn().mockImplementation(loadFn);
281    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
282    let onUpdateFn = jest.fn().mockImplementation(updateFn);
283
284    const wrapper = mount(
285      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onUpdate={onUpdateFn}>
286        {childrenFn}
287      </UU5.Common.ListDataManager>
288    );
289
290    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
291
292    // update
293    childrenParams.handleUpdate(dataUpdate.id, dataUpdate);
294
295    // onUpdate
296    let expectedFinalData = data.map((it) => (it.id === dataUpdateFromServer.id ? dataUpdateFromServer : it));
297    await testHandle("update", wrapper, childrenFn, onUpdateFn, 2, dataUpdate, data, expectedFinalData);
298  });
299});
300
301describe(`UU5.Common.ListDataManager onCreate`, () => {
302  const dataCreate = { id: "4", name: "D" };
303  const dataCreateFromServer = { ...dataCreate, name: dataCreate.name + "X" };
304  const createFn = () => new Promise((resolve, reject) => resolve(dataCreateFromServer));
305
306  it("optimistic", async () => {
307    let onLoadFn = jest.fn().mockImplementation(loadFn);
308    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
309    let onCreateFn = jest.fn().mockImplementation(createFn);
310
311    const wrapper = mount(
312      <UU5.Common.ListDataManager onLoad={onLoadFn} onCreate={onCreateFn}>
313        {childrenFn}
314      </UU5.Common.ListDataManager>
315    );
316
317    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
318
319    // create
320    childrenParams.handleCreate(dataCreate);
321
322    // onCreate
323    let expectedData = [...data, dataCreate];
324    let expectedFinalData = [...data, dataCreateFromServer];
325    await testHandle("create", wrapper, childrenFn, onCreateFn, 2, dataCreate, expectedData, expectedFinalData);
326  });
327
328  it("pessimistic", async () => {
329    let onLoadFn = jest.fn().mockImplementation(loadFn);
330    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
331    let onCreateFn = jest.fn().mockImplementation(createFn);
332
333    const wrapper = mount(
334      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onCreate={onCreateFn}>
335        {childrenFn}
336      </UU5.Common.ListDataManager>
337    );
338
339    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
340
341    // create
342    childrenParams.handleCreate(dataCreate);
343
344    // onCreate
345    let expectedFinalData = [...data, dataCreateFromServer];
346    await testHandle("create", wrapper, childrenFn, onCreateFn, 2, dataCreate, data, expectedFinalData);
347  });
348});
349
350describe(`UU5.Common.ListDataManager onDelete`, () => {
351  const dataDelete = { id: "1", name: "A" };
352  const deleteFn = () => new Promise((resolve, reject) => resolve(dataDelete));
353
354  it("optimistic", async () => {
355    let onLoadFn = jest.fn().mockImplementation(loadFn);
356    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
357    let onDeleteFn = jest.fn().mockImplementation(deleteFn);
358
359    const wrapper = mount(
360      <UU5.Common.ListDataManager onLoad={onLoadFn} onDelete={onDeleteFn}>
361        {childrenFn}
362      </UU5.Common.ListDataManager>
363    );
364
365    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
366
367    // delete
368    childrenParams.handleDelete(dataDelete.id);
369
370    // onDelete
371    let expectedData = data.filter((it) => it.id !== dataDelete.id);
372    await testHandle("delete", wrapper, childrenFn, onDeleteFn, 2, dataDelete.id, expectedData);
373  });
374
375  it("pessimistic", async () => {
376    let onLoadFn = jest.fn().mockImplementation(loadFn);
377    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
378    let onDeleteFn = jest.fn().mockImplementation(deleteFn);
379
380    const wrapper = mount(
381      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onDelete={onDeleteFn}>
382        {childrenFn}
383      </UU5.Common.ListDataManager>
384    );
385
386    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
387
388    // delete
389    childrenParams.handleDelete(dataDelete.id);
390
391    // onDelete
392    let expectedData = data.filter((it) => it.id !== dataDelete.id);
393    await testHandle("delete", wrapper, childrenFn, onDeleteFn, 2, dataDelete.id, data, expectedData);
394  });
395});
396
397describe(`UU5.Common.ListDataManager onBulkUpdate`, () => {
398  const dataUpdate = [
399    { id: "3", name: "A" },
400    { id: "1", name: "C" },
401  ];
402  const dataUpdateFromServer = dataUpdate.map((it) => ({ ...it, name: it.name + "X" }));
403  const updateFn = () => new Promise((resolve, reject) => resolve(dataUpdateFromServer));
404
405  it("optimistic", async () => {
406    let onLoadFn = jest.fn().mockImplementation(loadFn);
407    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
408    let onBulkUpdateFn = jest.fn().mockImplementation(updateFn);
409
410    const wrapper = mount(
411      <UU5.Common.ListDataManager onLoad={onLoadFn} onBulkUpdate={onBulkUpdateFn}>
412        {childrenFn}
413      </UU5.Common.ListDataManager>
414    );
415
416    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
417
418    // bulkUpdate
419    childrenParams.handleBulkUpdate(
420      dataUpdate.map((it) => it.id),
421      dataUpdate
422    );
423
424    // onBulkUpdate
425    let expectedData = data.map((it) => dataUpdate.find((upIt) => upIt.id === it.id) || it);
426    let expectedFinalData = data.map((it) => dataUpdateFromServer.find((upIt) => upIt.id === it.id) || it);
427    await testHandle("bulkUpdate", wrapper, childrenFn, onBulkUpdateFn, 2, dataUpdate, expectedData, expectedFinalData);
428  });
429
430  it("pessimistic", async () => {
431    let onLoadFn = jest.fn().mockImplementation(loadFn);
432    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
433    let onBulkUpdateFn = jest.fn().mockImplementation(updateFn);
434
435    const wrapper = mount(
436      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onBulkUpdate={onBulkUpdateFn}>
437        {childrenFn}
438      </UU5.Common.ListDataManager>
439    );
440
441    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
442
443    // bulkUpdate
444    childrenParams.handleBulkUpdate(
445      dataUpdate.map((it) => it.id),
446      dataUpdate
447    );
448
449    // onBulkUpdate
450    let expectedFinalData = data.map((it) => dataUpdateFromServer.find((upIt) => upIt.id === it.id) || it);
451    await testHandle("bulkUpdate", wrapper, childrenFn, onBulkUpdateFn, 2, dataUpdate, data, expectedFinalData);
452  });
453});
454
455describe(`UU5.Common.ListDataManager onBulkCreate`, () => {
456  const dataCreate = [
457    { id: "4", name: "D" },
458    { id: "5", name: "E" },
459  ];
460  const dataCreateFromServer = dataCreate.map((it) => ({ ...it, name: it.name + "X" }));
461  const createFn = () => new Promise((resolve, reject) => resolve(dataCreateFromServer));
462
463  it("optimistic", async () => {
464    let onLoadFn = jest.fn().mockImplementation(loadFn);
465    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
466    let onBulkCreateFn = jest.fn().mockImplementation(createFn);
467
468    const wrapper = mount(
469      <UU5.Common.ListDataManager onLoad={onLoadFn} onBulkCreate={onBulkCreateFn}>
470        {childrenFn}
471      </UU5.Common.ListDataManager>
472    );
473
474    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
475
476    // bulkCreate
477    childrenParams.handleBulkCreate(dataCreate);
478
479    // onBulkCreate
480    let expectedData = [...data, ...dataCreate];
481    let expectedFinalData = [...data, ...dataCreateFromServer];
482    await testHandle("bulkCreate", wrapper, childrenFn, onBulkCreateFn, 2, dataCreate, expectedData, expectedFinalData);
483  });
484
485  it("pessimistic", async () => {
486    let onLoadFn = jest.fn().mockImplementation(loadFn);
487    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
488    let onBulkCreateFn = jest.fn().mockImplementation(createFn);
489
490    const wrapper = mount(
491      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onBulkCreate={onBulkCreateFn}>
492        {childrenFn}
493      </UU5.Common.ListDataManager>
494    );
495
496    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
497
498    // bulkCreate
499    childrenParams.handleBulkCreate(dataCreate);
500
501    // onBulkCreate
502    let expectedFinalData = [...data, ...dataCreateFromServer];
503    await testHandle("bulkCreate", wrapper, childrenFn, onBulkCreateFn, 2, dataCreate, data, expectedFinalData);
504  });
505});
506
507describe(`UU5.Common.ListDataManager onBulkDelete`, () => {
508  const dataDelete = [
509    { id: "3", name: "C" },
510    { id: "1", name: "A" },
511  ];
512  const deleteFn = () => new Promise((resolve, reject) => resolve(dataDelete));
513
514  it("optimistic", async () => {
515    let onLoadFn = jest.fn().mockImplementation(loadFn);
516    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
517    let onBulkDeleteFn = jest.fn().mockImplementation(deleteFn);
518
519    const wrapper = mount(
520      <UU5.Common.ListDataManager onLoad={onLoadFn} onBulkDelete={onBulkDeleteFn}>
521        {childrenFn}
522      </UU5.Common.ListDataManager>
523    );
524
525    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
526
527    // bulkDelete
528    let ids = dataDelete.map((it) => it.id);
529    childrenParams.handleBulkDelete(ids);
530
531    // onBulkDelete
532    let expectedData = data.filter((it) => !dataDelete.some((upIt) => upIt.id === it.id));
533    await testHandle("bulkDelete", wrapper, childrenFn, onBulkDeleteFn, 2, ids, expectedData);
534  });
535
536  it("pessimistic", async () => {
537    let onLoadFn = jest.fn().mockImplementation(loadFn);
538    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
539    let onBulkDeleteFn = jest.fn().mockImplementation(deleteFn);
540
541    const wrapper = mount(
542      <UU5.Common.ListDataManager pessimistic onLoad={onLoadFn} onBulkDelete={onBulkDeleteFn}>
543        {childrenFn}
544      </UU5.Common.ListDataManager>
545    );
546
547    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
548
549    // bulkDelete
550    let ids = dataDelete.map((it) => it.id);
551    childrenParams.handleBulkDelete(ids);
552
553    // onBulkDelete
554    let expectedData = data.filter((it) => !dataDelete.some((upIt) => upIt.id === it.id));
555    await testHandle("bulkDelete", wrapper, childrenFn, onBulkDeleteFn, 2, ids, data, expectedData);
556  });
557});
558
Full Screen

data-manager-props.test.js

Source: data-manager-props.test.js Github

copy
1/**
2 * Copyright (C) 2021 Unicorn a.s.
3 *
4 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6 * version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at
10 * <https://gnu.org/licenses/> for more details.
11 *
12 * You may obtain additional information at <https://unicorn.com> or contact Unicorn a.s. at address: V Kapslovne 2767/2,
13 * Praha 3, Czech Republic or at the email: info@unicorn.com.
14 */
15
16import regeneratorRuntime from "regenerator-runtime";
17import React from "react";
18import UU5 from "uu5g04";
19
20const { mount, shallow, wait } = UU5.Test.Tools;
21
22const loadTest = async (key, wrapper, childrenFn, onLoadFn, dataProp, dataReturn) => {
23  // load render
24  expect(childrenFn).toHaveBeenCalledTimes(1);
25  let childrenParams1 = childrenFn.mock.calls[0][0];
26  expect(childrenParams1).toMatchObject({
27    viewState: "load",
28    errorState: null,
29    errorData: null,
30    data: null,
31  });
32  expect(typeof childrenParams1.handleLoad).toBe("function");
33  expect(typeof childrenParams1.handleReload).toBe("function");
34  expect(typeof childrenParams1.handleUpdate).toBe("function");
35
36  // props.onLoad
37  expect(onLoadFn).toHaveBeenCalledTimes(1);
38  let onLoadFnParams1 = onLoadFn.mock.calls[0][0];
39  let onLoadFnResult1 = onLoadFn.mock.results[0].value;
40  dataProp === null ? expect(onLoadFnParams1).toBeNull() : expect(onLoadFnParams1).toMatchObject(dataProp);
41  expect(onLoadFnResult1).toBeInstanceOf(Promise);
42
43  // waiting for loading
44  await onLoadFnResult1;
45  wrapper.update();
46
47  // ready render
48  expect(childrenFn).toHaveBeenCalledTimes(2);
49  let childrenParams2 = childrenFn.mock.calls[1][0];
50  expect(childrenParams2).toMatchObject({
51    viewState: "ready",
52    errorState: null,
53    errorData: null,
54    data: dataReturn,
55  });
56  expect(typeof childrenParams2.handleLoad).toBe("function");
57  expect(typeof childrenParams2.handleReload).toBe("function");
58  expect(typeof childrenParams2.handleUpdate).toBe("function");
59
60  return childrenParams2;
61};
62
63const testHandle = async (key, wrapper, childrenFn, fn, calledTimes, paramsData, resultData1, resultData2) => {
64  expect(fn).toHaveBeenCalledTimes(calledTimes);
65  let fnParams = fn.mock.calls[calledTimes - 1][0];
66  let fnResult = fn.mock.results[calledTimes - 1].value;
67  expect(fnParams).toMatchObject(paramsData);
68  expect(fnResult).toBeInstanceOf(Promise);
69
70  // waiting for fn
71  await fnResult;
72  wrapper.update();
73
74  // load -> ready -> ready || load
75  expect(childrenFn).toHaveBeenCalledTimes(resultData2 ? 4 : 3);
76  let childrenParams3 = childrenFn.mock.calls[2][0];
77  expect(childrenParams3).toMatchObject({
78    viewState: resultData2 ? key : "ready",
79    errorState: null,
80    errorData: null,
81    data: resultData1,
82  });
83  expect(typeof childrenParams3.handleLoad).toBe("function");
84  expect(typeof childrenParams3.handleReload).toBe("function");
85  expect(typeof childrenParams3.handleUpdate).toBe("function");
86
87  if (resultData2) {
88    // load -> ready -> load -> ready
89    let childrenParams4 = childrenFn.mock.calls[3][0];
90    expect(childrenParams4).toMatchObject({
91      viewState: "ready",
92      errorState: null,
93      errorData: null,
94      data: resultData2,
95    });
96    expect(typeof childrenParams4.handleLoad).toBe("function");
97    expect(typeof childrenParams4.handleReload).toBe("function");
98    expect(typeof childrenParams4.handleUpdate).toBe("function");
99  }
100};
101
102const data = { name: "AppName" };
103const dataProp = { dtoIn: "data" };
104
105const loadFn = () => new Promise((resolve, reject) => resolve(data));
106const getChildrenFn = () => "test";
107
108describe(`UU5.Common.DataManager onLoad`, () => {
109  const loadDataParams = { param: "load" };
110
111  it("optimistic", async () => {
112    let onLoadFn = jest.fn().mockImplementation(loadFn);
113    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
114
115    const wrapper = mount(
116      <UU5.Common.DataManager onLoad={onLoadFn} data={dataProp}>
117        {childrenFn}
118      </UU5.Common.DataManager>
119    );
120
121    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
122
123    // handleLoad
124    childrenParams.handleLoad(loadDataParams);
125
126    // onLoad
127    await testHandle("load", wrapper, childrenFn, onLoadFn, 2, loadDataParams, data);
128  });
129
130  it("pessimistic", async () => {
131    let onLoadFn = jest.fn().mockImplementation(loadFn);
132    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
133
134    const wrapper = mount(
135      <UU5.Common.DataManager pessimistic onLoad={onLoadFn} data={dataProp}>
136        {childrenFn}
137      </UU5.Common.DataManager>
138    );
139
140    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
141
142    // handleLoad
143    childrenParams.handleLoad(loadDataParams);
144
145    // onLoad
146    await testHandle("load", wrapper, childrenFn, onLoadFn, 2, loadDataParams, data, data);
147  });
148});
149
150describe(`UU5.Common.DataManager onReload`, () => {
151  const dataReload = { name: "AppNameReload" };
152  const reloadDataParams = { param: "reload" };
153  const reloadFn = () => new Promise((resolve, reject) => resolve(dataReload));
154
155  it("optimistic", async () => {
156    let onLoadFn = jest.fn().mockImplementation(loadFn);
157    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
158    let onReloadFn = jest.fn().mockImplementation(reloadFn);
159
160    const wrapper = mount(
161      <UU5.Common.DataManager onLoad={onLoadFn} onReload={onReloadFn} data={dataProp}>
162        {childrenFn}
163      </UU5.Common.DataManager>
164    );
165
166    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
167
168    // handleReload
169    childrenParams.handleReload(reloadDataParams);
170
171    // onReload
172    await testHandle("reload", wrapper, childrenFn, onReloadFn, 1, reloadDataParams, dataReload);
173  });
174
175  it("pessimistic", async () => {
176    let onLoadFn = jest.fn().mockImplementation(loadFn);
177    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
178    let onReloadFn = jest.fn().mockImplementation(reloadFn);
179
180    const wrapper = mount(
181      <UU5.Common.DataManager pessimistic onLoad={onLoadFn} onReload={onReloadFn} data={dataProp}>
182        {childrenFn}
183      </UU5.Common.DataManager>
184    );
185
186    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, dataProp, data);
187
188    // handleReload
189    childrenParams.handleReload(reloadDataParams);
190
191    // onReload
192    await testHandle("reload", wrapper, childrenFn, onReloadFn, 1, reloadDataParams, data, dataReload);
193  });
194});
195
196describe(`UU5.Common.DataManager onUpdate`, () => {
197  const dataUpdate = { name: "AppNameUpdate" };
198  const updateFn = () => new Promise((resolve, reject) => resolve(dataUpdate));
199
200  it("optimistic", async () => {
201    let onLoadFn = jest.fn().mockImplementation(loadFn);
202    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
203    let onUpdateFn = jest.fn().mockImplementation(updateFn);
204
205    const wrapper = mount(
206      <UU5.Common.DataManager onLoad={onLoadFn} onUpdate={onUpdateFn}>
207        {childrenFn}
208      </UU5.Common.DataManager>
209    );
210
211    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
212
213    // update
214    childrenParams.handleUpdate(dataUpdate);
215
216    // onUpdate
217    await testHandle("update", wrapper, childrenFn, onUpdateFn, 1, dataUpdate, dataUpdate);
218  });
219
220  it("pessimistic", async () => {
221    let onLoadFn = jest.fn().mockImplementation(loadFn);
222    let childrenFn = jest.fn().mockImplementation(getChildrenFn);
223    let onUpdateFn = jest.fn().mockImplementation(updateFn);
224
225    const wrapper = mount(
226      <UU5.Common.DataManager pessimistic onLoad={onLoadFn} onUpdate={onUpdateFn}>
227        {childrenFn}
228      </UU5.Common.DataManager>
229    );
230
231    let childrenParams = await loadTest("onLoad", wrapper, childrenFn, onLoadFn, null, data);
232
233    // update
234    childrenParams.handleUpdate(dataUpdate);
235
236    // onUpdate
237    await testHandle("update", wrapper, childrenFn, onUpdateFn, 1, dataUpdate, data, dataUpdate);
238  });
239});
240
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)