Skip to content

Commit

Permalink
Remove DI container, repair tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gilmoreg committed Jan 6, 2019
1 parent d4166a2 commit d2b73cf
Show file tree
Hide file tree
Showing 15 changed files with 1,858 additions and 1,892 deletions.
5 changes: 2 additions & 3 deletions __tests__/MAL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import FakeLog from '../__mocks__/Log';
import * as fetchMock from 'fetch-mock';
import * as fakes from '../__testutils__/testData';
import { MALLoadItem } from '../src/Types';
import { diContainer, DIContainer } from '../src/DIContainer';

const mockGetMALList = (list: Array<MALLoadItem>) => {
fetchMock
Expand All @@ -32,7 +31,7 @@ describe('syncType()', () => {
.once(/.+load.json.+/, [malAnime])
.once(/.+load.json.+/, [])
.once(/.+edit.+/, ' Successfully updated entry ');
const mal = new MAL('test', 'csrfToken', fakes.defaultMocks());
const mal = new MAL('test', 'csrfToken', new FakeLog());
await mal.syncType('anime', [alAnime]);
const [url] = fetchMock.calls()[2];
expect(url).toEqual('https://myanimelist.net/ownlist/anime/1/edit?hideLayout');
Expand All @@ -47,7 +46,7 @@ describe('syncType()', () => {
.once(/.+load.json.+/, [malManga])
.once(/.+load.json.+/, [])
.once(/.+edit.+/, {});
const mal = new MAL('test', 'csrfToken', fakes.defaultMocks());
const mal = new MAL('test', 'csrfToken', new FakeLog());
await mal.syncType('manga', [alManga]);
const [url] = fetchMock.calls()[1];
expect(url).toEqual('https://myanimelist.net/ownlist/manga/add.json');
Expand Down
10 changes: 5 additions & 5 deletions __tests__/MALEntry.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMALEntry } from '../src/MALEntry';
import { MALEntryAnime } from '../src/MALEntry';
import * as fakes from '../__testutils__/testData';

const fakeDomMethods = fakes.createFakeDomMethods();
Expand All @@ -7,23 +7,23 @@ describe('shouldUpdate()', () => {
it('should update if the start date is different', () => {
const malAnime = fakes.createFakeMALAnime({ start_date_string: '2-2-2002' });
const alAnime = fakes.createFakeAnilistAnime({ startedAt: { year: 2002, month: 1, day: 1 } });
const malEntry = createMALEntry(alAnime, malAnime, 'csrfToken', fakes.defaultMocks());
const malEntry = new MALEntryAnime(alAnime, malAnime, 'csrfToken', fakeDomMethods);
const result = malEntry.shouldUpdate();
expect(result).toEqual(true);
});

it('should update if the episode count is different and show is incomplete', () => {
const malAnime = fakes.createFakeMALAnime({ status: 1, num_watched_episodes: 1 });
const alAnime = fakes.createFakeAnilistAnime({ status: 'CURRENT', proress: 2 });
const malEntry = createMALEntry(alAnime, malAnime, 'csrfToken', fakes.defaultMocks());
const malEntry = new MALEntryAnime(alAnime, malAnime, 'csrfToken', fakeDomMethods);
const result = malEntry.shouldUpdate();
expect(result).toEqual(true);
});

it('should not update if chapter counts differ but show is complete', () => {
it('should not update if episode counts differ but show is complete', () => {
const malAnime = fakes.createFakeMALAnime({ num_watched_episodes: 1 });
const alAnime = fakes.createFakeAnilistAnime({ progress: 2 });
const malEntry = createMALEntry(alAnime, malAnime, 'csrfToken', fakes.defaultMocks());
const malEntry = new MALEntryAnime(alAnime, malAnime, 'csrfToken', fakeDomMethods);
const result = malEntry.shouldUpdate();
expect(result).toEqual(false);
});
Expand Down
37 changes: 36 additions & 1 deletion __testutils__/setupJest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
import * as fetch from 'node-fetch';
// @ts-ignore
global.fetch = fetch;
global.fetch = fetch;

class FakeDOM {
querySelector(selector: string) {
switch (selector) {
case 'add_anime_comments':
case 'add_manga_comments':
return {
value: 'comments'
}
default:
return {
value: '0'
}
}
}
}

class FakeXHR {
onload: Function = () => { };
onerror: Function = () => { };
responseType: string = 'document';
open(method: string, url: string) {
console.log(`Making XHR ${method} request to ${url}`);
}
send() {
this.onload();
}
responseXML: FakeDOM

constructor() {
this.responseXML = new FakeDOM();
}
}
// @ts-ignore
global.XMLHttpRequest = FakeXHR;
7 changes: 1 addition & 6 deletions __testutils__/testData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import * as Types from '../src/Types';
import { IDomMethods } from '../src/Dom';
import { DIContainer } from '../src/DIContainer';
import FakeLog from '../__mocks__/Log';
import { FakeMALForm } from '../__mocks__/MALForm';

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

Expand Down Expand Up @@ -74,6 +71,4 @@ export const createFakeDomMethods = (dateSetting = 'a'): IDomMethods => ({
getCSRFToken: () => 'csrfToken',
getMALUsername: () => 'malUsername',
getAnilistUsername: () => 'anilistUsername'
});

export const defaultMocks = () => new DIContainer(undefined, () => new FakeMALForm(), createFakeDomMethods(), new FakeLog());
});
98 changes: 35 additions & 63 deletions douki.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,6 @@ exports.getOperationDisplayName = (operation) => {
throw new Error('Unknown operation type');
}
};
exports.fetchDocument = (type, id) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
return resolve(this.responseXML ? this.responseXML : null);
};
xhr.onerror = function (e) {
reject(e);
};
xhr.open('GET', `https://myanimelist.net/ownlist/${type}/${id}/edit`);
xhr.responseType = 'document';
xhr.send();
});


/***/ }),
Expand Down Expand Up @@ -568,13 +556,12 @@ exports.getAnilistList = (username) => fetchList(username)
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = __webpack_require__(3);
const MALEntry_1 = __webpack_require__(7);
const DIContainer_1 = __webpack_require__(9);
const Log_1 = __webpack_require__(1);
class MAL {
constructor(username, csrfToken, deps = DIContainer_1.diContainer) {
constructor(username, csrfToken, log = Log_1.default) {
this.username = username;
this.csrfToken = csrfToken;
this.deps = deps;
this.Log = deps.log;
this.Log = log;
}
createMALHashMap(malList, type) {
const hashMap = {};
Expand Down Expand Up @@ -602,7 +589,7 @@ class MAL {
}
async getEntriesList(anilistList, type) {
const malHashMap = await this.getMALHashMap(type);
return anilistList.map(entry => MALEntry_1.createMALEntry(entry, malHashMap[entry.id], this.csrfToken, this.deps));
return anilistList.map(entry => MALEntry_1.createMALEntry(entry, malHashMap[entry.id], this.csrfToken));
}
async malEdit(data) {
const { type, id } = data;
Expand Down Expand Up @@ -693,10 +680,10 @@ exports.default = MAL;

Object.defineProperty(exports, "__esModule", { value: true });
const MALForm_1 = __webpack_require__(8);
const DIContainer_1 = __webpack_require__(9);
exports.createMALEntry = (al, mal, csrfToken, deps) => al.type === 'anime' ?
new MALEntryAnime(al, mal, csrfToken, deps) :
new MALEntryManga(al, mal, csrfToken, deps);
const Dom_1 = __webpack_require__(4);
exports.createMALEntry = (al, mal, csrfToken) => al.type === 'anime' ?
new MALEntryAnime(al, mal, csrfToken) :
new MALEntryManga(al, mal, csrfToken);
const MALStatus = {
Current: 1,
Completed: 2,
Expand Down Expand Up @@ -731,12 +718,12 @@ const createMALFormData = (malData) => {
return formData.replace(/&$/, '');
};
class BaseMALEntry {
constructor(al, mal, csrfToken = '', deps = DIContainer_1.diContainer) {
constructor(al, mal, csrfToken = '', dom = Dom_1.default) {
this.alData = al;
this.malData = mal;
this.csrfToken = csrfToken;
this._postData = this.createPostData();
this.deps = deps;
this.dom = dom;
}
createBaseMALPostItem() {
return {
Expand All @@ -758,7 +745,7 @@ class BaseMALEntry {
buildDateString(date) {
if (date.month === 0 && date.day === 0 && date.year === 0)
return null;
const dateSetting = this.deps.dom.getDateSetting();
const dateSetting = this.dom.getDateSetting();
const month = `${String(date.month).length < 2 ? '0' : ''}${date.month}`;
const day = `${String(date.day).length < 2 ? '0' : ''}${date.day}`;
const year = `${date.year ? String(date.year).slice(-2) : 0}`;
Expand Down Expand Up @@ -853,8 +840,8 @@ class BaseMALEntry {
}
exports.BaseMALEntry = BaseMALEntry;
class MALEntryAnime extends BaseMALEntry {
constructor(al, mal, csrfToken = '', deps = DIContainer_1.diContainer) {
super(al, mal, csrfToken, deps);
constructor(al, mal, csrfToken = '', dom = Dom_1.default) {
super(al, mal, csrfToken, dom);
}
createPostData() {
const result = this.createBaseMALPostItem();
Expand All @@ -865,12 +852,11 @@ class MALEntryAnime extends BaseMALEntry {
// For new items it will not be present; however the list will refresh after add and
// it should be available then
result.num_watched_episodes = this.malData && this.malData.anime_num_episodes ?
Math.min(this.alData.progress, this.malData.anime_num_episodes) :
this.alData.progress || 0;
Math.min(this.alData.progress, this.malData.anime_num_episodes) : 0;
return result;
}
async formData() {
const malFormData = this.deps.malFormFactory(this.alData.type, this.alData.id);
const malFormData = new MALForm_1.MALForm(this.alData.type, this.alData.id);
await malFormData.get();
const formData = {
anime_id: this.malData.anime_id,
Expand Down Expand Up @@ -905,8 +891,8 @@ class MALEntryAnime extends BaseMALEntry {
}
exports.MALEntryAnime = MALEntryAnime;
class MALEntryManga extends BaseMALEntry {
constructor(al, mal, csrfToken = '', deps = DIContainer_1.diContainer) {
super(al, mal, csrfToken, deps);
constructor(al, mal, csrfToken = '', dom = Dom_1.default) {
super(al, mal, csrfToken, dom);
}
createPostData() {
const result = this.createBaseMALPostItem();
Expand All @@ -917,11 +903,9 @@ class MALEntryManga extends BaseMALEntry {
// For new items they will not be present; however the list will refresh after add and
// they should be available then
result.num_read_chapters = this.malData && this.malData.manga_num_chapters ?
Math.min(this.alData.progress, this.malData.manga_num_chapters) :
this.alData.progress || 0;
Math.min(this.alData.progress, this.malData.manga_num_chapters) : 0;
result.num_read_volumes = this.malData && this.malData.manga_num_volumes ?
Math.min(this.alData.progressVolumes, this.malData.manga_num_volumes) :
this.alData.progressVolumes || 0;
Math.min(this.alData.progressVolumes, this.malData.manga_num_volumes) : 0;
return result;
}
async formData() {
Expand Down Expand Up @@ -970,13 +954,25 @@ exports.MALEntryManga = MALEntryManga;

Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = __webpack_require__(3);
const DIContainer_1 = __webpack_require__(9);
class MALForm {
constructor(type, id, deps = DIContainer_1.diContainer) {
constructor(type, id) {
this.document = null;
this.type = type;
this.id = id;
this.deps = deps;
}
fetchDocument(type, id) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
return resolve(this.responseXML ? this.responseXML : null);
};
xhr.onerror = function (e) {
reject(e);
};
xhr.open('GET', `https://myanimelist.net/ownlist/${type}/${id}/edit`);
xhr.responseType = 'document';
xhr.send();
});
}
getElement(id) {
if (!this.document)
Expand All @@ -985,7 +981,7 @@ class MALForm {
}
async get() {
await util_1.sleep(500);
const document = await this.deps.fetchDocument(this.type, this.id);
const document = await this.fetchDocument(this.type, this.id);
if (document) {
this.document = document;
}
Expand Down Expand Up @@ -1049,30 +1045,6 @@ class MALForm {
}
}
exports.MALForm = MALForm;
exports.createMALForm = (type, id) => new MALForm(type, id);


/***/ }),
/* 9 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = __webpack_require__(3);
const Dom_1 = __webpack_require__(4);
const MALForm_1 = __webpack_require__(8);
const Log_1 = __webpack_require__(1);
class DIContainer {
constructor(fetchDocumentFn = util_1.fetchDocument, malFormFactory = MALForm_1.createMALForm, dom = Dom_1.default, log = Log_1.default) {
this.fetchDocument = fetchDocumentFn;
this.malFormFactory = malFormFactory;
this.dom = dom;
this.log = log;
}
}
exports.DIContainer = DIContainer;
exports.diContainer = new DIContainer();


/***/ })
Expand Down
Loading

0 comments on commit d2b73cf

Please sign in to comment.