Skip to content

Commit

Permalink
[tests] update tests for frameId
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksteamdev committed Dec 7, 2020
1 parent d58bc99 commit daffa03
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 21 deletions.
23 changes: 23 additions & 0 deletions src/ChromeMessageError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CoreMessage, CoreResponse } from './types'

export class ChromeMessageError extends Error {
coreMessage: CoreMessage | null
coreResponse: CoreResponse | null

constructor({
coreMessage = null,
coreResponse = null,
message = chrome.runtime?.lastError?.message ||
coreResponse?.payload.greeting ||
'chrome.runtime.lastError is undefined',
}: {
coreMessage?: CoreMessage | null
coreResponse?: CoreResponse | null
message?: string
}) {
super(message)

this.coreMessage = coreMessage
this.coreResponse = coreResponse
}
}
11 changes: 9 additions & 2 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,18 @@ export function getScope(scope: string) {
if (typeof _options.tabId === 'number') {
tabId = _options.tabId
}
let frameId: number | undefined
if (typeof _options.frameId === 'number') {
frameId = _options.frameId
}

if (async) {
return send<WrappedMessage, R>({ greeting, data }, { async, tabId })
return send<WrappedMessage, R>(
{ greeting, data },
{ async, tabId, frameId },
)
} else {
return send<WrappedMessage>({ greeting, data }, { tabId })
return send<WrappedMessage>({ greeting, data }, { tabId, frameId })
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/send.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChromeMessageError } from './ChromeMessageError'
import { CoreMessage, CoreResponse, SendOptions } from './types'

export const scopeSend = (scope: string) => (
Expand All @@ -21,7 +22,7 @@ export const scopeSend = (scope: string) => (
if (lastError && lastError.includes(noResponse)) {
resolve()
} else {
reject({ message: lastError })
reject(new ChromeMessageError({ coreMessage }))
}
} else {
if (response && !response.success) {
Expand Down Expand Up @@ -53,13 +54,15 @@ export const scopeAsyncSend = (scope: string) => (
scope,
}

const callback = (coreResponse: CoreResponse) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError)
} else if (coreResponse.success === false) {
reject(new Error(coreResponse.payload.greeting))
const callback = (coreResponse: CoreResponse | null) => {
if (
chrome.runtime.lastError ||
coreResponse === null ||
!coreResponse.success
) {
reject(new ChromeMessageError({ coreMessage, coreResponse }))
} else {
resolve(coreResponse.payload)
resolve(coreResponse!.payload)
}
}

Expand Down
38 changes: 35 additions & 3 deletions tests/units/send-async.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chrome } from 'jest-chrome'
import { ChromeMessageError } from '../../src/ChromeMessageError'
import { getScope } from '../../src/scope'
import { CoreMessage, CoreResponse } from '../../src/types'

Expand Down Expand Up @@ -52,6 +53,30 @@ test('sends async CoreMessage to tab', () => {
)
})

test('sends async CoreMessage to frame', () => {
const payload = { greeting: 'hello' }
const tabId = 1234
const frameId = 5678

const coreMessage: CoreMessage = {
async: true,
payload,
scope,
tabId,
}

messages.send(payload, { tabId, frameId, async: true })

expect(chrome.tabs.sendMessage).toBeCalledWith(
tabId,
coreMessage,
{ frameId },
expect.any(Function),
)

expect(chrome.runtime.sendMessage).not.toBeCalled()
})

test('resolves with response', async () => {
const message = { greeting: 'hello' }

Expand All @@ -73,7 +98,7 @@ test('resolves with response', async () => {
})

test('rejects if success === false', async () => {
expect.assertions(2)
expect.assertions(4)

const message = { greeting: 'hello' }
const response = {
Expand All @@ -93,13 +118,19 @@ test('rejects if success === false', async () => {
try {
await messages.send(message, { async: true })
} catch (error) {
expect(error).toBeInstanceOf(Error)
expect(error).toBeInstanceOf(ChromeMessageError)
expect(error.message).toBe(response.greeting)
expect(error.coreResponse).toBe(coreResponse)
expect(error.coreMessage).toMatchObject({
async: true,
payload: message,
scope,
})
}
})

test('rejects if runtime.lastError', async () => {
expect.assertions(2)
expect.assertions(3)

const message = { greeting: 'hello' }
lastErrorMessage = 'should reject'
Expand All @@ -109,6 +140,7 @@ test('rejects if runtime.lastError', async () => {
await messages.send(message, { async: true })
} catch (error) {
expect(lastErrorSpy).toBeCalled()
expect(error).toBeInstanceOf(ChromeMessageError)
expect(error.message).toBe(lastErrorMessage)
}
})
47 changes: 38 additions & 9 deletions tests/units/send-one-way.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chrome } from 'jest-chrome'
import { ChromeMessageError } from '../../src/ChromeMessageError'
import { getScope } from '../../src/scope'
import { CoreMessage } from '../../src/types'

Expand Down Expand Up @@ -42,7 +43,7 @@ test('calls runtime.sendMessage if no target', () => {
expect(chrome.tabs.sendMessage).not.toBeCalled()
})

test('calls tabs.sendMessage if target is number', () => {
test('calls tabs.sendMessage if tabId is given', () => {
const message = { greeting: 'hello' }
const tabId = 1234

Expand All @@ -52,6 +53,30 @@ test('calls tabs.sendMessage if target is number', () => {
expect(chrome.runtime.sendMessage).not.toBeCalled()
})

test('calls tabs.sendMessage with frameId if frameId is given', () => {
const payload = { greeting: 'hello' }
const tabId = 1234
const frameId = 5678

const coreMessage: CoreMessage = {
async: false,
tabId,
payload,
scope,
}

messages.send(payload, { tabId, frameId })

expect(chrome.tabs.sendMessage).toBeCalledWith(
tabId,
coreMessage,
{ frameId },
expect.any(Function),
)

expect(chrome.runtime.sendMessage).not.toBeCalled()
})

test('creates one-way coreMessage', () => {
const message = { greeting: 'hello' }

Expand All @@ -70,19 +95,23 @@ test('creates one-way coreMessage', () => {
)
})

// TODO: unskip once last error implemented
test.skip('rejects if runtime.lastError', async () => {
expect.assertions(1)
test('rejects if runtime.lastError', async () => {
expect.assertions(2)

const message = { greeting: 'hello' }
const errorMessage = 'should not resolve'
// chrome.runtime.sendMessage.setLastError(errorMessage)
const lastError = { message: 'should not resolve' }
chrome.runtime.lastError = lastError

const result = messages.send(message)
chrome.runtime.sendMessage.mockImplementation(
(m: any, sendResponse?: (respond: any) => void) => {
sendResponse?.({})
},
)

try {
await result
await messages.send(message)
} catch (error) {
expect(error.message).toBe(errorMessage)
expect(error).toBeInstanceOf(ChromeMessageError)
expect(error.message).toBe(lastError.message)
}
})

0 comments on commit daffa03

Please sign in to comment.