forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab-events.test.js
154 lines (139 loc) · 4.94 KB
/
tab-events.test.js
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
/* eslint-env jest */
import {
whenPageDOMLoaded,
whenPageLoadComplete,
whenTabActive,
} from './tab-events'
import * as eventToPromise from './event-to-promise'
describe('whenPageDOMLoaded', () => {
const tabId = 1
beforeEach(() => {
browser.tabs = {}
browser.webNavigation = {
onCommitted: jest.fn(),
}
eventToPromise.default = jest.fn().mockReturnValue(Promise.resolve())
})
test('should execute script and resolve promise if script executes', async () => {
browser.tabs = {
executeScript: jest.fn().mockReturnValue(Promise.resolve()),
}
await whenPageDOMLoaded({ tabId })
expect(browser.tabs.executeScript).toHaveBeenCalledWith(tabId, {
code: 'undefined',
runAt: 'document_end',
})
})
test('should reject the promise if the script is not executed', async () => {
expect.assertions(1)
browser.tabs = {
executeScript: jest
.fn()
.mockReturnValue(
Promise.reject(new Error('Script unable to execute')),
),
}
try {
await whenPageDOMLoaded({ tabId })
} catch (err) {
expect(err.message).toBe('Script unable to execute')
}
})
test.skip('should reject the promise if tab is changed', async () => {
expect.assertions(1)
browser.tabs = {
executeScript: jest
.fn()
.mockReturnValue(new Promise((resolve, reject) => {})),
}
eventToPromise.default = jest
.fn()
.mockReturnValue(Promise.reject(new Error('Tab was changed')))
await expect(whenPageDOMLoaded({ tabId })).rejects.toMatchObject({
message: 'Tab was changed',
})
})
})
describe('whenPageLoadComplete', () => {
const tabId = 1
beforeEach(() => {
eventToPromise.default = jest.fn().mockReturnValue(Promise.resolve())
browser.webNavigation = {
onCommitted: jest.fn(),
}
})
test('should return directly if the tab status is complete', async () => {
browser.tabs = {
get: jest.fn().mockReturnValueOnce({
status: 'complete',
}),
}
await whenPageLoadComplete({ tabId })
expect(browser.tabs.get).toHaveBeenCalledWith(tabId)
expect(eventToPromise.default).not.toHaveBeenCalled()
})
test('should run eventToPromise and resolve if its Promise resolves', async () => {
browser.tabs = {
get: jest.fn().mockReturnValueOnce({
status: 'loading',
}),
}
// Add a 'shibboleth' to be able to check that *this* Promise was returned & resolved.
eventToPromise.default.mockReturnValueOnce(
Promise.resolve('shibboleth'),
)
await expect(whenPageLoadComplete({ tabId })).resolves.toBe(
'shibboleth',
)
})
test.skip('should run eventToPromise and reject if its Promise rejects', async () => {
browser.tabs = {
get: jest.fn().mockReturnValueOnce({
status: 'loading',
}),
}
eventToPromise.default = jest
.fn()
.mockReturnValue(Promise.reject(new Error('Tab was changed')))
await expect(whenPageLoadComplete({ tabId })).rejects.toMatchObject({
message: 'Tab was changed',
})
})
})
describe('whenTabActive', () => {
beforeEach(() => {
eventToPromise.default = jest.fn().mockReturnValue(Promise.resolve())
browser.webNavigation = {
onCommitted: jest.fn(),
}
})
test('should return directly if the tab is already active', async () => {
browser.tabs = {
query: jest.fn().mockReturnValueOnce([{ id: 1 }]),
}
await whenTabActive({ tabId: 1 })
expect(browser.tabs.query).toHaveBeenCalledWith({ active: true })
expect(eventToPromise.default).not.toHaveBeenCalled()
})
test('should run eventToPromise and resolve if its Promise resolves', async () => {
browser.tabs = {
query: jest.fn().mockReturnValueOnce([{ id: 2 }]),
}
// Add a 'shibboleth' to be able to check that *this* Promise was returned & resolved.
eventToPromise.default.mockReturnValueOnce(
Promise.resolve('shibboleth'),
)
await expect(whenTabActive({ tabId: 1 })).resolves.toBe('shibboleth')
})
test.skip('should run eventToPromise and reject if its Promise rejects', async () => {
browser.tabs = {
query: jest.fn().mockReturnValueOnce([{ id: 2 }]),
}
eventToPromise.default = jest
.fn()
.mockReturnValue(Promise.reject(new Error('Tab was changed')))
await expect(whenTabActive({ tabId: 1 })).rejects.toMatchObject({
message: 'Tab was changed',
})
})
})