-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lib folder tests
- Loading branch information
Jack Steam
committed
May 29, 2020
1 parent
f43b5f5
commit 14b2ce6
Showing
4 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const { chrome } = require('../lib/index.cjs') | ||
|
||
test('chrome api events', () => { | ||
const listenerSpy = jest.fn() | ||
const sendResponseSpy = jest.fn() | ||
|
||
chrome.runtime.onMessage.addListener(listenerSpy) | ||
|
||
expect(listenerSpy).not.toBeCalled() | ||
expect(chrome.runtime.onMessage.hasListeners()).toBe(true) | ||
|
||
chrome.runtime.onMessage.callListeners( | ||
{ greeting: 'hello' }, // message | ||
{}, // MessageSender object | ||
sendResponseSpy, // SendResponse function | ||
) | ||
|
||
expect(listenerSpy).toBeCalledWith( | ||
{ greeting: 'hello' }, | ||
{}, | ||
sendResponseSpy, | ||
) | ||
expect(sendResponseSpy).not.toBeCalled() | ||
}) | ||
|
||
test('chrome api functions', () => { | ||
const manifest = { | ||
name: 'my chrome extension', | ||
manifest_version: 2, | ||
version: '1.0.0', | ||
} | ||
|
||
chrome.runtime.getManifest.mockImplementation( | ||
() => manifest, | ||
) | ||
|
||
expect(chrome.runtime.getManifest()).toEqual(manifest) | ||
expect(chrome.runtime.getManifest).toBeCalled() | ||
}) | ||
|
||
test('chrome api functions with callback', () => { | ||
const message = { greeting: 'hello?' } | ||
const response = { greeting: 'here I am' } | ||
const callbackSpy = jest.fn() | ||
|
||
chrome.runtime.sendMessage.mockImplementation( | ||
(message, callback) => { | ||
callback(response) | ||
}, | ||
) | ||
|
||
chrome.runtime.sendMessage(message, callbackSpy) | ||
|
||
expect(chrome.runtime.sendMessage).toBeCalledWith( | ||
message, | ||
callbackSpy, | ||
) | ||
expect(callbackSpy).toBeCalledWith(response) | ||
}) | ||
|
||
test('chrome api functions with lastError', () => { | ||
const message = { greeting: 'hello?' } | ||
const response = { greeting: 'here I am' } | ||
|
||
// lastError setup | ||
const lastErrorMessage = 'this is an error' | ||
const lastErrorGetter = jest.fn(() => lastErrorMessage) | ||
const lastError = { | ||
get message() { | ||
return lastErrorGetter() | ||
}, | ||
} | ||
|
||
// mock implementation | ||
chrome.runtime.sendMessage.mockImplementation( | ||
(message, callback) => { | ||
chrome.runtime.lastError = lastError | ||
|
||
callback(response) | ||
|
||
// lastError is undefined outside of a callback | ||
delete chrome.runtime.lastError | ||
}, | ||
) | ||
|
||
// callback implementation | ||
const lastErrorSpy = jest.fn() | ||
const callbackSpy = jest.fn(() => { | ||
if (chrome.runtime.lastError) { | ||
lastErrorSpy(chrome.runtime.lastError.message) | ||
} | ||
}) | ||
|
||
// send a message | ||
chrome.runtime.sendMessage(message, callbackSpy) | ||
|
||
expect(callbackSpy).toBeCalledWith(response) | ||
expect(lastErrorGetter).toBeCalled() | ||
expect(lastErrorSpy).toBeCalledWith(lastErrorMessage) | ||
|
||
// lastError has been cleared | ||
expect(chrome.runtime.lastError).toBeUndefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { chrome } from '../lib/index.esm' | ||
|
||
test('chrome api events', () => { | ||
const listenerSpy = jest.fn() | ||
const sendResponseSpy = jest.fn() | ||
|
||
chrome.runtime.onMessage.addListener(listenerSpy) | ||
|
||
expect(listenerSpy).not.toBeCalled() | ||
expect(chrome.runtime.onMessage.hasListeners()).toBe(true) | ||
|
||
chrome.runtime.onMessage.callListeners( | ||
{ greeting: 'hello' }, // message | ||
{}, // MessageSender object | ||
sendResponseSpy, // SendResponse function | ||
) | ||
|
||
expect(listenerSpy).toBeCalledWith( | ||
{ greeting: 'hello' }, | ||
{}, | ||
sendResponseSpy, | ||
) | ||
expect(sendResponseSpy).not.toBeCalled() | ||
}) | ||
|
||
test('chrome api functions', () => { | ||
const manifest = { | ||
name: 'my chrome extension', | ||
manifest_version: 2, | ||
version: '1.0.0', | ||
} | ||
|
||
chrome.runtime.getManifest.mockImplementation(() => manifest) | ||
|
||
expect(chrome.runtime.getManifest()).toEqual(manifest) | ||
expect(chrome.runtime.getManifest).toBeCalled() | ||
}) | ||
|
||
test('chrome api functions with callback', () => { | ||
const message = { greeting: 'hello?' } | ||
const response = { greeting: 'here I am' } | ||
const callbackSpy = jest.fn() | ||
|
||
chrome.runtime.sendMessage.mockImplementation( | ||
(message, callback) => { | ||
callback(response) | ||
}, | ||
) | ||
|
||
chrome.runtime.sendMessage(message, callbackSpy) | ||
|
||
expect(chrome.runtime.sendMessage).toBeCalledWith( | ||
message, | ||
callbackSpy, | ||
) | ||
expect(callbackSpy).toBeCalledWith(response) | ||
}) | ||
|
||
test('chrome api functions with lastError', () => { | ||
const message = { greeting: 'hello?' } | ||
const response = { greeting: 'here I am' } | ||
|
||
// lastError setup | ||
const lastErrorMessage = 'this is an error' | ||
const lastErrorGetter = jest.fn(() => lastErrorMessage) | ||
const lastError = { | ||
get message() { | ||
return lastErrorGetter() | ||
}, | ||
} | ||
|
||
// mock implementation | ||
chrome.runtime.sendMessage.mockImplementation( | ||
(message, callback) => { | ||
chrome.runtime.lastError = lastError | ||
|
||
callback(response) | ||
|
||
// lastError is undefined outside of a callback | ||
delete chrome.runtime.lastError | ||
}, | ||
) | ||
|
||
// callback implementation | ||
const lastErrorSpy = jest.fn() | ||
const callbackSpy = jest.fn(() => { | ||
if (chrome.runtime.lastError) { | ||
lastErrorSpy(chrome.runtime.lastError.message) | ||
} | ||
}) | ||
|
||
// send a message | ||
chrome.runtime.sendMessage(message, callbackSpy) | ||
|
||
expect(callbackSpy).toBeCalledWith(response) | ||
expect(lastErrorGetter).toBeCalled() | ||
expect(lastErrorSpy).toBeCalledWith(lastErrorMessage) | ||
|
||
// lastError has been cleared | ||
expect(chrome.runtime.lastError).toBeUndefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters