2022-06-21 23:20:53 +00:00
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
import type { EnvironmentWithOptionalUrl } from './use-config';
|
2022-07-15 11:00:57 +00:00
|
|
|
import { useConfig } from './use-config';
|
|
|
|
import { Networks, ErrorType } from '../types';
|
|
|
|
|
|
|
|
const mockConfig = {
|
|
|
|
hosts: [
|
|
|
|
'https://vega-host-1.com',
|
|
|
|
'https://vega-host-2.com',
|
|
|
|
'https://vega-host-3.com',
|
|
|
|
'https://vega-host-4.com',
|
|
|
|
],
|
2022-06-21 23:20:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockEnvironment: EnvironmentWithOptionalUrl = {
|
|
|
|
VEGA_ENV: Networks.TESTNET,
|
|
|
|
VEGA_CONFIG_URL: 'https://vega.url/config.json',
|
|
|
|
VEGA_NETWORKS: {},
|
|
|
|
ETHEREUM_PROVIDER_URL: 'https://ethereum.provider',
|
|
|
|
ETHERSCAN_URL: 'https://etherscan.url',
|
2022-07-12 16:34:54 +00:00
|
|
|
GIT_BRANCH: 'test',
|
|
|
|
GIT_ORIGIN_URL: 'https://github.com/test/repo',
|
|
|
|
GIT_COMMIT_HASH: 'abcde01234',
|
|
|
|
GITHUB_FEEDBACK_URL: 'https://github.com/test/feedback',
|
2022-06-21 23:20:53 +00:00
|
|
|
};
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
function setupFetch(configUrl: string) {
|
2022-06-21 23:20:53 +00:00
|
|
|
return (url: RequestInfo) => {
|
|
|
|
if (url === configUrl) {
|
|
|
|
return Promise.resolve({
|
|
|
|
ok: true,
|
2022-07-15 11:00:57 +00:00
|
|
|
json: () => Promise.resolve(mockConfig),
|
2022-06-21 23:20:53 +00:00
|
|
|
} as Response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve({
|
|
|
|
ok: true,
|
|
|
|
} as Response);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
global.fetch = jest.fn();
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
const onError = jest.fn();
|
2022-06-21 23:20:53 +00:00
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
beforeAll(() => {
|
2022-06-21 23:20:53 +00:00
|
|
|
jest.useFakeTimers();
|
2022-07-15 11:00:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
onError.mockClear();
|
2022-06-21 23:20:53 +00:00
|
|
|
window.localStorage.clear();
|
|
|
|
|
|
|
|
// @ts-ignore typescript doesn't recognise the mocked instance
|
|
|
|
global.fetch.mockReset();
|
|
|
|
// @ts-ignore typescript doesn't recognise the mocked instance
|
|
|
|
global.fetch.mockImplementation(
|
2022-07-15 11:00:57 +00:00
|
|
|
setupFetch(mockEnvironment.VEGA_CONFIG_URL ?? '')
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-07-15 11:00:57 +00:00
|
|
|
jest.clearAllMocks();
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('useConfig hook', () => {
|
2022-07-15 11:00:57 +00:00
|
|
|
it("doesn't update when there is no VEGA_CONFIG_URL in the environment", async () => {
|
|
|
|
const mockEnvWithoutUrl = {
|
|
|
|
...mockEnvironment,
|
|
|
|
VEGA_CONFIG_URL: undefined,
|
|
|
|
};
|
|
|
|
const { result } = renderHook(() => useConfig(mockEnvWithoutUrl, onError));
|
2022-06-21 23:20:53 +00:00
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
expect(result.current.config).toBe(undefined);
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
it('fetches configuration from the provided url', async () => {
|
2022-06-21 23:20:53 +00:00
|
|
|
const { result, waitForNextUpdate } = renderHook(() =>
|
2022-07-15 11:00:57 +00:00
|
|
|
useConfig(mockEnvironment, onError)
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await waitForNextUpdate();
|
2022-07-15 11:00:57 +00:00
|
|
|
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
|
|
|
|
expect(result.current.config).toEqual(mockConfig);
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
it('caches the configuration', async () => {
|
|
|
|
const { result: firstResult, waitForNextUpdate: waitForFirstUpdate } =
|
|
|
|
renderHook(() => useConfig(mockEnvironment, onError));
|
2022-06-21 23:20:53 +00:00
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
await waitForFirstUpdate();
|
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(firstResult.current.config).toEqual(mockConfig);
|
2022-06-21 23:20:53 +00:00
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
const { result: secondResult } = renderHook(() =>
|
|
|
|
useConfig(mockEnvironment, onError)
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(secondResult.current.config).toEqual(mockConfig);
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
it('executes the error callback when the config endpoint fails', async () => {
|
2022-06-21 23:20:53 +00:00
|
|
|
// @ts-ignore typescript doesn't recognise the mocked instance
|
2022-07-15 11:00:57 +00:00
|
|
|
global.fetch.mockImplementation(() => Promise.reject());
|
2022-06-21 23:20:53 +00:00
|
|
|
|
|
|
|
const { result, waitForNextUpdate } = renderHook(() =>
|
2022-07-15 11:00:57 +00:00
|
|
|
useConfig(mockEnvironment, onError)
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await waitForNextUpdate();
|
2022-07-15 11:00:57 +00:00
|
|
|
expect(result.current.config).toEqual({ hosts: [] });
|
|
|
|
expect(onError).toHaveBeenCalledWith(ErrorType.CONFIG_LOAD_ERROR);
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
it('executes the error callback when the config validation fails', async () => {
|
2022-06-21 23:20:53 +00:00
|
|
|
// @ts-ignore typescript doesn't recognise the mocked instance
|
2022-07-15 11:00:57 +00:00
|
|
|
global.fetch.mockImplementation(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
ok: true,
|
|
|
|
json: () => Promise.resolve({ data: 'not-valid-config' }),
|
|
|
|
})
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const { result, waitForNextUpdate } = renderHook(() =>
|
2022-07-15 11:00:57 +00:00
|
|
|
useConfig(mockEnvironment, onError)
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await waitForNextUpdate();
|
2022-07-15 11:00:57 +00:00
|
|
|
expect(result.current.config).toBe(undefined);
|
|
|
|
expect(onError).toHaveBeenCalledWith(ErrorType.CONFIG_VALIDATION_ERROR);
|
2022-06-21 23:20:53 +00:00
|
|
|
});
|
|
|
|
});
|