-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-chrome.test.ts
More file actions
156 lines (130 loc) · 5.21 KB
/
monitor-chrome.test.ts
File metadata and controls
156 lines (130 loc) · 5.21 KB
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
import { expect } from 'chai'
import { createMocks } from './mocks'
import { runBackground } from './steps/run-background'
describe('monitorChrome', () => {
const mocks = createMocks()
runBackground(mocks, { alreadyAuthenticated: true })
describe('chrome.tabs.onCreated', () => {
it("adds the newly created tab to the store's state", () => {
expect(mocks.chrome.tabs.onCreated.addListener).to.have.callCount(1)
const [callback] = mocks.chrome.tabs.onCreated.addListener.firstCall.args
callback({
id: 18,
windowId: 1,
active: false,
url: 'https://www.new.com/abc/def',
})
const state = mocks.getState()
const tab = state.tabs.get(18)!
expect(tab.id).to.equal(18)
expect(tab.windowId).to.equal(1)
expect(tab.parsedUrl).to.eql({
host: 'new.com',
hostWithoutSubdomain: 'new.com',
subdomain: undefined,
firstPath: 'abc',
fullWithFirstPath: 'new.com/abc',
fullWithoutQuery: 'new.com/abc/def',
})
})
})
describe('chrome.tabs.onRemoved', () => {
it("removes a tab from the store's state", () => {
const [callback] = mocks.chrome.tabs.onRemoved.addListener.firstCall.args
const toRemoveId = 11
callback(toRemoveId)
expect(mocks.getState().tabs.get(toRemoveId)).to.equal(undefined)
})
})
describe('chrome.tabs.onUpdated', () => {
it("updates the appropriate tab change to store's state", () => {
const [callback] = mocks.chrome.tabs.onUpdated.addListener.firstCall.args
const changeInfo = {
id: 16,
url: 'https://foo.changeinfo.com/abc/def',
}
callback(changeInfo.id, changeInfo)
const state = mocks.getState()
const tab = state.tabs.get(changeInfo.id)!
expect(tab.id).to.equal(changeInfo.id)
expect(tab.parsedUrl).to.eql({
host: 'foo.changeinfo.com',
hostWithoutSubdomain: 'changeinfo.com',
subdomain: 'foo',
firstPath: 'abc',
fullWithFirstPath: 'foo.changeinfo.com/abc',
fullWithoutQuery: 'foo.changeinfo.com/abc/def',
})
})
})
describe('chrome.tabs.onAttached', () => {
it("attaches a tab to another window and updates the store's state", () => {
const [callback] = mocks.chrome.tabs.onAttached.addListener.firstCall.args
const attachInfo = {
id: 15,
windowId: 2,
newWindowId: 3,
}
callback(attachInfo.id, attachInfo)
const state = mocks.getState()
const tab = state.tabs.get(attachInfo.id)!
expect(tab.id).to.equal(attachInfo.id)
expect(tab.windowId).to.equal(attachInfo.newWindowId)
})
})
describe('chrome.tabs.onActivated', () => {
it('sets the specified tab as active for a given window and sets any other tabs of that window to be inactive', () => {
const [callback] = mocks.chrome.tabs.onActivated.addListener.firstCall.args
const activeInfo: chrome.tabs.TabActiveInfo = { tabId: 13, windowId: 1 }
callback(activeInfo)
const { tabs } = mocks.getState()
expect(tabs.get(13)).to.have.property('active', true)
expect(tabs.get(12)).to.have.property('active', false)
})
it('has no effect for active tabs of other windows', () => {
const { tabs } = mocks.getState()
expect(tabs.get(17)).to.have.property('active', true)
})
})
describe('chrome.tabs.onReplaced', () => {
it("replaces a tab (create and remove) and updates store's state", () => {
const existingTab = mocks.getState().tabs.get(18)!
const newTabId = 11
const [callback] = mocks.chrome.tabs.onReplaced.addListener.firstCall.args
callback(newTabId, existingTab.id)
const { tabs } = mocks.getState()
expect(tabs.get(existingTab.id)).to.equal(undefined)
expect(tabs.get(newTabId)).to.have.property('id', newTabId)
expect(tabs.get(newTabId)).to.have.property('windowId', existingTab.windowId)
expect(tabs.get(newTabId)).to.have.property('active', existingTab.active)
expect(tabs.get(newTabId)).to.have.property('parsedUrl').that.eql(existingTab.parsedUrl)
})
})
describe('chrome.windows.onCreated', () => {
it("adds a newly created window to store's state", () => {
const [callback] = mocks.chrome.windows.onCreated.addListener.firstCall.args
const win = { id: 4, focused: true }
callback(win)
const state = mocks.getState()
expect(state.focusedWindowId).to.eql(win.id)
})
})
describe('chrome.windows.onRemoved', () => {
it("deletes a removed window from store's state", () => {
const [callback] = mocks.chrome.windows.onRemoved.addListener.firstCall.args
const removedWindow = { id: 4, focused: true }
callback(removedWindow.id)
const state = mocks.getState()
expect(state.focusedWindowId).not.eql(removedWindow.id)
})
})
describe('chrome.windows.onFocusChanged', () => {
it("changes windows focus status in store's state", () => {
const [callback] = mocks.chrome.windows.onFocusChanged.addListener.firstCall.args
const changedWindow = { id: 3, focused: true }
callback(changedWindow.id)
const state = mocks.getState()
expect(state.focusedWindowId).to.equal(changedWindow.id)
})
})
})