2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 22:18:24 +00:00
iconify/packages/core/tests/api/callbacks-test.ts

507 lines
10 KiB
TypeScript
Raw Normal View History

import { updateCallbacks, storeCallback } from '../../lib/api/callbacks';
2022-06-29 09:30:13 +00:00
import type { IconStorageWithAPI } from '../../lib/api/types';
2020-04-28 09:47:35 +00:00
import { sortIcons } from '../../lib/icon/sort';
import { getStorage, addIconSet } from '../../lib/storage/storage';
2020-04-28 09:47:35 +00:00
describe('Testing API callbacks', () => {
let prefixCounter = 0;
function nextPrefix(): string {
prefixCounter++;
2022-03-16 21:30:16 +00:00
return (
'api-cb-test-' +
(prefixCounter < 10 ? '0' : '') +
prefixCounter.toString()
);
2020-04-28 09:47:35 +00:00
}
it('Simple callback', (done) => {
const provider = 'iconify';
2020-04-28 09:47:35 +00:00
const prefix = nextPrefix();
let counter = 0;
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
2020-04-28 09:47:35 +00:00
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
2020-04-28 09:47:35 +00:00
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
]);
expect(storage.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(storage);
2020-04-28 09:47:35 +00:00
return;
case 2:
// Second run - icon2 should be added, completing callback
expect(loaded).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([]);
expect(storage.loaderCallbacks?.length).toBe(0);
2020-04-28 09:47:35 +00:00
done();
}
},
sortIcons([
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]),
[storage]
2020-04-28 09:47:35 +00:00
);
// Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Test update - should do nothing
updateCallbacks(storage);
2020-04-28 09:47:35 +00:00
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
2020-04-28 09:47:35 +00:00
// Add few icons and run updateCallbacks
addIconSet(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(storage);
2020-04-28 09:47:35 +00:00
});
});
it('Callback that should not be stored', () => {
const provider = '';
2020-04-28 09:47:35 +00:00
const prefix = nextPrefix();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
2020-04-28 09:47:35 +00:00
addIconSet(storage, {
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
not_found: ['icon3'],
});
storeCallback(
() => {
2020-04-28 09:47:35 +00:00
throw new Error('This code should not be executed!');
},
sortIcons([
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]),
[storage]
2020-04-28 09:47:35 +00:00
);
// callbacks should not have been initialised
expect(storage.loaderCallbacks).toBeUndefined();
2020-04-28 09:47:35 +00:00
});
it('Cancel callback', (done) => {
const provider = 'foo';
2020-04-28 09:47:35 +00:00
const prefix = nextPrefix();
let counter = 0;
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
2020-04-28 09:47:35 +00:00
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
2020-04-28 09:47:35 +00:00
counter++;
expect(counter).toBe(1);
2020-04-28 09:47:35 +00:00
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
2020-04-28 09:47:35 +00:00
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
]);
expect(storage.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(storage);
2020-04-28 09:47:35 +00:00
// Unsubscribe and set timer to call done()
unsubscribe();
expect(storage.loaderCallbacks?.length).toBe(0);
2020-04-28 09:47:35 +00:00
setTimeout(done);
},
sortIcons([
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon1',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon2',
},
{
provider,
2020-04-28 09:47:35 +00:00
prefix,
name: 'icon3',
},
]),
[storage]
2020-04-28 09:47:35 +00:00
);
// Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Test update - should do nothing
updateCallbacks(storage);
2020-04-28 09:47:35 +00:00
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
2020-04-28 09:47:35 +00:00
// Add few icons and run updateCallbacks
addIconSet(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(storage);
});
});
it('Multiple prefixes', (done) => {
const provider = '';
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
2022-06-29 09:30:13 +00:00
const storage1 = getStorage(provider, prefix1) as IconStorageWithAPI;
const storage2 = getStorage(provider, prefix2) as IconStorageWithAPI;
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider,
prefix: prefix1,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix: prefix2,
name: 'icon2',
},
]);
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(storage2);
break;
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
done();
break;
default:
2022-03-16 21:30:16 +00:00
done(`Callback was called ${counter} times.`);
}
},
sortIcons([
{
provider,
prefix: prefix1,
name: 'icon1',
},
{
provider,
prefix: prefix2,
name: 'icon2',
},
{
provider,
prefix: prefix1,
name: 'icon3',
},
]),
[storage1, storage2]
);
// Test callbacks
expect(storage1.loaderCallbacks?.length).toBe(1);
expect(storage2.loaderCallbacks?.length).toBe(1);
// Test update - should do nothing
updateCallbacks(storage1);
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
// Add few icons and run updateCallbacks
addIconSet(storage1, {
prefix: prefix1,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(storage1);
2020-04-28 09:47:35 +00:00
});
});
it('Multiple providers', (done) => {
const provider1 = nextPrefix();
const provider2 = nextPrefix();
2020-04-28 09:47:35 +00:00
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
2022-06-29 09:30:13 +00:00
const storage1 = getStorage(provider1, prefix1) as IconStorageWithAPI;
const storage2 = getStorage(provider2, prefix2) as IconStorageWithAPI;
2020-04-28 09:47:35 +00:00
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
2020-04-28 09:47:35 +00:00
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
2020-04-28 09:47:35 +00:00
{
provider: provider1,
2020-04-28 09:47:35 +00:00
prefix: prefix1,
name: 'icon1',
},
]);
expect(missing).toEqual([
2020-04-28 09:47:35 +00:00
{
provider: provider1,
2020-04-28 09:47:35 +00:00
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
2020-04-28 09:47:35 +00:00
{
provider: provider2,
2020-04-28 09:47:35 +00:00
prefix: prefix2,
name: 'icon2',
},
]);
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(storage2);
2020-04-28 09:47:35 +00:00
break;
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
2020-04-28 09:47:35 +00:00
done();
break;
default:
2022-03-16 21:30:16 +00:00
done(`Callback was called ${counter} times.`);
2020-04-28 09:47:35 +00:00
}
},
sortIcons([
{
provider: provider1,
2020-04-28 09:47:35 +00:00
prefix: prefix1,
name: 'icon1',
},
{
provider: provider2,
2020-04-28 09:47:35 +00:00
prefix: prefix2,
name: 'icon2',
},
{
provider: provider1,
2020-04-28 09:47:35 +00:00
prefix: prefix1,
name: 'icon3',
},
]),
[storage1, storage2]
2020-04-28 09:47:35 +00:00
);
// Test callbacks
expect(storage1.loaderCallbacks?.length).toBe(1);
expect(storage2.loaderCallbacks?.length).toBe(1);
2020-04-28 09:47:35 +00:00
// Test update - should do nothing
updateCallbacks(storage1);
2020-04-28 09:47:35 +00:00
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
2020-04-28 09:47:35 +00:00
// Add few icons and run updateCallbacks
addIconSet(storage1, {
prefix: prefix1,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(storage1);
2020-04-28 09:47:35 +00:00
});
});
});