-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathlogic.test.ts
202 lines (180 loc) · 7.17 KB
/
logic.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Logic, { State, Event } from './logic'
import {
insertBackgroundFunctionTab,
makeSingleDeviceUILogicTestFactory,
UILogicTestDevice,
} from 'src/tests/ui-logic-tests'
import { createSyncSettingsStore } from 'src/sync-settings/util'
const TEST_EXT_PAGE = 'chrome-extension://testest.html'
const DEFAULT_PAGE = 'https://getmemex.com'
async function setupTest(
{ backgroundModules, browserAPIs, createElement }: UILogicTestDevice,
args?: {
currentPageUrl?: string
openPage?: (url: string) => void
},
) {
const syncSettingsStore = createSyncSettingsStore({
syncSettingsBG: backgroundModules.syncSettings,
})
const _logic = new Logic({
tabsAPI: {
create: browserAPIs.tabs.create,
query: () => [{ url: args?.currentPageUrl ?? DEFAULT_PAGE }] as any,
update: args?.openPage
? (((tabId, { url }) => args.openPage(url)) as any)
: () => [{}] as any,
},
runtimeAPI: {
getURL: () => TEST_EXT_PAGE,
},
syncSettings: syncSettingsStore,
pageIndexingBG: insertBackgroundFunctionTab(
backgroundModules.pages.remoteFunctions,
) as any,
authBG: backgroundModules.auth.remoteFunctions,
pdfIntegrationBG: backgroundModules.pdfBg.remoteFunctions,
customListsBG: backgroundModules.customLists.remoteFunctions,
analyticsBG: backgroundModules.analyticsBG,
extensionAPI: { isAllowedFileSchemeAccess: async () => true },
annotationsBG: backgroundModules.directLinking.remoteFunctions,
})
const logic = createElement<State, Event>(_logic)
return { _logic, logic, syncSettingsStore }
}
describe('Popup UI logic', () => {
const it = makeSingleDeviceUILogicTestFactory()
it(
'should check whether tags migration is done to signal showing of tags UI on init',
async ({ device }) => {
const currentPageUrl = 'https://memex.memex'
const { logic, syncSettingsStore } = await setupTest(device, {
currentPageUrl,
})
await syncSettingsStore.extension.set(
'areTagsMigratedToSpaces',
false,
)
expect(logic.state.shouldShowTagsUIs).toBe(false)
await logic.init()
expect(logic.state.shouldShowTagsUIs).toBe(true)
await syncSettingsStore.extension.set(
'areTagsMigratedToSpaces',
true,
)
expect(logic.state.shouldShowTagsUIs).toBe(true)
await logic.init()
expect(logic.state.shouldShowTagsUIs).toBe(false)
},
{ shouldSkip: true },
)
it(
'should hydrate PDF reader enabled state based on sync settings+current URL on init',
async ({ device }) => {
const currentPageUrl = 'https://memex.memex'
const { logic, syncSettingsStore } = await setupTest(device, {
currentPageUrl,
})
await syncSettingsStore.pdfIntegration.set('shouldAutoOpen', true)
expect(logic.state.isPDFReaderEnabled).toBe(false)
expect(logic.state.currentTabFullUrl).toBe('')
expect(logic.state.loadState).toBe('pristine')
await logic.init()
expect(logic.state.isPDFReaderEnabled).toBe(true)
expect(logic.state.currentTabFullUrl).toBe(currentPageUrl)
expect(logic.state.loadState).toBe('success')
await syncSettingsStore.pdfIntegration.set('shouldAutoOpen', false)
expect(logic.state.isPDFReaderEnabled).toBe(true)
await logic.init()
expect(logic.state.isPDFReaderEnabled).toBe(false)
},
{ shouldSkip: true },
)
it(
'should hydrate page lists on init',
async ({ device }) => {
const currentPageUrl = 'https://memex.memex'
const { logic } = await setupTest(device, {
currentPageUrl,
})
const [
listIdA,
listIdB,
] = await device.backgroundModules.customLists.createCustomLists({
names: ['test a', 'test b'],
})
expect(logic.state.pageListIds).toEqual([])
await logic.init()
expect(logic.state.pageListIds).toEqual([])
await device.backgroundModules.customLists.insertPageToList({
url: currentPageUrl,
id: listIdA,
skipPageIndexing: true,
})
await logic.init()
expect(logic.state.pageListIds).toEqual([listIdA])
await device.backgroundModules.customLists.insertPageToList({
url: currentPageUrl,
id: listIdB,
skipPageIndexing: true,
})
await logic.init()
expect(logic.state.pageListIds).toEqual([listIdA, listIdB])
},
{ shouldSkip: true },
)
it('should be able to toggle PDF reader enabled state', async ({
device,
}) => {
const { logic, syncSettingsStore } = await setupTest(device)
expect(logic.state.isPDFReaderEnabled).toBe(false)
expect(
await syncSettingsStore.pdfIntegration.get('shouldAutoOpen'),
).toBe(null)
await logic.processEvent('togglePDFReaderEnabled', null)
expect(logic.state.isPDFReaderEnabled).toBe(true)
expect(
await syncSettingsStore.pdfIntegration.get('shouldAutoOpen'),
).toBe(true)
await logic.processEvent('togglePDFReaderEnabled', null)
expect(logic.state.isPDFReaderEnabled).toBe(false)
expect(
await syncSettingsStore.pdfIntegration.get('shouldAutoOpen'),
).toBe(false)
})
it(
'should be able to toggle the Memex PDF viewer ',
async ({ device }) => {
let openedPage: string
const { logic } = await setupTest(device, {
openPage: (url) => {
openedPage = url
},
})
await logic.init()
expect(openedPage).toBe(undefined)
await logic.processEvent('togglePDFReader', null)
expect(openedPage).toBe(TEST_EXT_PAGE + '?file=' + DEFAULT_PAGE)
await logic.processEvent('togglePDFReader', null)
expect(openedPage).toBe(DEFAULT_PAGE)
},
{ shouldSkip: true },
)
it(
'should be able to add and remove page lists',
async ({ device }) => {
const { logic } = await setupTest(device)
await logic.init()
expect(logic.state.pageListIds).toEqual([])
await logic.processEvent('addPageList', { listId: 1 })
expect(logic.state.pageListIds).toEqual([1])
await logic.processEvent('addPageList', { listId: 2 })
expect(logic.state.pageListIds).toEqual([1, 2])
await logic.processEvent('delPageList', { listId: 2 })
expect(logic.state.pageListIds).toEqual([1])
await logic.processEvent('delPageList', { listId: 1 })
expect(logic.state.pageListIds).toEqual([])
},
{ shouldSkip: true },
)
})