vega-frontend-monorepo/libs/environment/src/hooks/use-config.spec.tsx

312 lines
9.7 KiB
TypeScript
Raw Normal View History

feat(#507): Node Discovery * feat: add network-switcher lib * feat: add env variables for some deployed app urls * feat: add network processing to environment hoook * refactor: network handling * refactor: remove dialog from provider and add env setter * feat: add network switcher dialog to the trading app * refactor: add network redirect to dialog connect callback * fix: lint * feat: add network configuration files to static app * feat: update environments to use config file instead of static node url * refactor: split out network switcher utils * refactor: split up environment hook * fix: jsonify env variable for possible networks * fix: add formatter file * feat: add network loader component * feat: add network loader to the trading app * fix: assign correct global state to network swicther * feat: add status modal * feat: add network-switcher lib * feat: add env variables for some deployed app urls * feat: add network processing to environment hoook * refactor: network handling * refactor: remove dialog from provider and add env setter * feat: add network switcher dialog to the trading app * refactor: add network redirect to dialog connect callback * fix: lint * fix: jsonify env variable for possible networks * fix: add formatter file * fix: assign correct global state to network swicther * fix: failing tests from UI changes * feat: add environment validation * feat: add runtime validation for network configs * fix: readd node urls to envs to avoid breaking the apps for now * chore: rename network swicther lib to environmnet * fix: lint * feat: add tests for config hook * feat: add environment hook tests * fix: lint * fix: lint * feat: add environment hook tests * feat: add storage tests * fix: formet * feat: improve loading states * fix: format * fix: use router instead of window location * fix: rearrange network loader props and components * fix: remove FC type * fix: env validation * fix: untangle returns in network loader * fix: add teardown for env and localstorage * fix: add custom to env networks * fix: lint * fix: format * fix: lint * fix: remove env provider from simple trading app * fix: remove failing promise hacks * fix: some leftover format files * fix: remove network switcher from tsconf * fix: move Networks to libs/environment * fix: add defaults for ether env vars * feat: add tests for default ether env vars * fix: remove chain id env var from web3 container * fix: remove chain id from the environment * fix: format * fix: lint token * fix: lint env * fix: add comment to callout hack * fix: lint token again * fix: remove skip * fix: move addresses to token app * fix: improve schema validation errors and fix token app * fix: lint * fix: format * fix: format * fix: add network loaders to apps * fix: format * fix: remove logs * fix: cypress process errors * fix: change network loader hierarchy in token * fix: remove stray console.log * fix: revert test changes in simple trading app * fix: prefix env vars with NX Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: improve schema validation errors and fix token app * fix: format * fix: disable lint rules for catch block any types * fix: format again * fix: remove redundant process.platform injections * fix: format Co-authored-by: Joe <joe@vega.xyz> Co-authored-by: Matthew Russell <mattrussell36@gmail.com> Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-06-21 23:20:53 +00:00
import { renderHook } from '@testing-library/react-hooks';
import type { EnvironmentWithOptionalUrl } from './use-config';
import { useConfig, LOCAL_STORAGE_NETWORK_KEY } from './use-config';
import { Networks } from '../types';
type HostMapping = Record<string, number | Error>;
const mockHostsMap: HostMapping = {
'https://host1.com': 300,
'https://host2.com': 500,
'https://host3.com': 100,
'https://host4.com': 650,
};
const hostList = Object.keys(mockHostsMap);
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',
};
function setupFetch(configUrl: string, hostMap: HostMapping) {
const hostUrls = Object.keys(hostMap);
return (url: RequestInfo) => {
if (url === configUrl) {
return Promise.resolve({
ok: true,
json: () => Promise.resolve({ hosts: hostUrls }),
} as Response);
}
if (hostUrls.includes(url as string)) {
const value = hostMap[url as string];
return new Promise<Response>((resolve, reject) => {
if (typeof value === 'number') {
setTimeout(() => {
resolve({ ok: true } as Response);
}, value);
} else {
reject(value);
}
});
}
return Promise.resolve({
ok: true,
} as Response);
};
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
global.fetch = jest.fn();
const mockUpdate = jest.fn();
beforeEach(() => {
jest.useFakeTimers();
mockUpdate.mockClear();
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(
setupFetch(mockEnvironment.VEGA_CONFIG_URL ?? '', mockHostsMap)
);
});
afterAll(() => {
// @ts-ignore: typescript doesn't recognise the mocked fetch instance
fetch.mockRestore();
});
describe('useConfig hook', () => {
it('has an initial success state when the environment already has a URL', async () => {
const mockEnvWithUrl = {
...mockEnvironment,
VEGA_URL: 'https://some.url/query',
};
const { result } = renderHook(() => useConfig(mockEnvWithUrl, mockUpdate));
expect(fetch).not.toHaveBeenCalled();
expect(mockUpdate).not.toHaveBeenCalled();
expect(result.current.status).toBe('success');
});
it('updates the environment with a host url from the network configuration', async () => {
const allowedStatuses = [
'idle',
'loading-config',
'loading-node',
'success',
];
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
jest.runAllTimers();
await waitForNextUpdate();
expect(result.current.status).toBe('success');
result.all.forEach((state) => {
expect(allowedStatuses).toContain('status' in state && state.status);
});
// fetches config
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
// calls each node
hostList.forEach((url) => {
expect(fetch).toHaveBeenCalledWith(url);
});
// updates the environment
expect(hostList).toContain(mockUpdate.mock.calls[0][0]({}).VEGA_URL);
});
it('uses the host from the configuration which responds first', async () => {
const shortestResponseTime = Object.values(mockHostsMap).sort()[0];
const expectedHost = hostList.find((url: keyof typeof mockHostsMap) => {
return mockHostsMap[url] === shortestResponseTime;
});
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
jest.runAllTimers();
await waitForNextUpdate();
expect(result.current.status).toBe('success');
expect(mockUpdate.mock.calls[0][0]({}).VEGA_URL).toBe(expectedHost);
});
it('ignores failing hosts and uses one which returns a success response', async () => {
const mockHostsMapScoped = {
'https://host1.com': 350,
'https://host2.com': new Error('Server error'),
'https://host3.com': 230,
'https://host4.com': new Error('Server error'),
};
// @ts-ignore typescript doesn't recognise the mocked instance
global.fetch.mockImplementation(
setupFetch(mockEnvironment.VEGA_CONFIG_URL ?? '', mockHostsMapScoped)
);
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
jest.runAllTimers();
await waitForNextUpdate();
expect(result.current.status).toBe('success');
expect(mockUpdate.mock.calls[0][0]({}).VEGA_URL).toBe('https://host3.com');
});
it('returns the correct error status for when the config cannot be accessed', async () => {
// @ts-ignore typescript doesn't recognise the mocked instance
global.fetch.mockImplementation((url: RequestInfo) => {
if (url === mockEnvironment.VEGA_CONFIG_URL) {
return Promise.reject(new Error('Server error'));
}
return Promise.resolve({ ok: true } as Response);
});
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
expect(result.current.status).toBe('error-loading-config');
expect(mockUpdate).not.toHaveBeenCalled();
});
it('returns the correct error status for when the config is not valid', async () => {
// @ts-ignore typescript doesn't recognise the mocked instance
global.fetch.mockImplementation((url: RequestInfo) => {
if (url === mockEnvironment.VEGA_CONFIG_URL) {
return Promise.resolve({
ok: true,
json: () => Promise.resolve({ some: 'data' }),
});
}
return Promise.resolve({ ok: true } as Response);
});
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
expect(result.current.status).toBe('error-validating-config');
expect(mockUpdate).not.toHaveBeenCalled();
});
it('returns the correct error status for when no hosts can be accessed', async () => {
const mockHostsMapScoped = {
'https://host1.com': new Error('Server error'),
'https://host2.com': new Error('Server error'),
'https://host3.com': new Error('Server error'),
'https://host4.com': new Error('Server error'),
};
// @ts-ignore typescript doesn't recognise the mocked instance
global.fetch.mockImplementation(
setupFetch(mockEnvironment.VEGA_CONFIG_URL ?? '', mockHostsMapScoped)
);
const { result, waitForNextUpdate } = renderHook(() =>
useConfig(mockEnvironment, mockUpdate)
);
await waitForNextUpdate();
expect(result.current.status).toBe('error-loading-node');
expect(mockUpdate).not.toHaveBeenCalled();
});
it('caches the list of networks', async () => {
const run1 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
await run1.waitForNextUpdate();
jest.runAllTimers();
await run1.waitForNextUpdate();
expect(run1.result.current.status).toBe('success');
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
// @ts-ignore typescript doesn't recognise the mocked instance
fetch.mockClear();
const run2 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
jest.runAllTimers();
await run2.waitForNextUpdate();
expect(run2.result.current.status).toBe('success');
expect(fetch).not.toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
});
it('caches the list of networks between runs', async () => {
const run1 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
await run1.waitForNextUpdate();
jest.runAllTimers();
await run1.waitForNextUpdate();
expect(run1.result.current.status).toBe('success');
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
// @ts-ignore typescript doesn't recognise the mocked instance
fetch.mockClear();
const run2 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
jest.runAllTimers();
await run2.waitForNextUpdate();
expect(run2.result.current.status).toBe('success');
expect(fetch).not.toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
});
it('refetches the network configuration and resets the cache when malformed data found in the storage', async () => {
window.localStorage.setItem(LOCAL_STORAGE_NETWORK_KEY, '{not:{valid:{json');
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(noop);
const run1 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
await run1.waitForNextUpdate();
jest.runAllTimers();
await run1.waitForNextUpdate();
expect(run1.result.current.status).toBe('success');
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
expect(consoleWarnSpy).toHaveBeenCalled();
consoleWarnSpy.mockRestore();
});
it('refetches the network configuration and resets the cache when invalid data found in the storage', async () => {
window.localStorage.setItem(
LOCAL_STORAGE_NETWORK_KEY,
JSON.stringify({ invalid: 'data' })
);
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(noop);
const run1 = renderHook(() => useConfig(mockEnvironment, mockUpdate));
await run1.waitForNextUpdate();
jest.runAllTimers();
await run1.waitForNextUpdate();
expect(run1.result.current.status).toBe('success');
expect(fetch).toHaveBeenCalledWith(mockEnvironment.VEGA_CONFIG_URL);
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
});