|
| 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 | +); |
0 commit comments