|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +import { createBus, createReduxMiddleware } from 'suber' |
| 21 | +import configureMockStore, { MockStoreEnhanced } from 'redux-mock-store' |
| 22 | +import { |
| 23 | + PREVIEW_EVENT, |
| 24 | + trackNavigateToPreview, |
| 25 | + trackPageLoad |
| 26 | +} from './previewDuck' |
| 27 | + |
| 28 | +describe('previewDuck tests', () => { |
| 29 | + let store: MockStoreEnhanced<unknown, unknown> |
| 30 | + const bus = createBus() |
| 31 | + const mockStore = configureMockStore([createReduxMiddleware(bus)]) |
| 32 | + |
| 33 | + beforeAll(() => { |
| 34 | + store = mockStore() |
| 35 | + }) |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + bus.reset() |
| 39 | + store.clearActions() |
| 40 | + localStorage.clear() |
| 41 | + }) |
| 42 | + |
| 43 | + test('trackNavigateToPreview sends a PREVIEW_EVENT', done => { |
| 44 | + const action = trackNavigateToPreview() |
| 45 | + |
| 46 | + bus.take(PREVIEW_EVENT, () => { |
| 47 | + // Then |
| 48 | + const [action] = store.getActions() |
| 49 | + expect(action).toEqual({ |
| 50 | + type: PREVIEW_EVENT, |
| 51 | + label: 'PREVIEW_UI_SWITCH', |
| 52 | + data: { |
| 53 | + switchedTo: 'preview', |
| 54 | + timeSinceLastSwitch: null |
| 55 | + } |
| 56 | + }) |
| 57 | + done() |
| 58 | + }) |
| 59 | + |
| 60 | + // When |
| 61 | + store.dispatch(action) |
| 62 | + }) |
| 63 | + |
| 64 | + test('trackNavigateToPreview sets hasTriedPreviewUI', done => { |
| 65 | + localStorage.setItem('hasTriedPreviewUI', 'false') |
| 66 | + const action = trackNavigateToPreview() |
| 67 | + |
| 68 | + bus.take(PREVIEW_EVENT, () => { |
| 69 | + // Then |
| 70 | + const hasTriedPreviewUI = localStorage.getItem('hasTriedPreviewUI') |
| 71 | + expect(hasTriedPreviewUI).toBe('true') |
| 72 | + done() |
| 73 | + }) |
| 74 | + |
| 75 | + // When |
| 76 | + store.dispatch(action) |
| 77 | + }) |
| 78 | + |
| 79 | + test('trackNavigateToPreview sends correct timeSinceLastSwitch when timeSinceLastSwitchMs is unset', done => { |
| 80 | + const action = trackNavigateToPreview() |
| 81 | + |
| 82 | + bus.take(PREVIEW_EVENT, () => { |
| 83 | + // Then |
| 84 | + const [action] = store.getActions() |
| 85 | + expect(action.data.timeSinceLastSwitch).toBeNull() |
| 86 | + done() |
| 87 | + }) |
| 88 | + |
| 89 | + // When |
| 90 | + store.dispatch(action) |
| 91 | + }) |
| 92 | + |
| 93 | + test('trackNavigateToPreview sends correct timeSinceLastSwitch when timeSinceLastSwitchMs has been set', done => { |
| 94 | + localStorage.setItem('timeSinceLastSwitchMs', Date.now().toString()) |
| 95 | + const action = trackNavigateToPreview() |
| 96 | + |
| 97 | + bus.take(PREVIEW_EVENT, () => { |
| 98 | + // Then |
| 99 | + const [action] = store.getActions() |
| 100 | + expect(action.data.timeSinceLastSwitch).not.toBeNull() |
| 101 | + done() |
| 102 | + }) |
| 103 | + |
| 104 | + // When |
| 105 | + store.dispatch(action) |
| 106 | + }) |
| 107 | + |
| 108 | + test('trackPageLoad sends a PREVIEW_EVENT', done => { |
| 109 | + const action = trackPageLoad() |
| 110 | + |
| 111 | + bus.take(PREVIEW_EVENT, () => { |
| 112 | + // Then |
| 113 | + const [action] = store.getActions() |
| 114 | + expect(action).toEqual({ |
| 115 | + type: PREVIEW_EVENT, |
| 116 | + label: 'PREVIEW_PAGE_LOAD', |
| 117 | + data: { previewUI: false, hasTriedPreviewUI: false } |
| 118 | + }) |
| 119 | + done() |
| 120 | + }) |
| 121 | + |
| 122 | + // When |
| 123 | + store.dispatch(action) |
| 124 | + }) |
| 125 | + |
| 126 | + test('trackPageLoad sends correct hasTriedPreviewUI value when flag is unset', done => { |
| 127 | + const action = trackPageLoad() |
| 128 | + |
| 129 | + bus.take(PREVIEW_EVENT, () => { |
| 130 | + // Then |
| 131 | + const [action] = store.getActions() |
| 132 | + expect(action.data.hasTriedPreviewUI).toBeFalsy() |
| 133 | + done() |
| 134 | + }) |
| 135 | + |
| 136 | + // When |
| 137 | + store.dispatch(action) |
| 138 | + }) |
| 139 | + |
| 140 | + test('trackPageLoad sends correct hasTriedPreviewUI value when flag is set', done => { |
| 141 | + localStorage.setItem('hasTriedPreviewUI', 'true') |
| 142 | + const action = trackPageLoad() |
| 143 | + |
| 144 | + bus.take(PREVIEW_EVENT, () => { |
| 145 | + // Then |
| 146 | + const [action] = store.getActions() |
| 147 | + expect(action.data.hasTriedPreviewUI).toBeTruthy() |
| 148 | + done() |
| 149 | + }) |
| 150 | + |
| 151 | + // When |
| 152 | + store.dispatch(action) |
| 153 | + }) |
| 154 | +}) |
0 commit comments