2018-12-18 22:19:22 +03:00
|
|
|
import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder';
|
|
|
|
|
|
|
|
describe('ShlinkApiClientBuilder', () => {
|
2019-04-21 12:31:40 +03:00
|
|
|
const createBuilder = () => {
|
|
|
|
const builder = buildShlinkApiClient({});
|
2018-12-18 22:19:22 +03:00
|
|
|
|
2019-04-21 12:31:40 +03:00
|
|
|
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' }),
|
|
|
|
]);
|
2018-12-18 22:19:22 +03:00
|
|
|
|
|
|
|
expect(firstApiClient).not.toBe(secondApiClient);
|
|
|
|
expect(firstApiClient).not.toBe(thirdApiClient);
|
|
|
|
expect(secondApiClient).not.toBe(thirdApiClient);
|
|
|
|
});
|
|
|
|
|
2019-04-21 12:31:40 +03:00
|
|
|
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),
|
|
|
|
]);
|
2018-12-18 22:19:22 +03:00
|
|
|
|
|
|
|
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 });
|
|
|
|
|
2020-01-11 15:55:37 +03:00
|
|
|
expect(apiClient._baseUrl).toEqual(url);
|
2019-10-05 11:40:32 +03:00
|
|
|
expect(apiClient._apiKey).toEqual(apiKey);
|
|
|
|
});
|
2018-12-18 22:19:22 +03:00
|
|
|
});
|