2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-22 14:48:24 +00:00

Replace jest done function with promises in core tests

This commit is contained in:
Vjacheslav Trushkin 2022-09-07 20:03:58 +03:00
parent 2dc12bd462
commit ac009bcb39
5 changed files with 1571 additions and 1512 deletions

View File

@ -14,124 +14,126 @@ describe('Testing API callbacks', () => {
); );
} }
it('Simple callback', (done) => { it('Simple callback', () => {
const provider = 'iconify'; return new Promise((fulfill) => {
const prefix = nextPrefix(); const provider = 'iconify';
let counter = 0; const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix) as IconStorageWithAPI; const storage = getStorage(provider, prefix) as IconStorageWithAPI;
const abort = storeCallback( const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => { (loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort); expect(unsubscribe).toBe(abort);
counter++; counter++;
switch (counter) { switch (counter) {
case 1: case 1:
// First run - icon1 should be loaded, icon3 should be missing // First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([ expect(loaded).toEqual([
{ {
provider, provider,
prefix, prefix,
name: 'icon1', name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'icon2',
},
]);
expect(storage.loaderCallbacks?.length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
}, },
}, ]);
}); expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'icon2',
},
]);
expect(storage.loaderCallbacks?.length).toBe(1);
updateCallbacks(storage); // Add icon2 and trigger update
return; addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2: updateCallbacks(storage);
// Second run - icon2 should be added, completing callback return;
expect(loaded).toEqual([
{ case 2:
provider, // Second run - icon2 should be added, completing callback
prefix, expect(loaded).toEqual([
name: 'icon1', {
}, provider,
{ prefix,
provider, name: 'icon1',
prefix, },
name: 'icon2', {
}, provider,
]); prefix,
expect(missing).toEqual([ name: 'icon2',
{ },
provider, ]);
prefix, expect(missing).toEqual([
name: 'icon3', {
}, provider,
]); prefix,
expect(pending).toEqual([]); name: 'icon3',
expect(storage.loaderCallbacks?.length).toBe(0); },
done(); ]);
} expect(pending).toEqual([]);
}, expect(storage.loaderCallbacks?.length).toBe(0);
sortIcons([ fulfill(true);
{ }
provider,
prefix,
name: 'icon1',
}, },
{ sortIcons([
provider, {
prefix, provider,
name: 'icon2', prefix,
}, name: 'icon1',
{
provider,
prefix,
name: 'icon3',
},
]),
[storage]
);
// Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
// Test update - should do nothing
updateCallbacks(storage);
// 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(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
}, },
}, {
not_found: ['icon3'], provider,
}); prefix,
name: 'icon2',
},
{
provider,
prefix,
name: 'icon3',
},
]),
[storage]
);
// Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
// Test update - should do nothing
updateCallbacks(storage); updateCallbacks(storage);
// 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(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(storage);
});
}); });
}); });
@ -181,326 +183,344 @@ describe('Testing API callbacks', () => {
expect(storage.loaderCallbacks).toBeUndefined(); expect(storage.loaderCallbacks).toBeUndefined();
}); });
it('Cancel callback', (done) => { it('Cancel callback', () => {
const provider = 'foo'; return new Promise((fulfill) => {
const prefix = nextPrefix(); const provider = 'foo';
let counter = 0; const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix) as IconStorageWithAPI; const storage = getStorage(provider, prefix) as IconStorageWithAPI;
const abort = storeCallback( const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => { (loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort); expect(unsubscribe).toBe(abort);
counter++; counter++;
expect(counter).toBe(1); expect(counter).toBe(1);
// First run - icon1 should be loaded, icon3 should be missing // First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([ expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'icon2',
},
]);
expect(storage.loaderCallbacks?.length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(storage);
// Unsubscribe and set timer to complete test
unsubscribe();
expect(storage.loaderCallbacks?.length).toBe(0);
setTimeout(fulfill);
},
sortIcons([
{ {
provider, provider,
prefix, prefix,
name: 'icon1', name: 'icon1',
}, },
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{ {
provider, provider,
prefix, prefix,
name: 'icon2', name: 'icon2',
}, },
]); {
expect(storage.loaderCallbacks?.length).toBe(1); provider,
prefix,
name: 'icon3',
},
]),
[storage]
);
// Add icon2 and trigger update // Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
// Test update - should do nothing
updateCallbacks(storage);
// 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(storage, { addIconSet(storage, {
prefix: prefix, prefix: prefix,
icons: { icons: {
icon2: { icon1: {
body: '<g></g>', body: '<g></g>',
}, },
}, },
not_found: ['icon3'],
}); });
updateCallbacks(storage); updateCallbacks(storage);
// Unsubscribe and set timer to call done()
unsubscribe();
expect(storage.loaderCallbacks?.length).toBe(0);
setTimeout(done);
},
sortIcons([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
{
provider,
prefix,
name: 'icon3',
},
]),
[storage]
);
// Test callbacks
expect(storage.loaderCallbacks?.length).toBe(1);
// Test update - should do nothing
updateCallbacks(storage);
// 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(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
}); });
updateCallbacks(storage);
}); });
}); });
it('Multiple prefixes', (done) => { it('Multiple prefixes', () => {
const provider = ''; return new Promise((fulfill, reject) => {
const prefix1 = nextPrefix(); const provider = '';
const prefix2 = nextPrefix(); const prefix1 = nextPrefix();
let counter = 0; const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider, prefix1) as IconStorageWithAPI; const storage1 = getStorage(
const storage2 = getStorage(provider, prefix2) as IconStorageWithAPI; provider,
prefix1
) as IconStorageWithAPI;
const storage2 = getStorage(
provider,
prefix2
) as IconStorageWithAPI;
const abort = storeCallback( const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => { (loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort); expect(unsubscribe).toBe(abort);
counter++; counter++;
switch (counter) { switch (counter) {
case 1: case 1:
// First run - icon1 should be loaded, icon3 should be missing // First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([ expect(loaded).toEqual([
{ {
provider, provider,
prefix: prefix1, prefix: prefix1,
name: 'icon1', 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>',
}, },
}, ]);
}); 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);
updateCallbacks(storage2); // Add icon2 and trigger update
break; addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2: updateCallbacks(storage2);
// Second run - icon2 should be loaded break;
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
done();
break;
default: case 2:
done(`Callback was called ${counter} times.`); // Second run - icon2 should be loaded
} expect(storage1.loaderCallbacks?.length).toBe(0);
}, expect(storage2.loaderCallbacks?.length).toBe(0);
sortIcons([ fulfill(true);
{ break;
provider,
prefix: prefix1, default:
name: 'icon1', reject(`Callback was called ${counter} times.`);
}
}, },
{ sortIcons([
provider, {
prefix: prefix2, provider,
name: 'icon2', prefix: prefix1,
}, name: 'icon1',
{
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'], 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); 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);
});
}); });
}); });
it('Multiple providers', (done) => { it('Multiple providers', () => {
const provider1 = nextPrefix(); return new Promise((fulfill, reject) => {
const provider2 = nextPrefix(); const provider1 = nextPrefix();
const prefix1 = nextPrefix(); const provider2 = nextPrefix();
const prefix2 = nextPrefix(); const prefix1 = nextPrefix();
let counter = 0; const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider1, prefix1) as IconStorageWithAPI; const storage1 = getStorage(
const storage2 = getStorage(provider2, prefix2) as IconStorageWithAPI; provider1,
prefix1
) as IconStorageWithAPI;
const storage2 = getStorage(
provider2,
prefix2
) as IconStorageWithAPI;
const abort = storeCallback( const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => { (loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort); expect(unsubscribe).toBe(abort);
counter++; counter++;
switch (counter) { switch (counter) {
case 1: case 1:
// First run - icon1 should be loaded, icon3 should be missing // First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([ expect(loaded).toEqual([
{ {
provider: provider1, provider: provider1,
prefix: prefix1, prefix: prefix1,
name: 'icon1', name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider: provider1,
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider: provider2,
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>',
}, },
}, ]);
}); expect(missing).toEqual([
{
provider: provider1,
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider: provider2,
prefix: prefix2,
name: 'icon2',
},
]);
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(1);
updateCallbacks(storage2); // Add icon2 and trigger update
break; addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2: updateCallbacks(storage2);
// Second run - icon2 should be loaded break;
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
done(); case 2:
break; // Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
default: fulfill(true);
done(`Callback was called ${counter} times.`); break;
}
}, default:
sortIcons([ reject(`Callback was called ${counter} times.`);
{ }
provider: provider1,
prefix: prefix1,
name: 'icon1',
}, },
{ sortIcons([
provider: provider2, {
prefix: prefix2, provider: provider1,
name: 'icon2', prefix: prefix1,
}, name: 'icon1',
{
provider: provider1,
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'], provider: provider2,
}); prefix: prefix2,
name: 'icon2',
},
{
provider: provider1,
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); 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);
});
}); });
}); });
}); });

View File

@ -23,60 +23,66 @@ describe('Testing live API with fetch', () => {
setAPIModule('', mockAPIModule); setAPIModule('', mockAPIModule);
}); });
it('Missing API configuration', (done) => { it('Missing API configuration', () => {
const provider = nextProvider(); return new Promise((fulfill) => {
sendAPIQuery( const provider = nextProvider();
provider, sendAPIQuery(
{
type: 'custom',
provider, provider,
uri: '/collections', {
}, type: 'custom',
(data, error) => { provider,
expect(error).toBe(424); uri: '/collections',
expect(data).toBeUndefined(); },
done(); (data, error) => {
} expect(error).toBe(424);
); expect(data).toBeUndefined();
fulfill(true);
}
);
});
}); });
it('Custom request with provider', (done) => { it('Custom request with provider', () => {
const provider = nextProvider(); return new Promise((fulfill) => {
expect( const provider = nextProvider();
addAPIProvider(provider, { expect(
resources: [host], addAPIProvider(provider, {
}) resources: [host],
).toBe(true); })
).toBe(true);
sendAPIQuery( sendAPIQuery(
provider,
{
type: 'custom',
provider, provider,
uri: '/collections', {
}, type: 'custom',
(data, error) => { provider,
expect(error).toBeUndefined(); uri: '/collections',
expect(typeof data).toBe('object'); },
done(); (data, error) => {
} expect(error).toBeUndefined();
); expect(typeof data).toBe('object');
fulfill(true);
}
);
});
}); });
it('Custom request with host', (done) => { it('Custom request with host', () => {
sendAPIQuery( return new Promise((fulfill) => {
{ sendAPIQuery(
resources: [host], {
}, resources: [host],
{ },
type: 'custom', {
uri: '/collections', type: 'custom',
}, uri: '/collections',
(data, error) => { },
expect(error).toBeUndefined(); (data, error) => {
expect(typeof data).toBe('object'); expect(error).toBeUndefined();
done(); expect(typeof data).toBe('object');
} fulfill(true);
); }
);
});
}); });
}); });

