|
| 1 | +import findLast from 'lodash-es/findLast'; |
| 2 | +import get from 'lodash-es/get'; |
| 3 | +import mixpanel from 'mixpanel-browser'; |
| 4 | +import uuid from 'uuid/v4'; |
| 5 | + |
| 6 | +import {rehydrateProject} from '../../clients/localStorage'; |
| 7 | +import createApplicationStore from '../../createApplicationStore'; |
| 8 | +import config from '../../config'; |
| 9 | + |
| 10 | +import {applicationLoaded} from '../../actions'; |
| 11 | + |
| 12 | +import {firebaseProjectFactory} from '../../../__factories__/data/firebase'; |
| 13 | + |
| 14 | +import i18next from 'i18next'; |
| 15 | + |
| 16 | +import init from '..'; |
| 17 | + |
| 18 | +jest.mock('../../clients/localStorage'); |
| 19 | +jest.mock('../../clients/firebase'); |
| 20 | +jest.mock('../../createApplicationStore'); |
| 21 | + |
| 22 | +describe('init()', () => { |
| 23 | + let dispatch; |
| 24 | + let store; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + dispatch = jest.fn(); |
| 28 | + store = {dispatch}; |
| 29 | + createApplicationStore.mockReturnValue(store); |
| 30 | + }); |
| 31 | + |
| 32 | + test('initializes i18next', () => { |
| 33 | + init(); |
| 34 | + expect(i18next.init).toHaveBeenCalledWith(expect.anything()); |
| 35 | + }); |
| 36 | + |
| 37 | + test('initializes mixpanel', async () => { |
| 38 | + init(); |
| 39 | + await new Promise(resolve => { |
| 40 | + mixpanel.init.mockImplementation(resolve); |
| 41 | + }); |
| 42 | + expect(mixpanel.init).toHaveBeenCalledWith(config.mixpanelToken); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('applicationLoaded', () => { |
| 46 | + let dispatchInvoked; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + dispatchInvoked = new Promise(resolve => { |
| 50 | + dispatch.mockImplementation(resolve); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + async function dispatchedAction() { |
| 55 | + await dispatchInvoked; |
| 56 | + return get( |
| 57 | + findLast( |
| 58 | + dispatch.mock.calls, |
| 59 | + ([action]) => action.type === applicationLoaded.toString(), |
| 60 | + ), |
| 61 | + ['0'], |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + test('dispatches action', async () => { |
| 66 | + init(); |
| 67 | + |
| 68 | + const action = await dispatchedAction(); |
| 69 | + expect(action).toBeDefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('reads gist ID from query and removes it', async () => { |
| 73 | + history.pushState(null, '', '/?gist=12345'); |
| 74 | + init(); |
| 75 | + const { |
| 76 | + payload: {gistId}, |
| 77 | + } = await dispatchedAction(); |
| 78 | + expect(gistId).toBe('12345'); |
| 79 | + expect(location.search).toBeFalsy(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('reads snapshot key from query and removes it', async () => { |
| 83 | + const snapshotKey = uuid(); |
| 84 | + history.pushState(null, '', `/?snapshot=${snapshotKey}`); |
| 85 | + init(); |
| 86 | + const {payload} = await dispatchedAction(); |
| 87 | + expect(payload.snapshotKey).toEqual(snapshotKey); |
| 88 | + }); |
| 89 | + |
| 90 | + test('reads experimental mode from query and leaves it', async () => { |
| 91 | + history.pushState(null, '', '/?experimental'); |
| 92 | + init(); |
| 93 | + const { |
| 94 | + payload: {isExperimental}, |
| 95 | + } = await dispatchedAction(); |
| 96 | + expect(isExperimental).toBe(true); |
| 97 | + expect(location.search).toBe('?experimental'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('rehydrates project', async () => { |
| 101 | + const project = firebaseProjectFactory.build(); |
| 102 | + rehydrateProject.mockReturnValue(project); |
| 103 | + init(); |
| 104 | + const { |
| 105 | + payload: {rehydratedProject}, |
| 106 | + } = await dispatchedAction(); |
| 107 | + expect(rehydratedProject).toBe(project); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments