2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-22 22:58:27 +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,7 +14,8 @@ describe('Testing API callbacks', () => {
); );
} }
it('Simple callback', (done) => { it('Simple callback', () => {
return new Promise((fulfill) => {
const provider = 'iconify'; const provider = 'iconify';
const prefix = nextPrefix(); const prefix = nextPrefix();
let counter = 0; let counter = 0;
@ -87,7 +88,7 @@ describe('Testing API callbacks', () => {
]); ]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
expect(storage.loaderCallbacks?.length).toBe(0); expect(storage.loaderCallbacks?.length).toBe(0);
done(); fulfill(true);
} }
}, },
sortIcons([ sortIcons([
@ -134,6 +135,7 @@ describe('Testing API callbacks', () => {
updateCallbacks(storage); updateCallbacks(storage);
}); });
}); });
});
it('Callback that should not be stored', () => { it('Callback that should not be stored', () => {
const provider = ''; const provider = '';
@ -181,7 +183,8 @@ describe('Testing API callbacks', () => {
expect(storage.loaderCallbacks).toBeUndefined(); expect(storage.loaderCallbacks).toBeUndefined();
}); });
it('Cancel callback', (done) => { it('Cancel callback', () => {
return new Promise((fulfill) => {
const provider = 'foo'; const provider = 'foo';
const prefix = nextPrefix(); const prefix = nextPrefix();
let counter = 0; let counter = 0;
@ -230,10 +233,10 @@ describe('Testing API callbacks', () => {
updateCallbacks(storage); updateCallbacks(storage);
// Unsubscribe and set timer to call done() // Unsubscribe and set timer to complete test
unsubscribe(); unsubscribe();
expect(storage.loaderCallbacks?.length).toBe(0); expect(storage.loaderCallbacks?.length).toBe(0);
setTimeout(done); setTimeout(fulfill);
}, },
sortIcons([ sortIcons([
{ {
@ -279,15 +282,23 @@ describe('Testing API callbacks', () => {
updateCallbacks(storage); updateCallbacks(storage);
}); });
}); });
});
it('Multiple prefixes', (done) => { it('Multiple prefixes', () => {
return new Promise((fulfill, reject) => {
const provider = ''; const provider = '';
const prefix1 = nextPrefix(); const prefix1 = nextPrefix();
const prefix2 = nextPrefix(); const prefix2 = nextPrefix();
let counter = 0; 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) => {
@ -338,11 +349,11 @@ describe('Testing API callbacks', () => {
// Second run - icon2 should be loaded // Second run - icon2 should be loaded
expect(storage1.loaderCallbacks?.length).toBe(0); expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0); expect(storage2.loaderCallbacks?.length).toBe(0);
done(); fulfill(true);
break; break;
default: default:
done(`Callback was called ${counter} times.`); reject(`Callback was called ${counter} times.`);
} }
}, },
sortIcons([ sortIcons([
@ -390,16 +401,24 @@ describe('Testing API callbacks', () => {
updateCallbacks(storage1); updateCallbacks(storage1);
}); });
}); });
});
it('Multiple providers', (done) => { it('Multiple providers', () => {
return new Promise((fulfill, reject) => {
const provider1 = nextPrefix(); const provider1 = nextPrefix();
const provider2 = nextPrefix(); const provider2 = nextPrefix();
const prefix1 = nextPrefix(); const prefix1 = nextPrefix();
const prefix2 = nextPrefix(); const prefix2 = nextPrefix();
let counter = 0; 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) => {
@ -451,11 +470,11 @@ describe('Testing API callbacks', () => {
expect(storage1.loaderCallbacks?.length).toBe(0); expect(storage1.loaderCallbacks?.length).toBe(0);
expect(storage2.loaderCallbacks?.length).toBe(0); expect(storage2.loaderCallbacks?.length).toBe(0);
done(); fulfill(true);
break; break;
default: default:
done(`Callback was called ${counter} times.`); reject(`Callback was called ${counter} times.`);
} }
}, },
sortIcons([ sortIcons([
@ -503,4 +522,5 @@ describe('Testing API callbacks', () => {
updateCallbacks(storage1); updateCallbacks(storage1);
}); });
}); });
});
}); });

View File

@ -23,7 +23,8 @@ describe('Testing live API with fetch', () => {
setAPIModule('', mockAPIModule); setAPIModule('', mockAPIModule);
}); });
it('Missing API configuration', (done) => { it('Missing API configuration', () => {
return new Promise((fulfill) => {
const provider = nextProvider(); const provider = nextProvider();
sendAPIQuery( sendAPIQuery(
provider, provider,
@ -35,12 +36,14 @@ describe('Testing live API with fetch', () => {
(data, error) => { (data, error) => {
expect(error).toBe(424); expect(error).toBe(424);
expect(data).toBeUndefined(); expect(data).toBeUndefined();
done(); fulfill(true);
} }
); );
}); });
});
it('Custom request with provider', (done) => { it('Custom request with provider', () => {
return new Promise((fulfill) => {
const provider = nextProvider(); const provider = nextProvider();
expect( expect(
addAPIProvider(provider, { addAPIProvider(provider, {
@ -58,12 +61,14 @@ describe('Testing live API with fetch', () => {
(data, error) => { (data, error) => {
expect(error).toBeUndefined(); expect(error).toBeUndefined();
expect(typeof data).toBe('object'); expect(typeof data).toBe('object');
done(); fulfill(true);
} }
); );
}); });
});
it('Custom request with host', (done) => { it('Custom request with host', () => {
return new Promise((fulfill) => {
sendAPIQuery( sendAPIQuery(
{ {
resources: [host], resources: [host],
@ -75,8 +80,9 @@ describe('Testing live API with fetch', () => {
(data, error) => { (data, error) => {
expect(error).toBeUndefined(); expect(error).toBeUndefined();
expect(typeof data).toBe('object'); expect(typeof data).toBe('object');
done(); fulfill(true);
} }
); );
}); });
});
}); });

View File

@ -19,7 +19,8 @@ describe('Testing API loadIcons', () => {
); );
} }
it('Loading few icons', (done) => { it('Loading few icons', () => {
return new Promise((fulfill) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
let asyncCounter = 0; let asyncCounter = 0;
@ -144,7 +145,7 @@ describe('Testing API loadIcons', () => {
false false
); );
done(); fulfill(true);
} }
); );
@ -156,6 +157,7 @@ describe('Testing API loadIcons', () => {
expect(asyncCounter).toBe(0); expect(asyncCounter).toBe(0);
asyncCounter++; asyncCounter++;
}); });
});
it('Loading one icon with Promise', async () => { it('Loading one icon with Promise', async () => {
const provider = nextPrefix(); const provider = nextPrefix();
@ -231,7 +233,8 @@ describe('Testing API loadIcons', () => {
expect(isPending({ provider, prefix, name: 'icon1' })).toBe(false); expect(isPending({ provider, prefix, name: 'icon1' })).toBe(false);
}); });
it('Loading one icon twice with Promise', (done) => { it('Loading one icon twice with Promise', () => {
return new Promise((fulfill, reject) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
@ -311,14 +314,16 @@ describe('Testing API loadIcons', () => {
// Wait for Promise // Wait for Promise
p1.then((data) => { p1.then((data) => {
expect(data.body).toEqual('<path d="" />'); expect(data.body).toEqual('<path d="" />');
done(); fulfill(true);
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
done('Failed to load icon'); reject('Failed to load icon');
});
}); });
}); });
it('Split results', (done) => { it('Split results', () => {
return new Promise((fulfill) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
@ -375,7 +380,10 @@ describe('Testing API loadIcons', () => {
expect(params).toEqual(expected); expect(params).toEqual(expected);
// Send only requested icons // Send only requested icons
const icons = Object.create(null) as Record<string, IconifyIcon>; const icons = Object.create(null) as Record<
string,
IconifyIcon
>;
params.icons.forEach((icon) => { params.icons.forEach((icon) => {
icons[icon] = { icons[icon] = {
body: '<path d="" />', body: '<path d="" />',
@ -421,12 +429,14 @@ describe('Testing API loadIcons', () => {
]); ]);
expect(missing).toEqual([]); expect(missing).toEqual([]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
done(); fulfill(true);
} }
); );
}); });
});
it('Fail on default host', (done) => { it('Fail on default host', () => {
return new Promise((fulfill, reject) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
@ -486,7 +496,7 @@ describe('Testing API loadIcons', () => {
break; break;
default: default:
done( reject(
`Unexpected additional call to sendQuery for host ${host}.` `Unexpected additional call to sendQuery for host ${host}.`
); );
} }
@ -525,12 +535,14 @@ describe('Testing API loadIcons', () => {
expect(missing).toEqual([]); expect(missing).toEqual([]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
done(); fulfill(true);
} }
); );
}); });
});
it('Fail on default host, multiple queries', (done) => { it('Fail on default host, multiple queries', () => {
return new Promise((fulfill, reject) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
@ -616,7 +628,7 @@ describe('Testing API loadIcons', () => {
break; break;
default: default:
done( reject(
`Unexpected additional call to sendQuery for host ${host}.` `Unexpected additional call to sendQuery for host ${host}.`
); );
} }
@ -684,15 +696,17 @@ describe('Testing API loadIcons', () => {
expect(missing).toEqual([]); expect(missing).toEqual([]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
done(); fulfill(true);
} }
); );
}); });
} }
); );
}); });
});
it('Fail on default host, multiple queries with different prefixes', (done) => { it('Fail on default host, multiple queries with different prefixes', () => {
return new Promise((fulfill, reject) => {
const provider = nextPrefix(); const provider = nextPrefix();
const prefix = nextPrefix(); const prefix = nextPrefix();
const prefix2 = nextPrefix(); const prefix2 = nextPrefix();
@ -782,7 +796,7 @@ describe('Testing API loadIcons', () => {
break; break;
default: default:
done( reject(
`Unexpected additional call to sendQuery for host ${host}.` `Unexpected additional call to sendQuery for host ${host}.`
); );
} }
@ -850,11 +864,12 @@ describe('Testing API loadIcons', () => {
expect(missing).toEqual([]); expect(missing).toEqual([]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
done(); fulfill(true);
} }
); );
}); });
} }
); );
}); });
});
}); });