File diff suppressed because it is too large Load Diff

View File

@ -28,100 +28,88 @@ describe('Testing mock API module', () => {
}); });
// Tests // Tests
it('404 response', (done) => { it('404 response', () => {
const prefix = nextPrefix(); return new Promise((fulfill) => {
const prefix = nextPrefix();
mockAPIData({ mockAPIData({
type: 'icons', type: 'icons',
provider, provider,
prefix, prefix,
icons: ['test1', 'test2'], icons: ['test1', 'test2'],
response: 404, response: 404,
}); });
let isSync = true; let isSync = true;
loadIcons( loadIcons(
[ [
{
provider,
prefix,
name: 'test1',
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{ {
provider, provider,
prefix, prefix,
name: 'test1', name: 'test1',
}, },
]); ],
done(); (loaded, missing, pending) => {
} expect(isSync).toBe(false);
); expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'test1',
},
]);
fulfill(true);
}
);
isSync = false; isSync = false;
});
}); });
it('Load few icons', (done) => { it('Load few icons', () => {
const prefix = nextPrefix(); return new Promise((fulfill) => {
const prefix = nextPrefix();
mockAPIData({ mockAPIData({
type: 'icons', type: 'icons',
provider, provider,
prefix,
response: {
prefix, prefix,
icons: { response: {
test10: { prefix,
body: '<g />', icons: {
}, test10: {
test11: { body: '<g />',
body: '<g />', },
test11: {
body: '<g />',
},
}, },
}, },
}, });
}); mockAPIData({
mockAPIData({ type: 'icons',
type: 'icons', provider,
provider,
prefix,
response: {
prefix, prefix,
icons: { response: {
test20: { prefix,
body: '<g />', icons: {
}, test20: {
test21: { body: '<g />',
body: '<g />', },
test21: {
body: '<g />',
},
}, },
}, },
}, });
});
let isSync = true; let isSync = true;
loadIcons( loadIcons(
[ [
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
// All icons should have been loaded because API waits one tick before sending response, during which both queries are processed
expect(loaded).toEqual([
{ {
provider, provider,
prefix, prefix,
@ -132,272 +120,300 @@ describe('Testing mock API module', () => {
prefix, prefix,
name: 'test20', name: 'test20',
}, },
]); ],
expect(pending).toEqual([]); (loaded, missing, pending) => {
expect(missing).toEqual([]); expect(isSync).toBe(false);
done(); // All icons should have been loaded because API waits one tick before sending response, during which both queries are processed
} expect(loaded).toEqual([
); {
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([]);
fulfill(true);
}
);
isSync = false; isSync = false;
});
}); });
it('Load in batches and testing delay', (done) => { it('Load in batches and testing delay', () => {
const prefix = nextPrefix(); return new Promise((fulfill, reject) => {
let next: IconifyMockAPIDelayDoneCallback | undefined; const prefix = nextPrefix();
let next: IconifyMockAPIDelayDoneCallback | undefined;
mockAPIData({ mockAPIData({
type: 'icons', type: 'icons',
provider, provider,
prefix,
response: {
prefix, prefix,
icons: { response: {
test10: { prefix,
body: '<g />', icons: {
}, test10: {
test11: { body: '<g />',
body: '<g />', },
test11: {
body: '<g />',
},
}, },
}, },
}, });
}); mockAPIData({
mockAPIData({ type: 'icons',
type: 'icons', provider,
provider,
prefix,
response: {
prefix, prefix,
icons: { response: {
test20: { prefix,
body: '<g />', icons: {
}, test20: {
test21: { body: '<g />',
body: '<g />', },
test21: {
body: '<g />',
},
}, },
}, },
}, delay: (callback) => {
delay: (callback) => { next = callback;
next = callback;
},
});
let callbackCounter = 0;
loadIcons(
[
{
provider,
prefix,
name: 'test10',
}, },
{ });
provider,
prefix,
name: 'test20',
},
],
(loaded, missing, pending) => {
callbackCounter++;
switch (callbackCounter) {
case 1:
// First load: only 'test10'
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'test20',
},
]);
// Send second response let callbackCounter = 0;
expect(typeof next).toBe('function');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
next!();
break;
case 2: loadIcons(
// All icons should have been loaded [
expect(loaded).toEqual([ {
{ provider,
provider, prefix,
prefix, name: 'test10',
name: 'test10', },
}, {
{ provider,
provider, prefix,
prefix, name: 'test20',
name: 'test20', },
}, ],
]); (loaded, missing, pending) => {
expect(missing).toEqual([]); callbackCounter++;
done(); switch (callbackCounter) {
break; case 1:
// First load: only 'test10'
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'test20',
},
]);
default: // Send second response
done('Callback was called more times than expected'); expect(typeof next).toBe('function');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
next!();
break;
case 2:
// All icons should have been loaded
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
]);
expect(missing).toEqual([]);
fulfill(true);
break;
default:
reject(
'Callback was called more times than expected'
);
}
} }
} );
); });
}); });
// This is useful for testing component where loadIcons() cannot be accessed // This is useful for testing component where loadIcons() cannot be accessed
it('Using timer in callback for second test', (done) => { it('Using timer in callback for second test', () => {
const prefix = nextPrefix(); return new Promise((fulfill) => {
const name = 'test1'; const prefix = nextPrefix();
const name = 'test1';
// Mock data // Mock data
mockAPIData({ mockAPIData({
type: 'icons', type: 'icons',
provider, provider,
prefix,
response: {
prefix, prefix,
icons: { response: {
[name]: { prefix,
body: '<g />', icons: {
[name]: {
body: '<g />',
},
}, },
}, },
}, delay: (next) => {
delay: (next) => { // Icon should not be loaded yet
// Icon should not be loaded yet const storage = getStorage(provider, prefix);
const storage = getStorage(provider, prefix); expect(iconExists(storage, name)).toBe(false);
expect(iconExists(storage, name)).toBe(false);
// Set data // Set data
next(); next();
// Icon should be loaded now // Icon should be loaded now
expect(iconExists(storage, name)).toBe(true); expect(iconExists(storage, name)).toBe(true);
done(); fulfill(true);
}, },
}); });
// Load icons // Load icons
loadIcons([ loadIcons([
{
provider,
prefix,
name,
},
]);
});
it('Custom query', (done) => {
mockAPIData({
type: 'custom',
provider,
uri: '/test',
response: {
foo: true,
},
});
let isSync = true;
sendAPIQuery(
provider,
{
type: 'custom',
provider,
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: true,
});
expect(isSync).toBe(false);
done();
}
);
isSync = false;
});
it('Custom query with host', (done) => {
const host = 'http://' + nextPrefix();
setAPIModule(host, mockAPIModule);
mockAPIData({
type: 'host',
host,
uri: '/test',
response: {
foo: 2,
},
});
let isSync = true;
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: 2,
});
expect(isSync).toBe(false);
done();
}
);
isSync = false;
});
it('not_found response', (done) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
response: {
prefix,
icons: {},
not_found: ['test1', 'test2'],
},
});
let isSync = true;
loadIcons(
[
{ {
provider, provider,
prefix, prefix,
name: 'test1', name,
}, },
], ]);
(loaded, missing, pending) => { });
expect(isSync).toBe(false); });
expect(loaded).toEqual([]);
expect(pending).toEqual([]); it('Custom query', () => {
expect(missing).toEqual([ return new Promise((fulfill) => {
mockAPIData({
type: 'custom',
provider,
uri: '/test',
response: {
foo: true,
},
});
let isSync = true;
sendAPIQuery(
provider,
{
type: 'custom',
provider,
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: true,
});
expect(isSync).toBe(false);
fulfill(true);
}
);
isSync = false;
});
});
it('Custom query with host', () => {
return new Promise((fulfill) => {
const host = 'http://' + nextPrefix();
setAPIModule(host, mockAPIModule);
mockAPIData({
type: 'host',
host,
uri: '/test',
response: {
foo: 2,
},
});
let isSync = true;
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: 2,
});
expect(isSync).toBe(false);
fulfill(true);
}
);
isSync = false;
});
});
it('not_found response', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
response: {
prefix,
icons: {},
not_found: ['test1', 'test2'],
},
});
let isSync = true;
loadIcons(
[
{ {
provider, provider,
prefix, prefix,
name: 'test1', name: 'test1',
}, },
]); ],
done(); (loaded, missing, pending) => {
} expect(isSync).toBe(false);
); expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'test1',
},
]);
fulfill(true);
}
);
isSync = false; isSync = false;
});
}); });
}); });

