Skip to content

Commit ba50139

Browse files
committed
fix JS tests
1 parent 55ad5b4 commit ba50139

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

src/client/package-lock.json

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/packages/idom-client-react/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"description": "A client for IDOM implemented in React",
88
"devDependencies": {
99
"jsdom": "16.3.0",
10+
"lodash": "^4.17.21",
1011
"prettier": "^2.5.1",
1112
"uvu": "^0.5.1"
1213
},
@@ -25,8 +26,8 @@
2526
"url": "https://github.com/idom-team/idom"
2627
},
2728
"scripts": {
28-
"format": "prettier --write ./src",
29-
"check-format": "prettier --check ./src",
29+
"format": "prettier --write ./src ./tests",
30+
"check-format": "prettier --check ./src ./tests",
3031
"test": "uvu tests"
3132
},
3233
"type": "module",

src/client/packages/idom-client-react/tests/event-to-object.test.js

+57-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
import { test } from "uvu";
2+
import lodash from "lodash";
23
import * as assert from "uvu/assert";
34
import { serializeEvent } from "../src/event-to-object.js";
45
import "./tooling/setup.js";
56

7+
function assertEqualSerializedEventData(eventData, expectedSerializedData) {
8+
const mockBoundingRect = {
9+
left: 0,
10+
top: 0,
11+
right: 0,
12+
bottom: 0,
13+
x: 0,
14+
y: 0,
15+
width: 0,
16+
};
17+
18+
const mockElement = {
19+
tagName: null,
20+
getBoundingClientRect: () => mockBoundingRect,
21+
};
22+
23+
const commonEventData = {
24+
target: mockElement,
25+
currentTarget: mockElement,
26+
relatedTarget: mockElement,
27+
};
28+
29+
const commonSerializedEventData = {
30+
target: { boundingClientRect: mockBoundingRect },
31+
currentTarget: { boundingClientRect: mockBoundingRect },
32+
relatedTarget: { boundingClientRect: mockBoundingRect },
33+
};
34+
35+
assert.equal(
36+
serializeEvent(lodash.merge({}, commonEventData, eventData)),
37+
lodash.merge({}, commonSerializedEventData, expectedSerializedData)
38+
);
39+
}
40+
641
const allTargetData = {
742
files: [
843
{
@@ -21,33 +56,38 @@ const allTargetData = {
2156
{
2257
case: "adds 'files' and 'value' attributes for INPUT if type=file",
2358
tagName: "INPUT",
24-
otherAttrs: { type: "file" },
59+
addTargetAttrs: { type: "file" },
2560
output: {
26-
files: allTargetData.files,
27-
value: allTargetData.value,
61+
target: {
62+
files: allTargetData.files,
63+
value: allTargetData.value,
64+
},
2865
},
2966
},
3067
...["BUTTON", "INPUT", "OPTION", "LI", "METER", "PROGRESS", "PARAM"].map(
3168
(tagName) => ({
3269
case: `adds 'value' attribute for ${tagName} element`,
3370
tagName,
34-
output: { value: allTargetData.value },
71+
output: { target: { value: allTargetData.value } },
3572
})
3673
),
3774
...["AUDIO", "VIDEO"].map((tagName) => ({
3875
case: `adds 'currentTime' attribute for ${tagName} element`,
3976
tagName,
40-
output: { currentTime: allTargetData.currentTime },
77+
output: { target: { currentTime: allTargetData.currentTime } },
4178
})),
4279
].forEach((expectation) => {
4380
test(`serializeEvent() ${expectation.case}`, () => {
4481
const eventData = {
45-
target: { ...allTargetData, tagName: expectation.tagName },
82+
target: {
83+
...allTargetData,
84+
tagName: expectation.tagName,
85+
},
4686
};
47-
if (expectation.otherAttrs) {
48-
Object.assign(eventData.target, expectation.otherAttrs);
87+
if (expectation.addTargetAttrs) {
88+
Object.assign(eventData.target, expectation.addTargetAttrs);
4989
}
50-
assert.equal(serializeEvent(eventData), expectation.output);
90+
assertEqualSerializedEventData(eventData, expectation.output);
5191
});
5292
});
5393

@@ -247,8 +287,8 @@ const allEventData = {
247287
},
248288
].forEach((expectation) => {
249289
test(`serializeEvent() adds ${expectation.case} attributes`, () => {
250-
assert.equal(
251-
serializeEvent({ ...allEventData, type: expectation.eventType }),
290+
assertEqualSerializedEventData(
291+
{ ...allEventData, type: expectation.eventType },
252292
expectation.output
253293
);
254294
});
@@ -267,9 +307,12 @@ test("serializeEvent() adds text of current selection", () => {
267307
const start = document.getElementById("start");
268308
const end = document.getElementById("end");
269309
window.getSelection().setBaseAndExtent(start, 0, end, 0);
270-
assert.equal(serializeEvent({ ...allEventData, type: "select" }), {
271-
selectedText: "START\nMIDDLE\n",
272-
});
310+
assertEqualSerializedEventData(
311+
{ ...allEventData, type: "select" },
312+
{
313+
selectedText: "START\nMIDDLE\n",
314+
}
315+
);
273316
});
274317

275318
test.run();

0 commit comments

Comments
 (0)