shlink-web-client/test/utils/services/ShlinkApiClientBuilder.test.js

46 lines
1.6 KiB
JavaScript
Raw Normal View History

import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder';
describe('ShlinkApiClientBuilder', () => {
const createBuilder = () => {
const builder = buildShlinkApiClient({});
return (selectedServer) => builder(() => ({ selectedServer }));
};
it('creates new instances when provided params are different', async () => {
const builder = createBuilder();
const [ firstApiClient, secondApiClient, thirdApiClient ] = await Promise.all([
builder({ url: 'foo', apiKey: 'bar' }),
builder({ url: 'bar', apiKey: 'bar' }),
builder({ url: 'bar', apiKey: 'foo' }),
]);
expect(firstApiClient).not.toBe(secondApiClient);
expect(firstApiClient).not.toBe(thirdApiClient);
expect(secondApiClient).not.toBe(thirdApiClient);
});
it('returns existing instances when provided params are the same', async () => {
const builder = createBuilder();
const selectedServer = { url: 'foo', apiKey: 'bar' };
const [ firstApiClient, secondApiClient, thirdApiClient ] = await Promise.all([
builder(selectedServer),
builder(selectedServer),
builder(selectedServer),
]);
expect(firstApiClient).toBe(secondApiClient);
expect(firstApiClient).toBe(thirdApiClient);
expect(secondApiClient).toBe(thirdApiClient);
});
2019-10-05 11:40:32 +03:00
it('does not fetch from state when provided param is already selected server', async () => {
const url = 'url';
const apiKey = 'apiKey';
const apiClient = await buildShlinkApiClient({})({ url, apiKey });
expect(apiClient._baseUrl).toEqual(url);
2019-10-05 11:40:32 +03:00
expect(apiClient._apiKey).toEqual(apiKey);
});
});