import mocha from 'mocha'; import chai from 'chai'; import { FakeData, setFakeData, prepareQuery, sendQuery } from './fake-api'; import { setAPIModule } from '@iconify/core/lib/api/modules'; import { addAPIProvider } from '@iconify/core/lib/api/config'; import { loadIcons } from '@iconify/core/lib/api/icons'; const expect = chai.expect; let prefixCounter = 0; function nextPrefix(): string { return 'fake-api-' + prefixCounter++; } describe('Testing fake API', () => { before(() => { setAPIModule('', { prepare: prepareQuery, send: sendQuery, }); }); it('Loading results', (done) => { const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1', 'icon2'], data: { prefix, icons: { icon1: { body: '', }, icon2: { body: '', }, }, width: 24, height: 24, }, }; addAPIProvider(provider, { resources: ['https://api1.local', 'https://api2.local'], }); setFakeData(provider, prefix, data); // Attempt to load icons loadIcons( [ provider + ':' + prefix + ':icon1', provider + ':' + prefix + ':icon2', ], (loaded, missing, pending) => { expect(loaded).to.be.eql([ { provider, prefix, name: 'icon1', }, { provider, prefix, name: 'icon2', }, ]); done(); } ); }); it('Loading results with delay', (done) => { const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1', 'icon2'], delay: 100, data: { prefix, icons: { icon1: { body: '', }, icon2: { body: '', }, }, width: 24, height: 24, }, }; addAPIProvider(provider, { resources: ['https://api1.local', 'https://api2.local'], }); setFakeData(provider, prefix, data); // Attempt to load icons const start = Date.now(); loadIcons( [ { provider, prefix, name: 'icon1', }, { provider, prefix, name: 'icon2', }, ], (loaded, missing, pending) => { expect(loaded).to.be.eql([ { provider, prefix, name: 'icon1', }, { provider, prefix, name: 'icon2', }, ]); const end = Date.now(); expect(end - start).to.be.at.least(50); expect(end - start).to.be.at.most(150); done(); } ); }); it('Loading partial results', (done) => { const provider = nextPrefix(); const prefix = nextPrefix(); const data: FakeData = { icons: ['icon1'], delay: 20, data: { prefix, icons: { icon1: { body: '', }, }, width: 24, height: 24, }, }; addAPIProvider(provider, { resources: ['https://api1.local', 'https://api2.local'], rotate: 20, timeout: 100, }); setFakeData(provider, prefix, data); // Attempt to load icons let counter = 0; loadIcons( [ provider + ':' + prefix + ':icon1', provider + ':' + prefix + ':icon2', ], (loaded, missing, pending) => { try { counter++; switch (counter) { case 1: // Loaded icon1 expect(loaded).to.be.eql([ { provider, prefix, name: 'icon1', }, ]); expect(pending).to.be.eql([ { provider, prefix, name: 'icon2', }, ]); expect(missing).to.be.eql([]); done(); break; case 2: done( 'Callback should not be called ' + counter + ' times.' ); } } catch (err) { done(err); } } ); }); });