Skip to content

Commit

Permalink
Fix build config
Browse files Browse the repository at this point in the history
Add lib folder tests
  • Loading branch information
Jack Steam committed May 29, 2020
1 parent f43b5f5 commit 14b2ce6
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 9 deletions.
6 changes: 0 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ export default [
sourcemap: true,
},
],
external: [
'chrome-promise',
'@bumble/chrome-rxjs',
'rxjs',
'rxjs/operators',
],
plugins: [
typescript({
tsconfig: './tsconfig.base.json',
Expand Down
103 changes: 103 additions & 0 deletions tests/demo-cjs.test.js
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()
})
101 changes: 101 additions & 0 deletions tests/demo-esm.test.js
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()
})
6 changes: 3 additions & 3 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["es2017", "dom", "esnext.array"],
"module": "CommonJS",
"module": "ES2015",
"moduleResolution": "node",
"noImplicitReturns": true,
"noUnusedLocals": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"exclude": ["lib", "playground"],
"exclude": ["lib", "playground", "tests"],
"typeAcquisition": {
"enable": true
}
Expand Down

0 comments on commit 14b2ce6

Please sign in to comment.