diff --git a/tests/icon-set/get-stored-icon-test.ts b/tests/icon-set/get-stored-icon-test.ts index 0d6d8de..f8cfc03 100644 --- a/tests/icon-set/get-stored-icon-test.ts +++ b/tests/icon-set/get-stored-icon-test.ts @@ -145,4 +145,49 @@ describe('Loading icon data from storage', () => { // Missing icons expect(await getIcon('invalid-icon')).toBeNull(); }); + + test('Synchronous loading', async () => { + const iconSet = JSON.parse(await loadFixture('json/mdi-light.json')) as IconifyJSON; + + function store(): Promise { + return new Promise((fulfill, reject) => { + // Create storage + const dir = uniqueCacheDir(); + const cacheDir = '{cache}/' + dir; + const storage = createStorage({ + cacheDir, + maxCount: 2, + }); + + // Split icon set + storeLoadedIconSet(iconSet, fulfill, storage, { + chunkSize: 5000, + minIconsPerChunk: 10, + }); + }); + } + const storedIconSet = await store(); + + function syncTest(): Promise { + return new Promise((fulfill, reject) => { + const name = 'star'; + let isSync1 = true; + + // First run should be async + getStoredIconData(storedIconSet, name, () => { + let isSync2 = true; + + // Second run should be synchronous + getStoredIconData(storedIconSet, name, () => { + fulfill(isSync2 === true && isSync1 === false); + }); + isSync2 = false; + }); + isSync1 = false; + }); + } + + // Load icon + expect(await syncTest()).toBeTruthy(); + }); }); diff --git a/tests/icon-set/get-stored-icons-test.ts b/tests/icon-set/get-stored-icons-test.ts index cba6bd6..476e448 100644 --- a/tests/icon-set/get-stored-icons-test.ts +++ b/tests/icon-set/get-stored-icons-test.ts @@ -160,4 +160,49 @@ describe('Loading icons from storage', () => { height: 24, }); }); + + test('Synchronous loading', async () => { + const iconSet = JSON.parse(await loadFixture('json/mdi-light.json')) as IconifyJSON; + + function store(): Promise { + return new Promise((fulfill, reject) => { + // Create storage + const dir = uniqueCacheDir(); + const cacheDir = '{cache}/' + dir; + const storage = createStorage({ + cacheDir, + maxCount: 5, + }); + + // Split icon set + storeLoadedIconSet(iconSet, fulfill, storage, { + chunkSize: 5000, + minIconsPerChunk: 10, + }); + }); + } + const storedIconSet = await store(); + + function syncTest(): Promise { + return new Promise((fulfill, reject) => { + const names: string[] = ['abacus', 'floor-1', 'star', 'wifi']; + let isSync1 = true; + + // First run should be async + getStoredIconsData(storedIconSet, names, () => { + let isSync2 = true; + + // Second run should be synchronous + getStoredIconsData(storedIconSet, names, () => { + fulfill(isSync2 === true && isSync1 === false); + }); + isSync2 = false; + }); + isSync1 = false; + }); + } + + // Load icons + expect(await syncTest()).toBeTruthy(); + }); });