Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 23, 2024
1 parent 851f78f commit a97d701
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = {
moduleNameMapper: {
'^@packages/jq/(.*)$': '<rootDir>packages/jq/$1',
'^@packages/tokenizer/(.*)$': '<rootDir>packages/tokenizer/$1',
'^@packages/jq$': '<rootDir>packages/jq',
'^@packages/tokenizer$': '<rootDir>packages/tokenizer',
'^@core/(.*)$': '<rootDir>src/core/$1',
'^@testing/(.*)$': '<rootDir>testing/$1',
'^@testing$': '<rootDir>testing',
Expand Down
61 changes: 61 additions & 0 deletions src/background/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const formatMock = jest.fn();
const tokenizeMock = jest.fn();
const jqMock = jest.fn();

jest.mock('@packages/jq', () => ({ jq: jqMock }));
jest.mock('@packages/tokenizer', () => ({
format: formatMock,
tokenize: tokenizeMock,
}));

import { type Message } from '@core/background';
import { describe, test } from '@jest/globals';
import { handler } from './handler';

describe('handler', () => {
test('should handle tokenize message', async () => {
const message: Message = { json: 'json', action: 'tokenize' };
tokenizeMock.mockResolvedValue('tokenized');

const sendResponse = jest.fn();

await handler(message, sendResponse);

expect(tokenizeMock).toHaveBeenCalledWith(message.json);
expect(sendResponse).toHaveBeenCalledWith('tokenized');
});

test('should handle format message', async () => {
const message: Message = { json: 'json', action: 'format' };
formatMock.mockResolvedValue('formatted');

const sendResponse = jest.fn();

await handler(message, sendResponse);

expect(formatMock).toHaveBeenCalledWith(message.json);
expect(sendResponse).toHaveBeenCalledWith('formatted');
});

test('should handle jq message', async () => {
const message: Message = { json: 'json', query: '.query', action: 'jq' };
jqMock.mockResolvedValue('jq');

const sendResponse = jest.fn();

await handler(message, sendResponse);

expect(jqMock).toHaveBeenCalledWith(message.json, message.query);
expect(sendResponse).toHaveBeenCalledWith('jq');
});

test('should do nothing if message is not recognized', async () => {
const message = { json: 'json', action: 'unknown' } as unknown as Message;

const sendResponse = jest.fn();

await handler(message, sendResponse);

expect(sendResponse).not.toHaveBeenCalled();
});
});

0 comments on commit a97d701

Please sign in to comment.