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);
|
|
|
|
});
|
|
|
|
});
|