2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { buildShlinkApiClient } from '../../../src/api/services/ShlinkApiClientBuilder';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ReachableServer, SelectedServer } from '../../../src/servers/data';
|
2018-12-18 22:19:22 +03:00
|
|
|
|
|
|
|
describe('ShlinkApiClientBuilder', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const server = fromPartial<ReachableServer>;
|
2020-08-27 23:09:16 +03:00
|
|
|
|
2019-04-21 12:31:40 +03:00
|
|
|
const createBuilder = () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const builder = buildShlinkApiClient(fromPartial({}));
|
|
|
|
return (selectedServer: SelectedServer) => builder(() => fromPartial({ selectedServer }));
|
2019-04-21 12:31:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
it('creates new instances when provided params are different', async () => {
|
|
|
|
const builder = createBuilder();
|
2022-03-26 14:17:42 +03:00
|
|
|
const [firstApiClient, secondApiClient, thirdApiClient] = await Promise.all([
|
2020-08-27 23:09:16 +03:00
|
|
|
builder(server({ url: 'foo', apiKey: 'bar' })),
|
|
|
|
builder(server({ url: 'bar', apiKey: 'bar' })),
|
|
|
|
builder(server({ url: 'bar', apiKey: 'foo' })),
|
2019-04-21 12:31:40 +03:00
|
|
|
]);
|
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();
|
2020-08-27 23:09:16 +03:00
|
|
|
const selectedServer = server({ url: 'foo', apiKey: 'bar' });
|
2022-03-26 14:17:42 +03:00
|
|
|
const [firstApiClient, secondApiClient, thirdApiClient] = await Promise.all([
|
2019-04-21 12:31:40 +03:00
|
|
|
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
|
|
|
|
2020-03-05 11:23:53 +03:00
|
|
|
it('does not fetch from state when provided param is already selected server', () => {
|
2019-10-05 11:40:32 +03:00
|
|
|
const url = 'url';
|
|
|
|
const apiKey = 'apiKey';
|
2023-04-13 22:48:29 +03:00
|
|
|
const apiClient = buildShlinkApiClient(fromPartial({}))(server({ url, apiKey }));
|
2019-10-05 11:40:32 +03:00
|
|
|
|
2021-02-28 14:56:56 +03:00
|
|
|
expect(apiClient['baseUrl']).toEqual(url); // eslint-disable-line @typescript-eslint/dot-notation
|
|
|
|
expect(apiClient['apiKey']).toEqual(apiKey); // eslint-disable-line @typescript-eslint/dot-notation
|
2019-10-05 11:40:32 +03:00
|
|
|
});
|
2018-12-18 22:19:22 +03:00
|
|
|
});
|