View File

@ -14,72 +14,74 @@ describe('Testing simple names with API module', () => {
allowSimpleNames(false); allowSimpleNames(false);
}); });
it('Loading icons without prefix', (done) => { it('Loading icons without prefix', () => {
mockAPIData({ return new Promise((fulfill) => {
type: 'icons', mockAPIData({
provider: '', type: 'icons',
prefix: '', provider: '',
response: {
prefix: '', prefix: '',
icons: { response: {
test100: {
body: '<g />',
},
test101: {
body: '<g />',
},
},
},
});
mockAPIData({
type: 'icons',
provider: '',
prefix: 'test200',
response: {
prefix: 'test200',
icons: {
foo: {
body: '<g />',
},
bar: {
body: '<g />',
},
},
},
});
loadIcons(
[
{
provider: '',
prefix: '', prefix: '',
name: 'test100', icons: {
}, test100: {
{ body: '<g />',
provider: '', },
prefix: 'test200', test101: {
name: 'foo', body: '<g />',
}, },
],
(loaded, missing, pending) => {
// 'test100' should be missing because it does not have a prefix
expect(loaded).toEqual([
{
provider: '',
prefix: 'test200',
name: 'foo',
}, },
]); },
expect(pending).toEqual([]); });
expect(missing).toEqual([ mockAPIData({
type: 'icons',
provider: '',
prefix: 'test200',
response: {
prefix: 'test200',
icons: {
foo: {
body: '<g />',
},
bar: {
body: '<g />',
},
},
},
});
loadIcons(
[
{ {
provider: '', provider: '',
prefix: '', prefix: '',
name: 'test100', name: 'test100',
}, },
]); {
done(); provider: '',
} prefix: 'test200',
); name: 'foo',
},
],
(loaded, missing, pending) => {
// 'test100' should be missing because it does not have a prefix
expect(loaded).toEqual([
{
provider: '',
prefix: 'test200',
name: 'foo',
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{
provider: '',
prefix: '',
name: 'test100',
},
]);
fulfill(true);
}
);
});
}); });
}); });