Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a getCharacterData() method for easier access to loaded character data #223

Merged
merged 1 commit into from Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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