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

37 lines
1.3 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);
});
});