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

53 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-08-27 23:09:16 +03:00
import { Mock } from 'ts-mockery';
import { AxiosInstance } from 'axios';
import buildShlinkApiClient from '../../../src/api/services/ShlinkApiClientBuilder';
2020-08-27 23:09:16 +03:00
import { ReachableServer, SelectedServer } from '../../../src/servers/data';
import { ShlinkState } from '../../../src/container/types';
describe('ShlinkApiClientBuilder', () => {
2020-08-27 23:09:16 +03:00
const axiosMock = Mock.all<AxiosInstance>();
const server = (data: Partial<ReachableServer>) => Mock.of<ReachableServer>(data);
const createBuilder = () => {
2020-08-27 23:09:16 +03:00
const builder = buildShlinkApiClient(axiosMock);
2020-08-27 23:09:16 +03:00
return (selectedServer: SelectedServer) => builder(() => Mock.of<ShlinkState>({ selectedServer }));
};
it('creates new instances when provided params are different', async () => {
const builder = createBuilder();
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' })),
]);
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();
2020-08-27 23:09:16 +03:00
const selectedServer = server({ 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', () => {
2019-10-05 11:40:32 +03:00
const url = 'url';
const apiKey = 'apiKey';
2020-08-27 23:09:16 +03:00
const apiClient = buildShlinkApiClient(axiosMock)(server({ url, apiKey }));
2019-10-05 11:40:32 +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
});
});