2021-09-05 16:09:25 +00:00
|
|
|
import crossFetch from 'cross-fetch';
|
|
|
|
import { sendAPIQuery } from '../../lib/api/query';
|
|
|
|
import { setAPIModule } from '../../lib/api/modules';
|
|
|
|
import { fetchAPIModule, setFetch } from '../../lib/api/modules/fetch';
|
2021-09-24 15:38:52 +00:00
|
|
|
import { addAPIProvider } from '../../lib/api/config';
|
2021-09-05 16:09:25 +00:00
|
|
|
import { mockAPIModule } from '../../lib/api/modules/mock';
|
|
|
|
|
|
|
|
describe('Testing live API with fetch', () => {
|
|
|
|
let counter = 0;
|
|
|
|
function nextProvider(): string {
|
2022-03-16 21:30:16 +00:00
|
|
|
return 'fetch-' + (counter++).toString();
|
2021-09-05 16:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const host = 'https://api.iconify.design';
|
|
|
|
|
|
|
|
// Set fetch module
|
2021-09-23 21:27:16 +00:00
|
|
|
beforeEach(() => {
|
2021-09-05 16:09:25 +00:00
|
|
|
setFetch(crossFetch);
|
|
|
|
setAPIModule('', fetchAPIModule);
|
|
|
|
});
|
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
afterAll(() => {
|
2021-09-05 16:09:25 +00:00
|
|
|
setAPIModule('', mockAPIModule);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Missing API configuration', (done) => {
|
|
|
|
const provider = nextProvider();
|
|
|
|
sendAPIQuery(
|
|
|
|
provider,
|
|
|
|
{
|
|
|
|
type: 'custom',
|
|
|
|
provider,
|
|
|
|
uri: '/collections',
|
|
|
|
},
|
|
|
|
(data, error) => {
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(error).toBe(424);
|
|
|
|
expect(data).toBeUndefined();
|
2021-09-05 16:09:25 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Custom request with provider', (done) => {
|
|
|
|
const provider = nextProvider();
|
|
|
|
expect(
|
2021-09-24 15:38:52 +00:00
|
|
|
addAPIProvider(provider, {
|
2021-09-05 16:09:25 +00:00
|
|
|
resources: [host],
|
|
|
|
})
|
2021-09-23 21:27:16 +00:00
|
|
|
).toBe(true);
|
2021-09-05 16:09:25 +00:00
|
|
|
|
|
|
|
sendAPIQuery(
|
|
|
|
provider,
|
|
|
|
{
|
|
|
|
type: 'custom',
|
|
|
|
provider,
|
|
|
|
uri: '/collections',
|
|
|
|
},
|
|
|
|
(data, error) => {
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(error).toBeUndefined();
|
|
|
|
expect(typeof data).toBe('object');
|
2021-09-05 16:09:25 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Custom request with host', (done) => {
|
|
|
|
sendAPIQuery(
|
|
|
|
{
|
|
|
|
resources: [host],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'custom',
|
|
|
|
uri: '/collections',
|
|
|
|
},
|
|
|
|
(data, error) => {
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(error).toBeUndefined();
|
|
|
|
expect(typeof data).toBe('object');
|
2021-09-05 16:09:25 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|