refactor/code-splitting #31
@@ -1,4 +1,5 @@
|
||||
/** @vitest-environment jsdom */
|
||||
import { FontParseError } from '../../errors';
|
||||
import { loadFont } from './loadFont';
|
||||
|
||||
describe('loadFont', () => {
|
||||
@@ -65,8 +66,7 @@ describe('loadFont', () => {
|
||||
expect(result).toBe(mockFontInstance);
|
||||
});
|
||||
|
||||
it('propagates and logs error when font.load() rejects', async () => {
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
it('throws FontParseError when font.load() rejects', async () => {
|
||||
const loadError = new Error('parse failed');
|
||||
const MockFontFace = vi.fn(
|
||||
function(this: any, name: string, buffer: BufferSource, options: FontFaceDescriptors) {
|
||||
@@ -75,18 +75,19 @@ describe('loadFont', () => {
|
||||
);
|
||||
vi.stubGlobal('FontFace', MockFontFace);
|
||||
|
||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toThrow('parse failed');
|
||||
expect(consoleSpy).toHaveBeenCalledWith(loadError);
|
||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toBeInstanceOf(
|
||||
FontParseError,
|
||||
);
|
||||
});
|
||||
|
||||
it('propagates and logs error when document.fonts.add throws', async () => {
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
it('throws FontParseError when document.fonts.add throws', async () => {
|
||||
const addError = new Error('add failed');
|
||||
mockFontFaceSet.add.mockImplementation(() => {
|
||||
throw addError;
|
||||
});
|
||||
|
||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toThrow('add failed');
|
||||
expect(consoleSpy).toHaveBeenCalledWith(addError);
|
||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toBeInstanceOf(
|
||||
FontParseError,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { FontLoadRequestConfig } from '../../../../types';
|
||||
import { FontParseError } from '../../errors';
|
||||
|
||||
export type PartialConfig = Pick<FontLoadRequestConfig, 'weight' | 'name' | 'isVariable'>;
|
||||
/**
|
||||
@@ -6,6 +7,7 @@ export type PartialConfig = Pick<FontLoadRequestConfig, 'weight' | 'name' | 'isV
|
||||
* @param config - The font load request configuration.
|
||||
* @param buffer - The buffer containing the font data.
|
||||
* @returns A promise that resolves to the loaded `FontFace`.
|
||||
* @throws {@link FontParseError} When the font buffer cannot be parsed or added to the document font set.
|
||||
*/
|
||||
export async function loadFont(config: PartialConfig, buffer: BufferSource): Promise<FontFace> {
|
||||
try {
|
||||
@@ -20,7 +22,6 @@ export async function loadFont(config: PartialConfig, buffer: BufferSource): Pro
|
||||
|
||||
return font;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
throw new FontParseError(config.name, error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user