Skip to content

Commit d0b748a

Browse files
committed
Add test for ChannelStore
1 parent 7775cf6 commit d0b748a

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

jest.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
const { defaults } = require('jest-config');
2-
31
module.exports = {
42
roots: ['<rootDir>/src'],
53
collectCoverage: true,
64
collectCoverageFrom: ['src/**/*.js', '!**/node_modules/**'],
75
coverageReporters: ['html', 'text'],
6+
moduleNameMapper: {
7+
'@storybook/addons': '<rootDir>/src/__tests__/__mocks__/storybook-addons.js'
8+
},
9+
testPathIgnorePatterns: ['/node_modules/', '/__mocks__/'],
810
coverageThreshold: {
911
global: {
1012
branches: 0,

jsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"typeAcquisition": {
3+
"include": [
4+
"jest"
5+
]
6+
}
7+
}

src/__tests__/ChannelStore.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import ChannelStore, { getSingleStore } from '../ChannelStore';
2+
import { getConfig } from '../config';
3+
4+
jest.mock('@storybook/addons', () => {
5+
const initMockinfo = {
6+
onEvent: jest.fn(),
7+
reset() {
8+
this.onEvent.mockReset();
9+
},
10+
};
11+
const channel = {
12+
on: (event, cb) => initMockinfo.onEvent([event, cb]),
13+
mock: () => initMockinfo,
14+
};
15+
return {
16+
getChannel: () => channel,
17+
};
18+
});
19+
20+
const config = getConfig();
21+
const configWith = props => ({ ...config, ...props });
22+
const whatSide = isPanel => (isPanel ? 'Panel-Side' : 'Decorator-Side');
23+
24+
describe.each([{ isPanel: false }, { isPanel: true }])(
25+
'ChannelStore %o',
26+
({ isPanel }) => {
27+
it(`should init ${whatSide(isPanel)} Store by default`, () => {
28+
const store = new ChannelStore(configWith({ isPanel }));
29+
store.channel = null; // exclude mocked channel from snapshot
30+
expect(store).toMatchInlineSnapshot(
31+
`
32+
ChannelStore {
33+
"EVENT_ID_BACK": "adk/event/back",
34+
"EVENT_ID_DATA": "adk/event/data",
35+
"EVENT_ID_INIT": "adk/event/init",
36+
"_createAction": [Function],
37+
"channel": null,
38+
"connect": [Function],
39+
"createGlobalAction": [Function],
40+
"createLocalAction": [Function],
41+
"defaultReducer": [Function],
42+
"disconnect": [Function],
43+
"emit": [Function],
44+
"id": undefined,
45+
"init": [Function],
46+
"initData": Object {},
47+
"isPanel": ${isPanel},
48+
"name": "store",
49+
"onConnected": [Function],
50+
"onConnectedFn": [Function],
51+
"onData": [Function],
52+
"onDataChannel": [Function],
53+
"onInitChannel": [Function],
54+
"removeData": [Function],
55+
"removeInit": [Function],
56+
"selectData": [Function],
57+
"selectorId": null,
58+
"send": [Function],
59+
"sendInit": [Function],
60+
"store": Object {
61+
"global": Object {
62+
"init": Object {},
63+
"over": Object {},
64+
},
65+
},
66+
"subscriber": [Function],
67+
}
68+
`
69+
);
70+
});
71+
72+
it(`should init ${whatSide(isPanel)} Store`, () => {
73+
const store = new ChannelStore(configWith({ isPanel }));
74+
expect(store).toHaveProperty('isPanel', isPanel);
75+
});
76+
77+
it(`should connect to channel from ${whatSide(isPanel)}`, () => {
78+
const store = new ChannelStore(configWith({ isPanel }));
79+
store.channel.mock().reset();
80+
const onConnected = jest.fn();
81+
store.onConnected(onConnected);
82+
store.connect();
83+
expect(onConnected).toHaveBeenCalledTimes(1);
84+
expect(store.channel.mock().onEvent).toHaveBeenNthCalledWith(
85+
1,
86+
isPanel
87+
? [store.EVENT_ID_INIT, store.onInitChannel]
88+
: [store.EVENT_ID_BACK, store.onDataChannel]
89+
);
90+
if (isPanel) {
91+
expect(store.channel.mock().onEvent).toHaveBeenNthCalledWith(2, [
92+
store.EVENT_ID_DATA,
93+
store.onDataChannel,
94+
]);
95+
}
96+
});
97+
}
98+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const channel = {
2+
on: (event, cb) => console.log('on', event),
3+
};
4+
5+
console.log('TCL: channel', channel);
6+
7+
const addons = {
8+
getChannel: () => channel,
9+
};
10+
11+
export default addons;

0 commit comments

Comments
 (0)