Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Split extension-specific changes off
  • Loading branch information
cpiber committed Aug 17, 2024
1 parent 7e35335 commit aa6c31e
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/scripts/background/ifidf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dot, norm } from '../helpers/shared';
import { normalizeString } from './misc';
import { normalizeString } from './normalize';

type SearchableVideo = {
title: string,
Expand Down
1 change: 1 addition & 0 deletions src/scripts/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './misc';
export * from './nebula';
export * from './normalize';
export * from './youtube';

export type Creator = {
Expand Down
5 changes: 0 additions & 5 deletions src/scripts/background/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,3 @@ export const loadCreators: () => Promise<Creator[]> = (() => {

return getInformation;
})();

// @ts-expect-error regex works, don't touch
export const normalizeString = (str: string) => str.toLowerCase().normalize('NFD').replace(/\p{Pd}/g, '-')
/* eslint-disable-next-line no-misleading-character-class */
.replace(/["'()[\]{}\u0300-\u036f]/g, '');
2 changes: 1 addition & 1 deletion src/scripts/background/nebula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Video } from '.';
import { getChannelVideos, searchVideos } from '../helpers/api';
import { ytvideo } from '../helpers/shared';
import { creatorHasVideo } from './ifidf';
import { normalizeString } from './misc';
import { normalizeString } from './normalize';

const plistcache: { [key: string]: Video[]; } = {};
export const loadNebulaChannelVideos = async (channel: string, num: number) => {
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/background/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-expect-error regex works, don't touch
export const normalizeString = (str: string) => str.toLowerCase().normalize('NFD').replace(/\p{Pd}/g, '-')
/* eslint-disable-next-line no-misleading-character-class */
.replace(/["'()[\]{}\u0300-\u036f]/g, '');
2 changes: 1 addition & 1 deletion src/scripts/background/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Video } from '.';
import { getBase, ytvideo } from '../helpers/shared';
import { creatorHasVideo } from './ifidf';
import { normalizeString } from './misc';
import { normalizeString } from './normalize';

const plistcache: { [key: string]: Video[]; } = {};
export const loadYTVideos = async (playlist: string, num: number) => {
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/background_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ getBrowserInstance().runtime.onMessage.addListener((message: string | { [key: st
if (ret) {
ret.then(
res => {
console.dev.log('Result for', msg.type, ':', res);
if (__DEV__) console.log('Result for', msg.type, ':', res);
sendResponse({ res });
},
err => {
console.dev.log('Error running', msg.type, ':', err);
if (__DEV__) console.log('Error running', msg.type, ':', err);
sendResponse({ err });
},
);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/creators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { jest } from '@jest/globals';
import fetch from 'node-fetch';
import { loadCreators } from '../../src/scripts/background';
import { matchVideoConfidence } from '../../src/scripts/background/ifidf';
import '../../src/scripts/helpers/shared/prototype';
import { getInformation as loadCreators } from '../../src/scripts/page/offscreen';

global.fetch = fetch as unknown as typeof global.fetch;

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/nebula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '../../src/scripts/helpers/shared/prototype';

global.fetch = fetch as unknown as typeof global.fetch;

const { creatorHasNebulaVideo, loadNebulaChannelVideos, existsNebulaVideo, loadNebulaSearchVideos } = await import('../../src/scripts/background');
const { creatorHasNebulaVideo, loadNebulaChannelVideos, existsNebulaVideo, loadNebulaSearchVideos } = await import('../../src/scripts/background/nebula');

describe('loading nebula videos', () => {
const consoleError = console.error;
Expand Down Expand Up @@ -40,9 +40,9 @@ describe('loading nebula videos', () => {
const fetchMock = jest.fn<any>();
global.fetch = fetchMock;
await expect(creatorHasNebulaVideo('hai', vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId });
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
await expect(loadNebulaChannelVideos('hai', 100)).resolves.toHaveLength(100);
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
global.fetch = fetch as unknown as typeof global.fetch;
});

Expand All @@ -60,9 +60,9 @@ describe('loading nebula videos', () => {
const fetchMock = jest.fn<any>();
global.fetch = fetchMock;
await expect(existsNebulaVideo(vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId });
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
await expect(loadNebulaSearchVideos('test', 100)).resolves.toHaveLength(100);
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
global.fetch = fetch as unknown as typeof global.fetch;
});
});
21 changes: 6 additions & 15 deletions tests/unit/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { jest } from '@jest/globals';
import { JSDOM } from 'jsdom';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { Event, Message, arrFromLengthy, clone, debounce, dot, getCookie, injectFunction, injectScript, isMobile, isVideoListPage, isVideoPage, norm, parseMaybeJSON, parseTypeObject, replyMessage, sendEventHandler, sendMessage } from '../../src/scripts/helpers/shared';
import { Event, Message, arrFromLengthy, clone, debounce, dot, getCookie, injectScript, isMobile, isVideoListPage, isVideoPage, norm, parseMaybeJSON, parseTypeObject, replyMessage, sendEventHandler, sendMessage } from '../../src/scripts/helpers/shared';
import '../../src/scripts/helpers/shared/prototype';

const __dirname = dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('other', () => {

expect(isMobile()).toBe(true);
expect(isMobile()).toBe(false);
expect(mock).toBeCalledTimes(2);
expect(mock).toHaveBeenCalledTimes(2);
mock.mock.calls.forEach(c => {
expect(c[0]).toMatch(/pointer/);
});
Expand All @@ -121,12 +121,12 @@ describe('other', () => {
for (let i = 0; i < 10; i++)
m();
jest.runAllTimers();
expect(mock).toBeCalledTimes(1);
expect(mock).toHaveBeenCalledTimes(1);

for (let i = 0; i < 10; i++)
m();
jest.runAllTimers();
expect(mock).toBeCalledTimes(2);
expect(mock).toHaveBeenCalledTimes(2);
});

test('arrFromLengthy converts to array', () => {
Expand Down Expand Up @@ -234,17 +234,8 @@ describe('script injection', () => {

const wrapper = dom.window.document.head;
await expect(injectScript('../fixtures/waitForEvent.js', wrapper, 'test', 'data', dom.window as never as Window)).resolves.toBe(void 0);
expect(mock).toBeCalledTimes(1);
expect(mock).toHaveBeenCalledWith('data');
console.log = log;
});

test('injecting function works with arguments', () => {
const { log } = console;
const mock = console.log = jest.fn();
injectFunction(document.body, data => console.log(data), 'test-data');
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('test-data');
expect(mock).toHaveBeenCalledWith('data');
console.log = log;
});
});
Expand Down Expand Up @@ -358,7 +349,7 @@ describe('message event listeners', () => {
cb.mockImplementationOnce(resolve);
window.postMessage({ type: name, res: i }, '*');
})).resolves.toBe(i);
expect(cb).toBeCalledTimes(i + 1);
expect(cb).toHaveBeenCalledTimes(i + 1);
}
});
});
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { jest } from '@jest/globals';
import fetch from 'node-fetch';
import { Video, creatorHasYTVideo, loadYTVideos } from '../../src/scripts/background';
import type { Video } from '../../src/scripts/background';
import { creatorHasYTVideo, loadYTVideos } from '../../src/scripts/background/youtube';
import '../../src/scripts/helpers/shared/prototype';

global.fetch = fetch as unknown as typeof global.fetch;
Expand Down Expand Up @@ -36,9 +37,9 @@ describe('loading youtube videos', () => {
const fetchMock = jest.fn<any>();
global.fetch = fetchMock;
await expect(creatorHasYTVideo('UUuCkxoKLYO_EQ2GeFtbM_bw', vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId });
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
await expect(loadYTVideos('UUuCkxoKLYO_EQ2GeFtbM_bw', 100)).resolves.toHaveLength(100);
expect(fetchMock).not.toBeCalled();
expect(fetchMock).not.toHaveBeenCalled();
global.fetch = fetch as unknown as typeof global.fetch;
});
});

0 comments on commit aa6c31e

Please sign in to comment.