Skip to content

Commit

Permalink
feat: adding a getCharacterData() method for easier loading of charac…
Browse files Browse the repository at this point in the history
…ter data (#223)
  • Loading branch information
chanind committed Mar 2, 2021
1 parent 9384f1c commit 3766a8b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/HanziWriter.ts
Expand Up @@ -419,6 +419,14 @@ export default class HanziWriter {
return this._withDataPromise;
}

async getCharacterData(): Promise<Character> {
if (!this._char) {
throw new Error('setCharacter() must be called before calling getCharacterData()');
}
const character = await this._withData(() => this._character);
return character!;
}

_assignOptions(options: Partial<HanziWriterOptions>): ParsedHanziWriterOptions {
const mergedOptions = {
...defaultOptions,
Expand Down
55 changes: 55 additions & 0 deletions src/__tests__/HanziWriter-test.ts
Expand Up @@ -9,6 +9,7 @@ import HanziWriter from '../HanziWriter';
import { timeout } from '../utils';
import { resolvePromises } from '../testUtils';
import Quiz from '../Quiz';
import parseCharData from '../parseCharData';

const charDataLoader = () => ren;

Expand Down Expand Up @@ -225,6 +226,60 @@ describe('HanziWriter', () => {
});
});

describe('getCharacterData', () => {
it('returns a promise with the loaded character', async () => {
const writer = HanziWriter.create('target', '人', {
charDataLoader,
});
const character = await writer.getCharacterData();
expect(character).toEqual(parseCharData('人', ren));
});

it('returns a promise with the loaded character after initial loading is done', async () => {
const writer = HanziWriter.create('target', '人', {
charDataLoader,
});
await writer._withDataPromise;
const character = await writer.getCharacterData();
expect(character).toEqual(parseCharData('人', ren));
});

it('errors if no character has been set yet', async () => {
const writer = new HanziWriter('target', {
charDataLoader,
});
await expect(writer.getCharacterData()).rejects.toThrow(
new Error('setCharacter() must be called before calling getCharacterData()'),
);
});

it('errors if loading fails', async () => {
const writer = HanziWriter.create('target', '人', {
charDataLoader: async () => {
throw new Error('omg!');
},
});

await expect(writer.getCharacterData()).rejects.toThrow(new Error('omg!'));
});

it('errors if loading fails before this method is called', async () => {
const writer = HanziWriter.create('target', '人', {
charDataLoader: async () => {
throw new Error('omg!');
},
});
try {
await writer._withDataPromise;
// eslint-disable-next-line no-empty
} catch (err) {}

await expect(writer.getCharacterData()).rejects.toThrow(
new Error('Failed to load character data. Call setCharacter and try again.'),
);
});
});

describe('animateCharacter', () => {
it('animates and returns promise that resolves when animation is finished', async () => {
document.body.innerHTML = '<div id="target"></div>';
Expand Down

0 comments on commit 3766a8b

Please sign in to comment.