forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebextensionRPC.test.ts
94 lines (83 loc) · 3.54 KB
/
webextensionRPC.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
import * as expect from 'expect'
import { remoteFunction, fakeRemoteFunction } from './webextensionRPC'
describe('remoteFunction', () => {
const jest =
(typeof window !== 'undefined' ? window : global)['jest'] ||
require('jest-mock')
beforeEach(() => {
if (global) {
global['window'] = { browser: {} }
}
window['browser'].runtime = {
sendMessage: jest.fn(() => Promise.resolve()),
}
window['browser'].tabs = {
sendMessage: jest.fn(() => Promise.resolve()),
}
})
it('should create a function', () => {
const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
expect(remoteFunc.name).toBe('remoteFunc_RPC')
expect(typeof remoteFunc).toBe('function')
})
// test.skip('should throw an error when unable to sendMessage', async () => {
// const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
// window['browser'].tabs.sendMessage.mockImplementationOnce(() => {
// throw new Error()
// })
// await expect(remoteFunc()).rejects.toMatchObject({
// message: `Got no response when trying to call 'remoteFunc'. Did you enable RPC in the tab's content script?`,
// })
// })
it('should call the browser.tabs function when tabId is given', async () => {
const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
try {
await remoteFunc()
} catch (e) {
// Do nothing
}
expect(window['browser'].tabs.sendMessage).toHaveBeenCalledTimes(1)
expect(window['browser'].runtime.sendMessage).toHaveBeenCalledTimes(0)
})
it('should call the browser.runtime function when tabId is undefined', async () => {
const remoteFunc = remoteFunction('remoteFunc')
try {
await remoteFunc()
} catch (e) {
// Do nothing
}
expect(window['browser'].tabs.sendMessage).toHaveBeenCalledTimes(0)
expect(window['browser'].runtime.sendMessage).toHaveBeenCalledTimes(1)
})
// test.skip('should throw an error if response does not contain RPC token', async () => {
// const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
// await expect(remoteFunc()).rejects.toMatchObject({
// message: 'RPC got a response from an interfering listener.',
// })
// })
// test.skip('should throw an error if the response contains an error message', async () => {
// window['browser'].tabs.sendMessage.mockReturnValueOnce({
// __RPC_RESPONSE__: '__RPC_RESPONSE__',
// errorMessage: 'Remote function error',
// })
// const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
// await expect(remoteFunc()).rejects.toMatchObject({
// message: 'Remote function error',
// })
// })
it('should return the value contained in the response', async () => {
window['browser'].tabs.sendMessage.mockReturnValueOnce({
__RPC_RESPONSE__: '__RPC_RESPONSE__',
returnValue: 'Remote function return value',
})
const remoteFunc = remoteFunction('remoteFunc', { tabId: 1 })
const returnVal = await remoteFunc()
expect(returnVal).toBe('Remote function return value')
})
it('should create fake remote functions', async () => {
const remoteFunc = fakeRemoteFunction({
foo: (a, b) => a + b + 5,
})
expect(await remoteFunc('foo')(1, 2)).toEqual(8)
})
})