Skip to content

Commit 96b38a7

Browse files
authored
Merge pull request #115 from JJ-Cro/WsStore040226
feat(2.4.1):add tests for isDeepObjectMatch function
2 parents 48362d2 + 98f10d5 commit 96b38a7

File tree

4 files changed

+176
-7
lines changed

4 files changed

+176
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kucoin-api",
3-
"version": "2.4.0",
3+
"version": "2.4.1",
44
"description": "Complete & robust Node.js SDK for Kucoin's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
55
"scripts": {
66
"clean": "rm -rf dist",

src/lib/websocket/WsStore.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ export function isDeepObjectMatch(object1: any, object2: any): boolean {
3636
const value1 = object1[key];
3737
const value2 = object2[key];
3838

39-
if (typeof value1 === 'object' && typeof value2 === 'object') {
39+
if (
40+
typeof value1 === 'object' &&
41+
typeof value2 === 'object' &&
42+
value1 !== null &&
43+
value2 !== null
44+
) {
4045
if (!isDeepObjectMatch(value1, value2)) {
4146
return false;
4247
}
43-
}
44-
45-
if (value1 !== value2) {
48+
} else if (value1 !== value2) {
4649
return false;
4750
}
4851
}

test/websockets/wsStore.test.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { isDeepObjectMatch } from '../../src/lib/websocket/WsStore.js';
2+
3+
describe('WsStore', () => {
4+
describe('isDeepObjectMatch()', () => {
5+
it('should match two equal strings', () => {
6+
expect(
7+
isDeepObjectMatch('/market/ticker:BTC-USDT', '/market/ticker:BTC-USDT'),
8+
).toBeTruthy();
9+
expect(
10+
isDeepObjectMatch('/market/ticker:BTC-USDT', '/market/ticker:ETH-USDT'),
11+
).toBeFalsy();
12+
});
13+
14+
it('should match simple topic objects', () => {
15+
const topic1 = {
16+
topic: '/market/ticker:BTC-USDT',
17+
};
18+
const topic2 = {
19+
topic: '/market/ticker:BTC-USDT',
20+
};
21+
22+
expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy();
23+
});
24+
25+
it('should match topic objects with payload, even if keys are differently ordered', () => {
26+
const topic1 = {
27+
topic: 'kline',
28+
payload: {
29+
tradeType: 'SPOT',
30+
symbol: 'BTC-USDT',
31+
interval: '1min',
32+
},
33+
};
34+
const topic2 = {
35+
payload: {
36+
tradeType: 'SPOT',
37+
symbol: 'BTC-USDT',
38+
interval: '1min',
39+
},
40+
topic: 'kline',
41+
};
42+
43+
expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy();
44+
});
45+
46+
it('should match nested payload objects', () => {
47+
const topic1 = {
48+
topic: 'orderAll',
49+
payload: {
50+
tradeType: 'SPOT',
51+
},
52+
};
53+
const topic2 = {
54+
topic: 'orderAll',
55+
payload: {
56+
tradeType: 'SPOT',
57+
},
58+
};
59+
60+
expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy();
61+
});
62+
63+
it('should NOT match topics with different payload values', () => {
64+
const topic1 = {
65+
topic: 'kline',
66+
payload: {
67+
tradeType: 'SPOT',
68+
symbol: 'BTC-USDT',
69+
interval: '1min',
70+
},
71+
};
72+
const topic2 = {
73+
topic: 'kline',
74+
payload: {
75+
tradeType: 'SPOT',
76+
symbol: 'ETH-USDT',
77+
interval: '1min',
78+
},
79+
};
80+
81+
expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy();
82+
});
83+
84+
it('should NOT match topics with nested payload differences', () => {
85+
const topic1 = {
86+
topic: 'kline',
87+
payload: {
88+
tradeType: 'SPOT',
89+
symbol: 'BTC-USDT',
90+
interval: '1min',
91+
},
92+
};
93+
const topic2 = {
94+
topic: 'kline',
95+
payload: {
96+
tradeType: 'SPOT',
97+
symbol: 'BTC-USDT',
98+
interval: '5min',
99+
},
100+
};
101+
102+
expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy();
103+
});
104+
105+
it('should NOT match topics with different tradeType', () => {
106+
const topic1 = {
107+
topic: 'orderAll',
108+
payload: {
109+
tradeType: 'SPOT',
110+
},
111+
};
112+
const topic2 = {
113+
topic: 'orderAll',
114+
payload: {
115+
tradeType: 'FUTURES',
116+
},
117+
};
118+
119+
expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy();
120+
});
121+
122+
it('should NOT match asymmetric objects (missing payload property)', () => {
123+
const topic1 = {
124+
topic: 'kline',
125+
payload: {
126+
tradeType: 'SPOT',
127+
symbol: 'BTC-USDT',
128+
interval: '1min',
129+
},
130+
};
131+
const topic2 = {
132+
topic: 'kline',
133+
};
134+
135+
expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy();
136+
});
137+
138+
it('should NOT match asymmetric objects (missing nested property)', () => {
139+
const topic1 = {
140+
topic: 'kline',
141+
payload: {
142+
tradeType: 'SPOT',
143+
symbol: 'BTC-USDT',
144+
interval: '1min',
145+
},
146+
};
147+
const topic2 = {
148+
topic: 'kline',
149+
payload: {
150+
tradeType: 'SPOT',
151+
symbol: 'BTC-USDT',
152+
},
153+
};
154+
155+
expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy();
156+
});
157+
158+
it('should NOT match string to object', () => {
159+
expect(
160+
isDeepObjectMatch('/market/ticker:BTC-USDT', {
161+
topic: '/market/ticker:BTC-USDT',
162+
}),
163+
).toBeFalsy();
164+
});
165+
});
166+
});

0 commit comments

Comments
 (0)