Skip to content

Commit 469adb6

Browse files
committed
improved tests and improve state management
1 parent b174b93 commit 469adb6

8 files changed

+192
-234
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ setupJest.js
2525
webpack.config.babel.js
2626
testUtil.js
2727
src/__tests__
28+
src/testUtil.js
2829
/package

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
verbose: true,
3-
setupFiles:['jest-canvas-mock', "<rootDir>/setupJest.js"],
3+
setupFiles:["<rootDir>/setupJest.js"],
44
modulePathIgnorePatterns:['<rootDir>/src/__tests__/utils',
55
'<rootDir>/package',
66
'<rootDir>/dist']

package-lock.json

Lines changed: 3 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"eslint-plugin-jsx-a11y": "^6.4.1",
6060
"eslint-plugin-react": "^7.21.5",
6161
"eslint-plugin-react-hooks": "^4.2.0",
62-
"jest-canvas-mock": "^2.3.0",
6362
"jest-dom": "^4.0.0",
6463
"jest": "^26.6.1",
6564
"prettier": "^2.1.2",
@@ -69,7 +68,7 @@
6968
"webpack": "^5.2.1"
7069
},
7170
"dependencies": {
72-
"global-input-message": "^2.0.1",
71+
"global-input-message": "^2.0.3",
7372
"qrcode.react": "^1.0.0"
7473
}
7574
}

src/__tests__/mobile-app-and-device-app-communicate.test.js

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,16 @@
1-
import { useGlobalInputApp, createMessageConnector, createInputReceivers } from '../index';
1+
import { useGlobalInputApp, createMessageConnector} from '../index';
22
import { renderHook } from '@testing-library/react-hooks'
3-
3+
import * as testUtil from '../testUtil';
44

55
/**
66
* compare initData to expectedInitData
77
* @param {*} initData
88
* @param {*} expectedInitData
99
*/
10-
expect.extend({
11-
toBeSameInitData(received, expected) {
12-
if (received.action !== expected.action) {
13-
return {
14-
pass: false,
15-
message: () => `action = "${expected.action}" is expected but received "${received.action}" instead`
16-
};
17-
}
18-
if (received.dataType !== expected.dataType) {
19-
return {
20-
pass: false,
21-
message: () => `dataType = "${expected.dataType}" is expected but received "${received.dataType}" instead`
22-
};
23-
}
24-
if (received.form.id !== expected.form.id) {
25-
return {
26-
pass: false,
27-
message: () => `form.id = "${expected.form.id}" is expected but received "${received.form.id}" instead`
28-
};
29-
}
30-
if (received.form.title !== expected.form.title) {
31-
return {
32-
pass: false,
33-
message: () => `form.title = "${expected.form.title}" is expected but received "${received.form.title}" instead`
34-
};
35-
}
36-
if (received.form.label !== expected.form.label) {
37-
return {
38-
pass: false,
39-
message: () => `form.label = "${expected.form.label}" is expected but received "${received.form.label}" instead`
40-
};
41-
}
42-
for (const [index, field] of expected.form.fields.entries()) {
43-
if (field.label !== received.form.fields[index].label) {
44-
return {
45-
pass: false,
46-
message: () => `form.fields[${index}].label = "${field.label}" is expected but received "${received.form.fields[index].label}" instead`
47-
};
48-
}
49-
if (field.id !== received.form.fields[index].id) {
50-
return {
51-
pass: false,
52-
message: () => `form.fields[${index}].id = "${field.id}" is expected but received "${received.form.fields[index].id}" instead`
53-
};
54-
}
55-
if (field.value !== received.form.fields[index].value) {
56-
return {
57-
pass: false,
58-
message: () => `form.fields[${index}].value = "${field.value}" is expected but received "${received.form.fields[index].value}" instead`
59-
};
60-
}
61-
if (field.nLines !== received.form.fields[index].nLines) {
62-
return {
63-
pass: false,
64-
message: () => `form.fields[${index}].nLines = "${field.nLines}" is expected but received "${received.form.fields[index].nLines}" instead`
65-
};
66-
}
67-
}
68-
69-
return {
70-
pass: true,
71-
message: () => `received initData contains the same data as in the expected initData`
72-
};
73-
}
74-
});
10+
expect.extend({toBeSameInitData:testUtil.toBeSameInitData});
7511

7612
it("Device App and Mobile App should be able to communicate", async function () {
7713

78-
79-
8014
const deviceConfig = {
8115
initData: {
8216
action: "input",
@@ -95,7 +29,7 @@ it("Device App and Mobile App should be able to communicate", async function ()
9529
}
9630
};
9731
const deviceApp = {
98-
receiver: createInputReceivers(deviceConfig),//message receivers on the device side
32+
receiver: testUtil.createInputReceivers(deviceConfig),//message receivers on the device side
9933
ui: deviceConfig.initData,
10034
hook:renderHook(() => useGlobalInputApp(deviceConfig)) //calls the hook
10135
}
@@ -113,7 +47,7 @@ it("Device App and Mobile App should be able to communicate", async function ()
11347

11448
const mobileApp = {
11549
con: createMessageConnector(), //creates a connector
116-
receiver: createInputReceivers(), //create promise objects inside callbacks to make testing more intuitive.
50+
receiver: testUtil.createInputReceivers(), //create promise objects inside callbacks to make testing more intuitive.
11751
};
11852
const { initData: initDataReceivedByMobile } = await mobileApp.con.connect(mobileApp.receiver.config, connectionCode) //mobile app connects to the device using the connectionCode that is obtained from the QR Code
11953
mobileApp.ui = initDataReceivedByMobile; //mobile app display user interface from the initData obtained
@@ -156,7 +90,7 @@ it("Device App and Mobile App should be able to communicate", async function ()
15690
}
15791
}
15892
};
159-
deviceApp.receiver = createInputReceivers(deviceConfig2); //create promise objects inside callbacks to make testing more intuitive.
93+
deviceApp.receiver = testUtil.createInputReceivers(deviceConfig2); //create promise objects inside callbacks to make testing more intuitive.
16094
deviceApp.ui = deviceConfig2.initData;
16195

16296
deviceApp.hook.result.current.sendInitData(deviceApp.ui); //device app sends a new mobile user interface

0 commit comments

Comments
 (0)