2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-06 07:20:40 +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) => {
const provider = 'iconify';
const prefix = nextPrefix();
let counter = 0;
it('Simple callback', () => {
return new Promise((fulfill) => {
const provider = 'iconify';
const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
const storage = getStorage(provider, prefix) 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,
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>',
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
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);
updateCallbacks(storage);
return;
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2:
// Second run - icon2 should be added, completing callback
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([]);
expect(storage.loaderCallbacks?.length).toBe(0);
done();
}
},
sortIcons([
{
provider,
prefix,
name: 'icon1',
updateCallbacks(storage);
return;
case 2:
// Second run - icon2 should be added, completing callback
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([]);
expect(storage.loaderCallbacks?.length).toBe(0);
fulfill(true);
}
},
{
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>',
sortIcons([
{
provider,
prefix,
name: 'icon1',
},
},
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);
// 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();
});
it('Cancel callback', (done) => {
const provider = 'foo';
const prefix = nextPrefix();
let counter = 0;
it('Cancel callback', () => {
return new Promise((fulfill) => {
const provider = 'foo';
const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
const storage = getStorage(provider, prefix) as IconStorageWithAPI;
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
expect(counter).toBe(1);
counter++;
expect(counter).toBe(1);
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
// First run - icon1 should be loaded, icon3 should be missing
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,
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
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, {
prefix: prefix,
icons: {
icon2: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
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) => {
const provider = '';
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
it('Multiple prefixes', () => {
return new Promise((fulfill, reject) => {
const provider = '';
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider, prefix1) as IconStorageWithAPI;
const storage2 = getStorage(provider, prefix2) as IconStorageWithAPI;
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);
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>',
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);
updateCallbacks(storage2);
break;
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
done();
break;
updateCallbacks(storage2);
break;
default:
done(`Callback was called ${counter} times.`);
}
},
sortIcons([
{
provider,
prefix: prefix1,
name: 'icon1',
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
fulfill(true);
break;
default:
reject(`Callback was called ${counter} times.`);
}
},
{
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>',
sortIcons([
{
provider,
prefix: prefix1,
name: 'icon1',
},
},
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);
// 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) => {
const provider1 = nextPrefix();
const provider2 = nextPrefix();
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
it('Multiple providers', () => {
return new Promise((fulfill, reject) => {
const provider1 = nextPrefix();
const provider2 = nextPrefix();
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider1, prefix1) as IconStorageWithAPI;
const storage2 = getStorage(provider2, prefix2) as IconStorageWithAPI;
const storage1 = getStorage(
provider1,
prefix1
) as IconStorageWithAPI;
const storage2 = getStorage(
provider2,
prefix2
) as IconStorageWithAPI;
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
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: provider1,
prefix: prefix1,
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>',
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider: provider1,
prefix: prefix1,
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);
updateCallbacks(storage2);
break;
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
updateCallbacks(storage2);
break;
done();
break;
case 2:
// Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0);
default:
done(`Callback was called ${counter} times.`);
}
},
sortIcons([
{
provider: provider1,
prefix: prefix1,
name: 'icon1',
fulfill(true);
break;
default:
reject(`Callback was called ${counter} times.`);
}
},
{
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);
// 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>',
sortIcons([
{
provider: provider1,
prefix: prefix1,
name: 'icon1',
},
},
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);
// 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);
});
it('Missing API configuration', (done) => {
const provider = nextProvider();
sendAPIQuery(
provider,
{
type: 'custom',
it('Missing API configuration', () => {
return new Promise((fulfill) => {
const provider = nextProvider();
sendAPIQuery(
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBe(424);
expect(data).toBeUndefined();
done();
}
);
{
type: 'custom',
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBe(424);
expect(data).toBeUndefined();
fulfill(true);
}
);
});
});
it('Custom request with provider', (done) => {
const provider = nextProvider();
expect(
addAPIProvider(provider, {
resources: [host],
})
).toBe(true);
it('Custom request with provider', () => {
return new Promise((fulfill) => {
const provider = nextProvider();
expect(
addAPIProvider(provider, {
resources: [host],
})
).toBe(true);
sendAPIQuery(
provider,
{
type: 'custom',
sendAPIQuery(
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
expect(typeof data).toBe('object');
done();
}
);
{
type: 'custom',
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
expect(typeof data).toBe('object');
fulfill(true);
}
);
});
});
it('Custom request with host', (done) => {
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
expect(typeof data).toBe('object');
done();
}
);
it('Custom request with host', () => {
return new Promise((fulfill) => {
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
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
it('404 response', (done) => {
const prefix = nextPrefix();
it('404 response', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
response: 404,
});
mockAPIData({
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
response: 404,
});
let isSync = true;
let isSync = true;
loadIcons(
[
{
provider,
prefix,
name: 'test1',
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
loadIcons(
[
{
provider,
prefix,
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) => {
const prefix = nextPrefix();
it('Load few icons', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
mockAPIData({
type: 'icons',
provider,
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
response: {
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
},
},
},
},
});
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
});
mockAPIData({
type: 'icons',
provider,
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
response: {
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
},
},
},
},
});
});
let isSync = true;
let isSync = true;
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([
loadIcons(
[
{
provider,
prefix,
@ -132,272 +120,300 @@ describe('Testing mock API module', () => {
prefix,
name: 'test20',
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([]);
done();
}
);
],
(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,
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) => {
const prefix = nextPrefix();
let next: IconifyMockAPIDelayDoneCallback | undefined;
it('Load in batches and testing delay', () => {
return new Promise((fulfill, reject) => {
const prefix = nextPrefix();
let next: IconifyMockAPIDelayDoneCallback | undefined;
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
mockAPIData({
type: 'icons',
provider,
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
response: {
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
},
},
},
},
});
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
});
mockAPIData({
type: 'icons',
provider,
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
response: {
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
},
},
},
},
delay: (callback) => {
next = callback;
},
});
let callbackCounter = 0;
loadIcons(
[
{
provider,
prefix,
name: 'test10',
delay: (callback) => {
next = callback;
},
{
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
expect(typeof next).toBe('function');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
next!();
break;
let callbackCounter = 0;
case 2:
// All icons should have been loaded
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
]);
expect(missing).toEqual([]);
done();
break;
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',
},
]);
default:
done('Callback was called more times than expected');
// Send second response
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
it('Using timer in callback for second test', (done) => {
const prefix = nextPrefix();
const name = 'test1';
it('Using timer in callback for second test', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix();
const name = 'test1';
// Mock data
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
// Mock data
mockAPIData({
type: 'icons',
provider,
prefix,
icons: {
[name]: {
body: '<g />',
response: {
prefix,
icons: {
[name]: {
body: '<g />',
},
},
},
},
delay: (next) => {
// Icon should not be loaded yet
const storage = getStorage(provider, prefix);
expect(iconExists(storage, name)).toBe(false);
delay: (next) => {
// Icon should not be loaded yet
const storage = getStorage(provider, prefix);
expect(iconExists(storage, name)).toBe(false);
// Set data
next();
// Set data
next();
// Icon should be loaded now
expect(iconExists(storage, name)).toBe(true);
// Icon should be loaded now
expect(iconExists(storage, name)).toBe(true);
done();
},
});
fulfill(true);
},
});
// Load icons
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(
[
// Load icons
loadIcons([
{
provider,
prefix,
name: 'test1',
name,
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
]);
});
});
it('Custom query', () => {
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,
prefix,
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);
});
it('Loading icons without prefix', (done) => {
mockAPIData({
type: 'icons',
provider: '',
prefix: '',
response: {
it('Loading icons without prefix', () => {
return new Promise((fulfill) => {
mockAPIData({
type: 'icons',
provider: '',
prefix: '',
icons: {
test100: {
body: '<g />',
},
test101: {
body: '<g />',
},
},
},
});
mockAPIData({
type: 'icons',
provider: '',
prefix: 'test200',
response: {
prefix: 'test200',
icons: {
foo: {
body: '<g />',
},
bar: {
body: '<g />',
},
},
},
});
loadIcons(
[
{
provider: '',
response: {
prefix: '',
name: 'test100',
},
{
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',
icons: {
test100: {
body: '<g />',
},
test101: {
body: '<g />',
},
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([
},
});
mockAPIData({
type: 'icons',
provider: '',
prefix: 'test200',
response: {
prefix: 'test200',
icons: {
foo: {
body: '<g />',
},
bar: {
body: '<g />',
},
},
},
});
loadIcons(
[
{
provider: '',
prefix: '',
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);
}
);
});
});
});