shlink-web-client/test/api/services/ShlinkApiClientBuilder.test.ts

46 lines
2 KiB
TypeScript
Raw Normal View History

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';
describe('ShlinkApiClientBuilder', () => {
const server = fromPartial<ReachableServer>;
2020-08-27 23:09:16 +03:00
const createBuilder = () => {
const builder = buildShlinkApiClient(fromPartial({}));
return (selectedServer: SelectedServer) => builder(() => fromPartial({ selectedServer }));
};
it('creates new instances when provided params are different', async () => {
const builder = createBuilder();
const firstApiClient = builder(server({ url: 'foo', apiKey: 'bar' }));
const secondApiClient = builder(server({ url: 'bar', apiKey: 'bar' }));
const thirdApiClient = builder(server({ url: 'bar', apiKey: 'foo' }));
expect(firstApiClient === secondApiClient).toEqual(false);
expect(firstApiClient === thirdApiClient).toEqual(false);
expect(secondApiClient === thirdApiClient).toEqual(false);
});
it('returns existing instances when provided params are the same', () => {
const builder = createBuilder();
2020-08-27 23:09:16 +03:00
const selectedServer = server({ url: 'foo', apiKey: 'bar' });
const firstApiClient = builder(selectedServer);
const secondApiClient = builder(selectedServer);
const thirdApiClient = builder(selectedServer);
expect(firstApiClient === secondApiClient).toEqual(true);
expect(firstApiClient === thirdApiClient).toEqual(true);
expect(secondApiClient === thirdApiClient).toEqual(true);
});
2019-10-05 11:40:32 +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';
const apiClient = buildShlinkApiClient(fromPartial({}))(server({ url, apiKey }));
2019-10-05 11:40:32 +03:00
expect(apiClient['serverInfo'].baseUrl).toEqual(url); // eslint-disable-line @typescript-eslint/dot-notation
expect(apiClient['serverInfo'].apiKey).toEqual(apiKey); // eslint-disable-line @typescript-eslint/dot-notation
2019-10-05 11:40:32 +03:00
});
});