View File

@ -28,7 +28,8 @@ describe('Testing mock API module', () => {
}); });
// Tests // Tests
it('404 response', (done) => { it('404 response', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix(); const prefix = nextPrefix();
mockAPIData({ mockAPIData({
@ -60,14 +61,16 @@ describe('Testing mock API module', () => {
name: 'test1', name: 'test1',
}, },
]); ]);
done(); fulfill(true);
} }
); );
isSync = false; isSync = false;
}); });
});
it('Load few icons', (done) => { it('Load few icons', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix(); const prefix = nextPrefix();
mockAPIData({ mockAPIData({
@ -135,14 +138,16 @@ describe('Testing mock API module', () => {
]); ]);
expect(pending).toEqual([]); expect(pending).toEqual([]);
expect(missing).toEqual([]); expect(missing).toEqual([]);
done(); fulfill(true);
} }
); );
isSync = false; isSync = false;
}); });
});
it('Load in batches and testing delay', (done) => { it('Load in batches and testing delay', () => {
return new Promise((fulfill, reject) => {
const prefix = nextPrefix(); const prefix = nextPrefix();
let next: IconifyMockAPIDelayDoneCallback | undefined; let next: IconifyMockAPIDelayDoneCallback | undefined;
@ -238,18 +243,22 @@ describe('Testing mock API module', () => {
}, },
]); ]);
expect(missing).toEqual([]); expect(missing).toEqual([]);
done(); fulfill(true);
break; break;
default: default:
done('Callback was called more times than expected'); 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', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix(); const prefix = nextPrefix();
const name = 'test1'; const name = 'test1';
@ -277,7 +286,7 @@ describe('Testing mock API module', () => {
// 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);
}, },
}); });
@ -290,8 +299,10 @@ describe('Testing mock API module', () => {
}, },
]); ]);
}); });
});
it('Custom query', (done) => { it('Custom query', () => {
return new Promise((fulfill) => {
mockAPIData({ mockAPIData({
type: 'custom', type: 'custom',
provider, provider,
@ -316,14 +327,16 @@ describe('Testing mock API module', () => {
foo: true, foo: true,
}); });
expect(isSync).toBe(false); expect(isSync).toBe(false);
done(); fulfill(true);
} }
); );
isSync = false; isSync = false;
}); });
});
it('Custom query with host', (done) => { it('Custom query with host', () => {
return new Promise((fulfill) => {
const host = 'http://' + nextPrefix(); const host = 'http://' + nextPrefix();
setAPIModule(host, mockAPIModule); setAPIModule(host, mockAPIModule);
mockAPIData({ mockAPIData({
@ -351,14 +364,16 @@ describe('Testing mock API module', () => {
foo: 2, foo: 2,
}); });
expect(isSync).toBe(false); expect(isSync).toBe(false);
done(); fulfill(true);
} }
); );
isSync = false; isSync = false;
}); });
});
it('not_found response', (done) => { it('not_found response', () => {
return new Promise((fulfill) => {
const prefix = nextPrefix(); const prefix = nextPrefix();
mockAPIData({ mockAPIData({
@ -394,10 +409,11 @@ describe('Testing mock API module', () => {
name: 'test1', name: 'test1',
}, },
]); ]);
done(); fulfill(true);
} }
); );
isSync = false; isSync = false;
}); });
});
}); });

View File

@ -14,7 +14,8 @@ describe('Testing simple names with API module', () => {
allowSimpleNames(false); allowSimpleNames(false);
}); });
it('Loading icons without prefix', (done) => { it('Loading icons without prefix', () => {
return new Promise((fulfill) => {
mockAPIData({ mockAPIData({
type: 'icons', type: 'icons',
provider: '', provider: '',
@ -78,8 +79,9 @@ describe('Testing simple names with API module', () => {
name: 'test100', name: 'test100',
}, },
]); ]);
done(); fulfill(true);
} }
); );
}); });
});
}); });