Skip to content

Commit

Permalink
Add jest and basic test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gilmoreg committed Dec 30, 2018
1 parent 1f092b6 commit c675f66
Show file tree
Hide file tree
Showing 13 changed files with 4,363 additions and 115 deletions.
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests",
"cwd": "${workspaceFolder}",
"args": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
]
}
11 changes: 11 additions & 0 deletions __mocks__/Dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const addDropDownItem = () => { }

export const addImportForm = (syncFn: Function) => jest.fn();

export const getDateSetting = (): string => 'a';

export const getCSRFToken = (): string => "csrfToken";

export const getMALUsername = () => 'malUsername';

export const getAnilistUsername = () => 'anilistUsername';
5 changes: 5 additions & 0 deletions __mocks__/Log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const clear = () => console.log('clearing console');
export const error = (msg: string) => console.log('logging error', msg);
export const info = (msg: string) => console.log('logging info', msg);
export const addCountLog = (op: string, type: string, max: number) => console.log('creating count log for', op, type, max);
export const updateCountLog = (op: string, type: string, ct: number) => console.log('updating count log for', op, type, ct);
1 change: 1 addition & 0 deletions __mocks__/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sleep = () => Promise.resolve();
24 changes: 24 additions & 0 deletions __tests__/sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
jest.mock('../src/Dom');
jest.mock('../src/Log');
jest.mock('../src/util');
import { syncType } from '../src/MAL';
import * as fetchMock from 'fetch-mock';
import { completedAnime, completedAnilistAnime } from '../__testutils__/testData';

// jest.useFakeTimers();

describe('Sync', () => {
beforeAll(() => fetchMock.catch(500));
afterEach(() => fetchMock.restore());
it('should sync', async () => {
fetchMock
.mock(/.+load.json.+/, [completedAnime], { repeat: 1 })
.mock(/.+load.json.+/, []);
try {
await syncType('anime', [completedAnilistAnime], 'test', 'test');
} catch (e) {
console.error(e);
}
console.log(fetchMock.calls())
});
});
3 changes: 3 additions & 0 deletions __testutils__/setupJest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as fetch from 'node-fetch';
// @ts-ignore
global.fetch = fetch;
32 changes: 32 additions & 0 deletions __testutils__/testData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as Types from '../src/Types';

const createDate = (year = 0, month = 0, day = 0) => ({ year, month, day });

const baseMalItem = (): Types.BaseMALItem => ({
status: 0,
csrf_token: 'csrfToken',
score: 10,
finish_date: createDate(),
start_date: createDate(),
});

export const completedAnime: Types.MALAnime = {
...baseMalItem(),
status: 2,
anime_id: 1,
num_watched_times: 1,
num_watched_episodes: 12,
};

export const completedAnilistAnime: Types.FormattedEntry = {
status: 'COMPLETED',
score: 10,
progress: 12,
progressVolumes: 0,
startedAt: createDate(),
completedAt: createDate(),
repeat: 1,
type: 'anime',
id: 1,
title: 'title',
};
Loading

0 comments on commit c675f66

Please sign in to comment.