-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding test coverage to loading manager * resetting test.js * fixing comment
- Loading branch information
Showing
7 changed files
with
1,895 additions
and
779 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
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,66 @@ | ||
const ren = require('hanzi-writer-data/人.json'); | ||
const HanziWriter = require('../HanziWriter'); | ||
|
||
describe('HanziWriter', () => { | ||
// Hack because JSDom doesn't support SVG well | ||
window.SVGElement.prototype.getTotalLength = () => 10; | ||
|
||
describe('constructor', () => { | ||
it("loads data and builds an instance in a dom element", () => { | ||
document.body.innerHTML = '<div id="target"></div>'; | ||
|
||
const writer = new HanziWriter('target', '人', { | ||
charDataLoader: () => ren, | ||
}); | ||
|
||
// TODO: add more assertions | ||
expect(document.querySelectorAll('svg').length).toBe(1); | ||
}); | ||
|
||
it("calls onLoadCharDataError if provided on loading failure", async () => { | ||
document.body.innerHTML = '<div id="target"></div>'; | ||
|
||
const onLoadCharDataError = jest.fn(); | ||
const writer = new HanziWriter('target', '人', { | ||
onLoadCharDataError, | ||
charDataLoader: () => Promise.reject('reasons'), | ||
}); | ||
|
||
await writer._withDataPromise; | ||
|
||
expect(onLoadCharDataError.mock.calls.length).toBe(1); | ||
expect(onLoadCharDataError.mock.calls[0][0]).toBe('reasons'); | ||
}); | ||
|
||
it("tries reloading when calling an animatable method after loading failure", async () => { | ||
document.body.innerHTML = '<div id="target"></div>'; | ||
|
||
const onLoadCharDataError = jest.fn(); | ||
const writer = new HanziWriter('target', '人', { | ||
onLoadCharDataError, | ||
charDataLoader: (char, onComplete, onErr) => { | ||
onErr('reasons'); | ||
}, | ||
}); | ||
|
||
await writer._withDataPromise; | ||
await writer.showCharacter(); | ||
|
||
expect(onLoadCharDataError.mock.calls.length).toBe(2); | ||
expect(onLoadCharDataError.mock.calls[0][0]).toBe('reasons'); | ||
expect(onLoadCharDataError.mock.calls[1][0]).toBe('reasons'); | ||
}); | ||
|
||
it("throws an error on loading fauire if onLoadCharDataError is not provided", async () => { | ||
document.body.innerHTML = '<div id="target"></div>'; | ||
|
||
const writer = new HanziWriter('target', '人', { | ||
charDataLoader: (char, onComplete, onErr) => { | ||
onErr(new Error('reasons')); | ||
}, | ||
}); | ||
|
||
await expect(writer._withDataPromise).rejects.toThrow(new Error('reasons')); | ||
}); | ||
}); | ||
}); |
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,113 @@ | ||
const ren = require('hanzi-writer-data/人.json'); | ||
const ta = require('hanzi-writer-data/他.json'); | ||
const { timeout } = require('../utils'); | ||
const LoadingManager = require('../LoadingManager'); | ||
|
||
describe('LoadingManager', () => { | ||
describe('loadCharData', () => { | ||
it("resolves when data is loaded via async callback", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => { | ||
setTimeout(() => onComplete(ren), 1); | ||
}, | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(data).toBe(ren); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
|
||
it("resolves when data is loaded via sync callback", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => { onComplete(ren); }, | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(data).toBe(ren); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
|
||
it("resolves when data is loaded via promise", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => Promise.resolve(ren), | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(data).toBe(ren); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
|
||
it("resolves when data is loaded via sync return", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => ren, | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(data).toBe(ren); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
|
||
it("passes data to onLoadCharDataSuccess if provided", async () => { | ||
let successVal; | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => ren, | ||
onLoadCharDataSuccess: (returnedData) => successVal = returnedData | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(data).toBe(ren); | ||
expect(successVal).toBe(ren); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
|
||
it("throws an error if loading fails via onErr callback and no callback is provided", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => { onErr('OMG'); }, | ||
}); | ||
await expect(manager.loadCharData('人')).rejects.toThrow(new Error('Failed to load char data for 人')); | ||
expect(manager.loadingFailed).toBe(true); | ||
}); | ||
|
||
it("rethrows if loading fails via onErr callback passing an Error and no callback is provided", async () => { | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => { onErr(new Error('OMG')); }, | ||
}); | ||
await expect(manager.loadCharData('人')).rejects.toThrow(new Error('OMG')); | ||
expect(manager.loadingFailed).toBe(true); | ||
}); | ||
|
||
it("resolves if loading fails via onErr callback and a callback is provided", async () => { | ||
let failureReason; | ||
const manager = new LoadingManager({ | ||
charDataLoader: (char, onComplete, onErr) => { onErr('everything is terrible'); }, | ||
onLoadCharDataError: (reason) => { failureReason = reason; }, | ||
}); | ||
const data = await manager.loadCharData('人'); | ||
expect(manager.loadingFailed).toBe(true); | ||
expect(data).toBe(undefined); | ||
expect(failureReason).toBe('everything is terrible'); | ||
}); | ||
|
||
it("debounces if multiple loads are called at the same time", async () => { | ||
const onLoadCharDataSuccess = jest.fn(); | ||
const onCompleteFns = []; | ||
const manager = new LoadingManager({ | ||
onLoadCharDataSuccess, | ||
charDataLoader: (char, onComplete) => { | ||
onCompleteFns.push(onComplete); | ||
}, | ||
}); | ||
|
||
const loadPromise1 = manager.loadCharData('人'); | ||
const loadPromise2 = manager.loadCharData('他'); | ||
// it should return the same promise for both since loading isn't complete | ||
expect(loadPromise1).toBe(loadPromise2); | ||
|
||
onCompleteFns[0].call(null, ren); | ||
onCompleteFns[1].call(null, ta); | ||
|
||
const data = await loadPromise1; | ||
|
||
// ren should be ignored. It's like it was never requested at all | ||
expect(data).toBe(ta); | ||
expect(onLoadCharDataSuccess.mock.calls.length).toBe(1); | ||
expect(onLoadCharDataSuccess.mock.calls[0][0]).toBe(ta); | ||
expect(manager.loadingFailed).toBe(false); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.