Skip to content

Commit

Permalink
Update examples in README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Steam committed Mar 9, 2020
1 parent 1f98869 commit c5af655
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ project adheres to
### Improved

- Move from jest.Mock to jest.MockedFunction
- Update README.md

### Added

* Add lastError example to demo test and README.md

## [**0.3.0**] - 2020-02-13

Expand Down
70 changes: 68 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ A complete mock of the Chrome API for Chrome extensions, for use
with Jest.

The `chrome` object is based on schemas from the Chromium
project.
project, with thanks to
[`sinon-chrome`](https://github.com/acvetkov/sinon-chrome) for
compiling the schemas.

TypeScript support is built in. Each function and event is typed
using the types from [`@types/chrome`](https://www.npmjs.com/package/@types/chrome).
using the types from
[`@types/chrome`](https://www.npmjs.com/package/@types/chrome).

## Usage

Expand All @@ -29,6 +32,69 @@ test('chrome api functions', () => {
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' }

const lastErrorSpy = jest.fn()
const callbackSpy = jest.fn(() => {
if (chrome.runtime.lastError) {
lastErrorSpy(chrome.runtime.lastError.message)
}
})

const lastErrorMessage = 'this is an error'
const lastErrorGetter = jest.fn(() => lastErrorMessage)
const lastError = {
get message() {
return lastErrorGetter()
},
}

chrome.runtime.sendMessage.mockImplementation(
(message, callback) => {
chrome.runtime.lastError = lastError

callback(response)

delete chrome.runtime.lastError
},
)

chrome.runtime.sendMessage(message, callbackSpy)

expect(chrome.runtime.sendMessage).toBeCalledWith(
message,
callbackSpy,
)

expect(callbackSpy).toBeCalledWith(response)
expect(lastErrorGetter).toBeCalled()
expect(lastErrorSpy).toBeCalledWith(lastErrorMessage)

expect(chrome.runtime.lastError).toBeUndefined()
})

test('chrome api events', () => {
const listenerSpy = jest.fn()
const sendResponseSpy = jest.fn()
Expand Down
63 changes: 63 additions & 0 deletions src/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,69 @@ test('chrome api functions', () => {
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' }

const lastErrorSpy = jest.fn()
const callbackSpy = jest.fn(() => {
if (chrome.runtime.lastError) {
lastErrorSpy(chrome.runtime.lastError.message)
}
})

const lastErrorMessage = 'this is an error'
const lastErrorGetter = jest.fn(() => lastErrorMessage)
const lastError = {
get message() {
return lastErrorGetter()
},
}

chrome.runtime.sendMessage.mockImplementation(
(message, callback) => {
chrome.runtime.lastError = lastError

callback(response)

delete chrome.runtime.lastError
},
)

chrome.runtime.sendMessage(message, callbackSpy)

expect(chrome.runtime.sendMessage).toBeCalledWith(
message,
callbackSpy,
)

expect(callbackSpy).toBeCalledWith(response)
expect(lastErrorGetter).toBeCalled()
expect(lastErrorSpy).toBeCalledWith(lastErrorMessage)

expect(chrome.runtime.lastError).toBeUndefined()
})

test('chrome api events', () => {
const listenerSpy = jest.fn()
const sendResponseSpy = jest.fn()
Expand Down

0 comments on commit c5af655

Please sign in to comment.