diff --git a/packages/core/tests/api/callbacks-test.ts b/packages/core/tests/api/callbacks-test.ts index c1420e3..998841f 100644 --- a/packages/core/tests/api/callbacks-test.ts +++ b/packages/core/tests/api/callbacks-test.ts @@ -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: '', + 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: '', + }, + }, + }); - 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: '', + 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: '', + }, + }, + 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: '', + }, + }, + }); + + 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: '', }, }, + 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: '', - }, - }, - 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: '', + 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: '', + }, + }, + }); - 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: '', + 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: '', + }, + }, + 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: '', + 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: '', + }, + }, + }); - 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: '', + 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: '', + }, + }, + not_found: ['icon3'], + }); + updateCallbacks(storage1); + }); }); }); }); diff --git a/packages/core/tests/api/fetch-test.ts b/packages/core/tests/api/fetch-test.ts index ac29ba6..b161d46 100644 --- a/packages/core/tests/api/fetch-test.ts +++ b/packages/core/tests/api/fetch-test.ts @@ -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); + } + ); + }); }); }); diff --git a/packages/core/tests/api/loading-test.ts b/packages/core/tests/api/loading-test.ts index e06ce77..e1018b1 100644 --- a/packages/core/tests/api/loading-test.ts +++ b/packages/core/tests/api/loading-test.ts @@ -19,142 +19,144 @@ describe('Testing API loadIcons', () => { ); } - it('Loading few icons', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); - let asyncCounter = 0; + it('Loading few icons', () => { + return new Promise((fulfill) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); + let asyncCounter = 0; - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - }); - - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - const item: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons, - }; - - // This callback should be called first - expect(asyncCounter).toBe(1); - asyncCounter++; - - // Test input and return as one item - const expected: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons: ['icon1', 'icon2'], - }; - expect(item).toEqual(expected); - - return [item]; - }; - - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - // This callback should be called after prepareQuery - expect(asyncCounter).toBe(2); - asyncCounter++; - - expect(params.type).toBe('icons'); - - // Test input - expect(host).toBe('https://api1.local'); - const expected: IconifyAPIQueryParams = { - type: 'icons', - provider, - prefix, - icons: ['icon1', 'icon2'], - }; - expect(params).toEqual(expected); - - // Send data - callback('success', { - prefix, - icons: { - icon1: { - body: '', - }, - icon2: { - body: '', - }, - }, + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], }); - // Counter should not have increased after status.done() call becuse parsing result should be done on next tick - expect(asyncCounter).toBe(3); - }; - - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icons - loadIcons( - [ - // as icon - { + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { + const item: IconifyAPIIconsQueryParams = { + type: 'icons', provider, prefix, - name: 'icon1', - }, - // as string - provider + ':' + prefix + ':icon2', - ], - (loaded, missing, pending) => { - // This callback should be called last - expect(asyncCounter).toBe(3); + icons, + }; + + // This callback should be called first + expect(asyncCounter).toBe(1); asyncCounter++; - expect(loaded).toEqual([ + // Test input and return as one item + const expected: IconifyAPIIconsQueryParams = { + type: 'icons', + provider, + prefix, + icons: ['icon1', 'icon2'], + }; + expect(item).toEqual(expected); + + return [item]; + }; + + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + // This callback should be called after prepareQuery + expect(asyncCounter).toBe(2); + asyncCounter++; + + expect(params.type).toBe('icons'); + + // Test input + expect(host).toBe('https://api1.local'); + const expected: IconifyAPIQueryParams = { + type: 'icons', + provider, + prefix, + icons: ['icon1', 'icon2'], + }; + expect(params).toEqual(expected); + + // Send data + callback('success', { + prefix, + icons: { + icon1: { + body: '', + }, + icon2: { + body: '', + }, + }, + }); + + // Counter should not have increased after status.done() call becuse parsing result should be done on next tick + expect(asyncCounter).toBe(3); + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icons + loadIcons( + [ + // as icon { provider, prefix, name: 'icon1', }, - { - provider, - prefix, - name: 'icon2', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); + // as string + provider + ':' + prefix + ':icon2', + ], + (loaded, missing, pending) => { + // This callback should be called last + expect(asyncCounter).toBe(3); + asyncCounter++; - expect( - isPending({ - provider, - prefix, - name: 'icon1', - }) - ).toBe(false); - expect(isPending({ provider, prefix, name: 'icon3' })).toBe( - false - ); + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon1', + }, + { + provider, + prefix, + name: 'icon2', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); - done(); - } - ); + expect( + isPending({ + provider, + prefix, + name: 'icon1', + }) + ).toBe(false); + expect(isPending({ provider, prefix, name: 'icon3' })).toBe( + false + ); - // Test isPending - expect(isPending({ provider, prefix, name: 'icon1' })).toBe(true); - expect(isPending({ provider, prefix, name: 'icon3' })).toBe(false); + fulfill(true); + } + ); - // Make sure asyncCounter wasn't increased because loading shoud happen on next tick - expect(asyncCounter).toBe(0); - asyncCounter++; + // Test isPending + expect(isPending({ provider, prefix, name: 'icon1' })).toBe(true); + expect(isPending({ provider, prefix, name: 'icon3' })).toBe(false); + + // Make sure asyncCounter wasn't increased because loading shoud happen on next tick + expect(asyncCounter).toBe(0); + asyncCounter++; + }); }); it('Loading one icon with Promise', async () => { @@ -231,630 +233,643 @@ describe('Testing API loadIcons', () => { expect(isPending({ provider, prefix, name: 'icon1' })).toBe(false); }); - it('Loading one icon twice with Promise', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); + it('Loading one icon twice with Promise', () => { + return new Promise((fulfill, reject) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - }); - - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - const item: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons, - }; - - // Test input and return as one item - const expected: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons: ['icon1'], - }; - expect(item).toEqual(expected); - - return [item]; - }; - - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - expect(params.type).toBe('icons'); - - // Test input - expect(host).toBe('https://api1.local'); - const expected: IconifyAPIQueryParams = { - type: 'icons', - provider, - prefix, - icons: ['icon1'], - }; - expect(params).toEqual(expected); - - // Send data - callback('success', { - prefix, - icons: { - icon1: { - body: '', - }, - }, + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], }); - }; - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icon, twice - const p1 = loadIcon(provider + ':' + prefix + ':icon1'); - const p2 = loadIcon(provider + ':' + prefix + ':icon1'); - - // Promise instances should be the same because parameter is a string that is cached - expect(p1).toEqual(p2); - - // Test isPending - expect(isPending({ provider, prefix, name: 'icon1' })).toBe(true); - - // Wait for Promise - p1.then((data) => { - expect(data.body).toEqual(''); - done(); - }).catch((err) => { - console.error(err); - done('Failed to load icon'); - }); - }); - - it('Split results', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); - - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - }); - - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - // Split all icons in multiple queries, one icon per query - const results: IconifyAPIIconsQueryParams[] = []; - icons.forEach((icon) => { + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { const item: IconifyAPIIconsQueryParams = { type: 'icons', provider, prefix, - icons: [icon], + icons, }; - results.push(item); - }); - expect(results.length).toBe(2); - - return results; - }; - - let queryCounter = 0; - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - // Test input - expect(host).toBe('https://api1.local'); - - expect(params.type).toBe('icons'); - if (params.type !== 'icons') { - return; - } - - // Icon names should match queryCounter: 'icon1' on first run, 'icon2' on second run - queryCounter++; - const expected: IconifyAPIQueryParams = { - type: 'icons', - provider, - prefix, - icons: ['icon' + queryCounter.toString()], - }; - expect(params).toEqual(expected); - - // Send only requested icons - const icons = Object.create(null) as Record; - params.icons.forEach((icon) => { - icons[icon] = { - body: '', + // Test input and return as one item + const expected: IconifyAPIIconsQueryParams = { + type: 'icons', + provider, + prefix, + icons: ['icon1'], }; - }); - callback('success', { - prefix, - icons, - // Test mismatched provider: should be ignored because provider name is not affected by actual API response - provider: nextPrefix(), - }); - }; + expect(item).toEqual(expected); - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icons - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix + ':icon1', - provider + ':' + prefix + ':icon2', - ], - (loaded, missing, pending) => { - // Callback should be called only once because results should be sent in same tick - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { - provider, - prefix, - name: 'icon1', - }, - { - provider, - prefix, - name: 'icon2', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - done(); - } - ); - }); - - it('Fail on default host', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); - - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - rotate: 100, // 100ms to speed up test - }); - - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - const item: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons, + return [item]; }; - return [item]; - }; - let queryCounter = 0; - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - queryCounter++; - params; - switch (queryCounter) { - case 1: - // First call on api1 - expect(host).toBe('https://api1.local'); + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + expect(params.type).toBe('icons'); - // Do nothing - fake failed response - break; + // Test input + expect(host).toBe('https://api1.local'); + const expected: IconifyAPIQueryParams = { + type: 'icons', + provider, + prefix, + icons: ['icon1'], + }; + expect(params).toEqual(expected); - case 2: - // First call on api2 - expect(host).toBe('https://api2.local'); - - // Return result - callback('success', { - prefix, - icons: { - icon1: { - body: '', - }, - icon2: { - body: '', - }, + // Send data + callback('success', { + prefix, + icons: { + icon1: { + body: '', }, - }); - break; - - default: - done( - `Unexpected additional call to sendQuery for host ${host}.` - ); - } - }; - - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icons - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix + ':icon1', - provider + ':' + prefix + ':icon2', - ], - (loaded, missing, pending) => { - // Callback should be called only once - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { - provider, - prefix, - name: 'icon1', }, - { - provider, - prefix, - name: 'icon2', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - - done(); - } - ); - }); - - it('Fail on default host, multiple queries', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); - - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - rotate: 100, // 100ms to speed up test - }); - - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - const item: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons, - }; - return [item]; - }; - - let queryCounter = 0; - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - queryCounter++; - - expect(params.type).toBe('icons'); - if (params.type !== 'icons') { - return; - } - - switch (queryCounter) { - case 1: - // First call on api1 - expect(params.icons).toEqual(['icon1', 'icon2']); - expect(host).toBe('https://api1.local'); - - // Do nothing - fake failed response - break; - - case 2: - // First call on api2 - expect(params.icons).toEqual(['icon1', 'icon2']); - expect(host).toBe('https://api2.local'); - - // Return result - callback('success', { - prefix, - icons: { - icon1: { - body: '', - }, - icon2: { - body: '', - }, - }, - }); - break; - - case 3: - // Second call, should have api2 as default - expect(params.icons).toEqual(['icon3', 'icon4']); - expect(host).toBe('https://api2.local'); - - // Return result - callback('success', { - prefix, - icons: { - icon3: { - body: '', - }, - icon4: { - body: '', - }, - }, - }); - break; - - default: - done( - `Unexpected additional call to sendQuery for host ${host}.` - ); - } - }; - - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icons - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix + ':icon1', - provider + ':' + prefix + ':icon2', - ], - (loaded, missing, pending) => { - // Callback should be called only once - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { - provider, - prefix, - name: 'icon1', - }, - { - provider, - prefix, - name: 'icon2', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - - // Send another query on next tick - setTimeout(() => { - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix + ':icon3', - provider + ':' + prefix + ':icon4', - ], - (loaded, missing, pending) => { - // Callback should be called only once - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { - provider, - prefix, - name: 'icon3', - }, - { - provider, - prefix, - name: 'icon4', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - - done(); - } - ); }); - } - ); + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icon, twice + const p1 = loadIcon(provider + ':' + prefix + ':icon1'); + const p2 = loadIcon(provider + ':' + prefix + ':icon1'); + + // Promise instances should be the same because parameter is a string that is cached + expect(p1).toEqual(p2); + + // Test isPending + expect(isPending({ provider, prefix, name: 'icon1' })).toBe(true); + + // Wait for Promise + p1.then((data) => { + expect(data.body).toEqual(''); + fulfill(true); + }).catch((err) => { + console.error(err); + reject('Failed to load icon'); + }); + }); }); - it('Fail on default host, multiple queries with different prefixes', (done) => { - const provider = nextPrefix(); - const prefix = nextPrefix(); - const prefix2 = nextPrefix(); + it('Split results', () => { + return new Promise((fulfill) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); - // Set config - addAPIProvider(provider, { - resources: ['https://api1.local', 'https://api2.local'], - rotate: 100, // 100ms to speed up test - }); + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], + }); - // Icon loader - const prepareQuery = ( - provider: string, - prefix: string, - icons: string[] - ): IconifyAPIIconsQueryParams[] => { - const item: IconifyAPIIconsQueryParams = { - type: 'icons', - provider, - prefix, - icons, - }; - return [item]; - }; - - let queryCounter = 0; - const sendQuery = ( - host: string, - params: IconifyAPIQueryParams, - callback: QueryModuleResponse - ): void => { - queryCounter++; - - expect(params.type).toBe('icons'); - if (params.type !== 'icons') { - return; - } - - switch (queryCounter) { - case 1: - // First call on api1 - expect(params.prefix).toBe(prefix); - expect(params.icons).toEqual(['icon1', 'icon2']); - expect(host).toBe('https://api1.local'); - - // Do nothing - fake failed response - break; - - case 2: - // First call on api2 - expect(params.prefix).toBe(prefix); - expect(params.icons).toEqual(['icon1', 'icon2']); - expect(host).toBe('https://api2.local'); - - // Return result - callback('success', { - prefix: params.prefix, - icons: { - icon1: { - body: '', - }, - icon2: { - body: '', - }, - }, - }); - break; - - case 3: - // Second call, should have api2 as default - expect(params.prefix).toBe(prefix2); - expect(params.icons).toEqual(['icon2', 'icon4']); - expect(host).toBe('https://api2.local'); - - // Return result - callback('success', { - prefix: params.prefix, - icons: { - icon2: { - body: '', - }, - icon4: { - body: '', - }, - }, - }); - break; - - default: - done( - `Unexpected additional call to sendQuery for host ${host}.` - ); - } - }; - - setAPIModule(provider, { - prepare: prepareQuery, - send: sendQuery, - }); - - // Load icons - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix + ':icon1', - provider + ':' + prefix + ':icon2', - ], - (loaded, missing, pending) => { - // Callback should be called only once - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { + // Split all icons in multiple queries, one icon per query + const results: IconifyAPIIconsQueryParams[] = []; + icons.forEach((icon) => { + const item: IconifyAPIIconsQueryParams = { + type: 'icons', provider, prefix, - name: 'icon1', - }, - { - provider, - prefix, - name: 'icon2', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - - // Send another query on next tick for different prefix that shares configuration - setTimeout(() => { - let callbackCalled = false; - loadIcons( - [ - provider + ':' + prefix2 + ':icon2', - provider + ':' + prefix2 + ':icon4', - ], - (loaded, missing, pending) => { - // Callback should be called only once - expect(callbackCalled).toBe(false); - callbackCalled = true; - - // Test data - expect(loaded).toEqual([ - { - provider, - prefix: prefix2, - name: 'icon2', - }, - { - provider, - prefix: prefix2, - name: 'icon4', - }, - ]); - expect(missing).toEqual([]); - expect(pending).toEqual([]); - - done(); - } - ); + icons: [icon], + }; + results.push(item); }); - } - ); + + expect(results.length).toBe(2); + + return results; + }; + + let queryCounter = 0; + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + // Test input + expect(host).toBe('https://api1.local'); + + expect(params.type).toBe('icons'); + if (params.type !== 'icons') { + return; + } + + // Icon names should match queryCounter: 'icon1' on first run, 'icon2' on second run + queryCounter++; + const expected: IconifyAPIQueryParams = { + type: 'icons', + provider, + prefix, + icons: ['icon' + queryCounter.toString()], + }; + expect(params).toEqual(expected); + + // Send only requested icons + const icons = Object.create(null) as Record< + string, + IconifyIcon + >; + params.icons.forEach((icon) => { + icons[icon] = { + body: '', + }; + }); + callback('success', { + prefix, + icons, + // Test mismatched provider: should be ignored because provider name is not affected by actual API response + provider: nextPrefix(), + }); + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icons + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], + (loaded, missing, pending) => { + // Callback should be called only once because results should be sent in same tick + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon1', + }, + { + provider, + prefix, + name: 'icon2', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + fulfill(true); + } + ); + }); + }); + + it('Fail on default host', () => { + return new Promise((fulfill, reject) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); + + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], + rotate: 100, // 100ms to speed up test + }); + + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { + const item: IconifyAPIIconsQueryParams = { + type: 'icons', + provider, + prefix, + icons, + }; + return [item]; + }; + + let queryCounter = 0; + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + queryCounter++; + params; + switch (queryCounter) { + case 1: + // First call on api1 + expect(host).toBe('https://api1.local'); + + // Do nothing - fake failed response + break; + + case 2: + // First call on api2 + expect(host).toBe('https://api2.local'); + + // Return result + callback('success', { + prefix, + icons: { + icon1: { + body: '', + }, + icon2: { + body: '', + }, + }, + }); + break; + + default: + reject( + `Unexpected additional call to sendQuery for host ${host}.` + ); + } + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icons + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], + (loaded, missing, pending) => { + // Callback should be called only once + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon1', + }, + { + provider, + prefix, + name: 'icon2', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + + fulfill(true); + } + ); + }); + }); + + it('Fail on default host, multiple queries', () => { + return new Promise((fulfill, reject) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); + + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], + rotate: 100, // 100ms to speed up test + }); + + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { + const item: IconifyAPIIconsQueryParams = { + type: 'icons', + provider, + prefix, + icons, + }; + return [item]; + }; + + let queryCounter = 0; + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + queryCounter++; + + expect(params.type).toBe('icons'); + if (params.type !== 'icons') { + return; + } + + switch (queryCounter) { + case 1: + // First call on api1 + expect(params.icons).toEqual(['icon1', 'icon2']); + expect(host).toBe('https://api1.local'); + + // Do nothing - fake failed response + break; + + case 2: + // First call on api2 + expect(params.icons).toEqual(['icon1', 'icon2']); + expect(host).toBe('https://api2.local'); + + // Return result + callback('success', { + prefix, + icons: { + icon1: { + body: '', + }, + icon2: { + body: '', + }, + }, + }); + break; + + case 3: + // Second call, should have api2 as default + expect(params.icons).toEqual(['icon3', 'icon4']); + expect(host).toBe('https://api2.local'); + + // Return result + callback('success', { + prefix, + icons: { + icon3: { + body: '', + }, + icon4: { + body: '', + }, + }, + }); + break; + + default: + reject( + `Unexpected additional call to sendQuery for host ${host}.` + ); + } + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icons + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], + (loaded, missing, pending) => { + // Callback should be called only once + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon1', + }, + { + provider, + prefix, + name: 'icon2', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + + // Send another query on next tick + setTimeout(() => { + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix + ':icon3', + provider + ':' + prefix + ':icon4', + ], + (loaded, missing, pending) => { + // Callback should be called only once + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon3', + }, + { + provider, + prefix, + name: 'icon4', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + + fulfill(true); + } + ); + }); + } + ); + }); + }); + + it('Fail on default host, multiple queries with different prefixes', () => { + return new Promise((fulfill, reject) => { + const provider = nextPrefix(); + const prefix = nextPrefix(); + const prefix2 = nextPrefix(); + + // Set config + addAPIProvider(provider, { + resources: ['https://api1.local', 'https://api2.local'], + rotate: 100, // 100ms to speed up test + }); + + // Icon loader + const prepareQuery = ( + provider: string, + prefix: string, + icons: string[] + ): IconifyAPIIconsQueryParams[] => { + const item: IconifyAPIIconsQueryParams = { + type: 'icons', + provider, + prefix, + icons, + }; + return [item]; + }; + + let queryCounter = 0; + const sendQuery = ( + host: string, + params: IconifyAPIQueryParams, + callback: QueryModuleResponse + ): void => { + queryCounter++; + + expect(params.type).toBe('icons'); + if (params.type !== 'icons') { + return; + } + + switch (queryCounter) { + case 1: + // First call on api1 + expect(params.prefix).toBe(prefix); + expect(params.icons).toEqual(['icon1', 'icon2']); + expect(host).toBe('https://api1.local'); + + // Do nothing - fake failed response + break; + + case 2: + // First call on api2 + expect(params.prefix).toBe(prefix); + expect(params.icons).toEqual(['icon1', 'icon2']); + expect(host).toBe('https://api2.local'); + + // Return result + callback('success', { + prefix: params.prefix, + icons: { + icon1: { + body: '', + }, + icon2: { + body: '', + }, + }, + }); + break; + + case 3: + // Second call, should have api2 as default + expect(params.prefix).toBe(prefix2); + expect(params.icons).toEqual(['icon2', 'icon4']); + expect(host).toBe('https://api2.local'); + + // Return result + callback('success', { + prefix: params.prefix, + icons: { + icon2: { + body: '', + }, + icon4: { + body: '', + }, + }, + }); + break; + + default: + reject( + `Unexpected additional call to sendQuery for host ${host}.` + ); + } + }; + + setAPIModule(provider, { + prepare: prepareQuery, + send: sendQuery, + }); + + // Load icons + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix + ':icon1', + provider + ':' + prefix + ':icon2', + ], + (loaded, missing, pending) => { + // Callback should be called only once + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix, + name: 'icon1', + }, + { + provider, + prefix, + name: 'icon2', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + + // Send another query on next tick for different prefix that shares configuration + setTimeout(() => { + let callbackCalled = false; + loadIcons( + [ + provider + ':' + prefix2 + ':icon2', + provider + ':' + prefix2 + ':icon4', + ], + (loaded, missing, pending) => { + // Callback should be called only once + expect(callbackCalled).toBe(false); + callbackCalled = true; + + // Test data + expect(loaded).toEqual([ + { + provider, + prefix: prefix2, + name: 'icon2', + }, + { + provider, + prefix: prefix2, + name: 'icon4', + }, + ]); + expect(missing).toEqual([]); + expect(pending).toEqual([]); + + fulfill(true); + } + ); + }); + } + ); + }); }); }); diff --git a/packages/core/tests/api/mock-test.ts b/packages/core/tests/api/mock-test.ts index ddad0b3..4735270 100644 --- a/packages/core/tests/api/mock-test.ts +++ b/packages/core/tests/api/mock-test.ts @@ -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: '', - }, - test11: { - body: '', + response: { + prefix, + icons: { + test10: { + body: '', + }, + test11: { + body: '', + }, }, }, - }, - }); - mockAPIData({ - type: 'icons', - provider, - prefix, - response: { + }); + mockAPIData({ + type: 'icons', + provider, prefix, - icons: { - test20: { - body: '', - }, - test21: { - body: '', + response: { + prefix, + icons: { + test20: { + body: '', + }, + test21: { + body: '', + }, }, }, - }, - }); + }); - 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: '', - }, - test11: { - body: '', + response: { + prefix, + icons: { + test10: { + body: '', + }, + test11: { + body: '', + }, }, }, - }, - }); - mockAPIData({ - type: 'icons', - provider, - prefix, - response: { + }); + mockAPIData({ + type: 'icons', + provider, prefix, - icons: { - test20: { - body: '', - }, - test21: { - body: '', + response: { + prefix, + icons: { + test20: { + body: '', + }, + test21: { + body: '', + }, }, }, - }, - 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: '', + response: { + prefix, + icons: { + [name]: { + body: '', + }, }, }, - }, - 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; + }); }); }); diff --git a/packages/core/tests/api/simple-names-test.ts b/packages/core/tests/api/simple-names-test.ts index bd3124e..6e85d7d 100644 --- a/packages/core/tests/api/simple-names-test.ts +++ b/packages/core/tests/api/simple-names-test.ts @@ -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: '', - }, - test101: { - body: '', - }, - }, - }, - }); - mockAPIData({ - type: 'icons', - provider: '', - prefix: 'test200', - response: { - prefix: 'test200', - icons: { - foo: { - body: '', - }, - bar: { - body: '', - }, - }, - }, - }); - - 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: '', + }, + test101: { + body: '', + }, }, - ]); - expect(pending).toEqual([]); - expect(missing).toEqual([ + }, + }); + mockAPIData({ + type: 'icons', + provider: '', + prefix: 'test200', + response: { + prefix: 'test200', + icons: { + foo: { + body: '', + }, + bar: { + body: '', + }, + }, + }, + }); + + 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); + } + ); + }); }); });