-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from interledger/feature/test-coverage-and-impr…
…ovements Improvements, fixes and unit test coverage
- Loading branch information
Showing
17 changed files
with
425 additions
and
79 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,123 @@ | ||
import { initListeners } from '@/lib/index' | ||
import { addTabChangeListener } from '@/lib/listeners' | ||
import { addMessageListener, sendRuntimeMessage } from '@/lib/messageUtils' | ||
|
||
// Mock the addMessageListener and sendRuntimeMessage functions | ||
jest.mock('@/lib/messageUtils', () => ({ | ||
addMessageListener: jest.fn(), | ||
sendRuntimeMessage: jest.fn(), | ||
})) | ||
|
||
// Mock browser.tabs.onActivated listener | ||
const onActivatedMock = jest.fn() | ||
const tabsMock = { | ||
onActivated: { | ||
addListener: onActivatedMock, | ||
}, | ||
} | ||
// Mock the browser object with the tabsMock | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
;(global as any).browser = { | ||
tabs: tabsMock, | ||
} | ||
|
||
describe('initListeners', () => { | ||
// Set up the HTML body before each test | ||
beforeEach(() => { | ||
document.body.innerHTML = ` | ||
<link rel="monetization" href="https://example.com/payment" /> | ||
` | ||
}) | ||
|
||
// Clean up the HTML body and mock function calls after each test | ||
afterEach(() => { | ||
document.body.innerHTML = '' | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('sends MONETIZATION_START message if monetization link exists', () => { | ||
initListeners() | ||
|
||
expect(sendRuntimeMessage).toHaveBeenCalledWith('MONETIZATION_START', true) | ||
}) | ||
|
||
it('sends MONETIZATION_START message with false if monetization link does not exist', () => { | ||
document.body.innerHTML = '' | ||
initListeners() | ||
|
||
expect(sendRuntimeMessage).toHaveBeenCalledWith('MONETIZATION_START', false) | ||
}) | ||
|
||
it('listens for GET_MONETIZATION message and responds with monetization link status', () => { | ||
// Mock addMessageListener function | ||
const addMessageListenerMock = addMessageListener as jest.Mock | ||
addMessageListenerMock.mockImplementation(listener => { | ||
const sendResponseMock = jest.fn() | ||
const sender = {} | ||
|
||
listener({ action: 'GET_MONETIZATION' }, sender, sendResponseMock) | ||
|
||
expect(sendResponseMock).toHaveBeenCalledWith(true) | ||
}) | ||
|
||
initListeners() | ||
}) | ||
|
||
it('sends MONETIZATION_START message based on link existence', () => { | ||
// Monetization link exists | ||
document.body.innerHTML = '<link rel="monetization" href="https://example.com/payment" />' | ||
const sendResponseMock = jest.fn() | ||
const sender = {} | ||
|
||
const addMessageListenerMock = addMessageListener as jest.Mock | ||
addMessageListenerMock.mockImplementation(listener => { | ||
listener({ action: 'GET_MONETIZATION' }, sender, sendResponseMock) | ||
}) | ||
|
||
initListeners() | ||
|
||
expect(sendResponseMock).toHaveBeenCalledWith(true) | ||
|
||
// Monetization link does not exist | ||
document.body.innerHTML = '' | ||
sendResponseMock.mockClear() | ||
|
||
initListeners() | ||
|
||
expect(sendResponseMock).toHaveBeenCalledWith(false) | ||
}) | ||
}) | ||
|
||
describe('addTabChangeListener', () => { | ||
it('registers tab change listener', () => { | ||
const listenerMock = jest.fn() | ||
tabsMock.onActivated.addListener.mockImplementation(callback => { | ||
callback({ tabId: 123, windowId: 456 }) | ||
|
||
expect(listenerMock).toHaveBeenCalled() | ||
}) | ||
|
||
addTabChangeListener(listenerMock) | ||
}) | ||
|
||
it('registers tab change listener and handles failure', () => { | ||
// Mock addMessageListener function | ||
const addMessageListenerMock = addMessageListener as jest.Mock | ||
addMessageListenerMock.mockImplementation(listener => { | ||
const sendResponseMock = jest.fn() | ||
const sender = {} | ||
|
||
listener({ action: 'GET_MONETIZATION' }, sender, sendResponseMock) | ||
|
||
expect(sendResponseMock).toHaveBeenCalledWith(false) | ||
}) | ||
|
||
// Simulate tab change listener registration failure | ||
tabsMock.onActivated.addListener = jest.fn(() => { | ||
throw new Error('Listener registration failed') | ||
}) | ||
|
||
// Ensure that no errors are thrown when initListeners is called | ||
expect(() => initListeners()).not.toThrow() | ||
}) | ||
}) |
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,120 @@ | ||
import { BrowserAPI } from '@/lib/index' | ||
import { | ||
addMessageListener, | ||
queryActiveTab, | ||
sendRuntimeMessage, | ||
sendTabsMessage, | ||
} from '@/lib/messageUtils' | ||
|
||
describe('messageUtils', () => { | ||
let originalBrowser | ||
|
||
// Store the original global browser object | ||
beforeAll(() => { | ||
originalBrowser = global.browser | ||
}) | ||
|
||
// Restore the original global browser object after all tests | ||
afterAll(() => { | ||
global.browser = originalBrowser | ||
}) | ||
|
||
// Clear mock function calls after each test | ||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('sendTabsMessage', () => { | ||
it('should send tabs message and return true', () => { | ||
const message = { action: 'ACTION', content: 'test' } | ||
const tabId = 123 | ||
const callback = jest.fn() | ||
|
||
// Call the sendTabsMessage function | ||
const result = sendTabsMessage(message, tabId, callback) | ||
|
||
// Check if BrowserAPI.tabs.sendMessage was called with the expected arguments | ||
expect(BrowserAPI.tabs.sendMessage).toHaveBeenCalledWith(tabId, message, callback) | ||
// Check if the result is true | ||
expect(result).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('sendRuntimeMessage', () => { | ||
it('should send runtime message with action and payload', () => { | ||
const action = 'ACTION' | ||
const payload = { data: 'payload' } | ||
const callback = jest.fn() | ||
|
||
// Call the sendRuntimeMessage function | ||
sendRuntimeMessage(action, payload, callback) | ||
|
||
// Check if BrowserAPI.runtime.sendMessage was called with the expected arguments | ||
expect(BrowserAPI.runtime.sendMessage).toHaveBeenCalledWith( | ||
{ type: action, content: payload }, | ||
callback, | ||
) | ||
}) | ||
}) | ||
|
||
describe('addMessageListener', () => { | ||
it('should add message listener and call the provided callback', () => { | ||
const listener = jest.fn() | ||
const addListenerMock = jest.fn() | ||
|
||
// Mock the runtime.onMessage.addListener function | ||
BrowserAPI.runtime.onMessage = { | ||
addListener: addListenerMock, | ||
} | ||
|
||
// Call the addMessageListener function | ||
addMessageListener(listener) | ||
|
||
// Check if BrowserAPI.runtime.onMessage.addListener was called with the expected argument | ||
expect(addListenerMock).toHaveBeenCalledWith(listener) | ||
}) | ||
|
||
it('adds message listener and calls the provided callback', () => { | ||
const listenerMock = jest.fn() | ||
// Call the addMessageListener function | ||
addMessageListener(listenerMock) | ||
|
||
// Check if BrowserAPI.runtime.onMessage.addListener was called with an anonymous function | ||
expect(BrowserAPI.runtime.onMessage.addListener).toHaveBeenCalledWith(expect.any(Function)) | ||
const addedListener = BrowserAPI.runtime.onMessage.addListener.mock.calls[0][0] | ||
|
||
// Simulate the listener being called | ||
const sendResponseMock = jest.fn() | ||
addedListener({ action: 'GET_MONETIZATION' }, {}, sendResponseMock) | ||
|
||
// Check if the listenerMock was called with the expected arguments | ||
expect(listenerMock).toHaveBeenCalledWith( | ||
{ action: 'GET_MONETIZATION' }, | ||
{}, | ||
sendResponseMock, | ||
) | ||
}) | ||
}) | ||
|
||
describe('queryActiveTab', () => { | ||
it('should query active tab and call the callback with the active tab', () => { | ||
const activeTab = { id: 123 } | ||
const callback = jest.fn() | ||
const tabsMock = { query: jest.fn((_, cb) => cb([activeTab])) } | ||
|
||
// Mock BrowserAPI.tabs with tabsMock | ||
BrowserAPI.tabs = tabsMock | ||
|
||
// Call the queryActiveTab function | ||
queryActiveTab(callback) | ||
|
||
// Check if the callback was called with the activeTab | ||
expect(callback).toHaveBeenCalledWith(activeTab) | ||
// Check if BrowserAPI.tabs.query was called with the expected arguments | ||
expect(tabsMock.query).toHaveBeenCalledWith( | ||
{ active: true, currentWindow: true }, | ||
expect.any(Function), | ||
) | ||
}) | ||
}) | ||
}) |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.