2023-04-26 15:17:23 +00:00
|
|
|
import { renderHook, act, waitFor } from '@testing-library/react';
|
|
|
|
import { useLocalStorage } from '@vegaprotocol/react-helpers';
|
2023-06-06 13:10:03 +00:00
|
|
|
import { SentryInit, SentryClose } from '@vegaprotocol/logger';
|
2023-09-01 09:00:20 +00:00
|
|
|
import {
|
|
|
|
STORAGE_KEY,
|
|
|
|
STORAGE_SECOND_KEY,
|
|
|
|
useTelemetryApproval,
|
|
|
|
} from './use-telemetry-approval';
|
|
|
|
import { Networks } from '@vegaprotocol/environment';
|
2023-04-26 15:17:23 +00:00
|
|
|
|
|
|
|
const mockSetValue = jest.fn();
|
2023-09-01 09:00:20 +00:00
|
|
|
let mockStorageHookApprovalResult: [string | null, jest.Mock] = [
|
|
|
|
null,
|
|
|
|
mockSetValue,
|
|
|
|
];
|
|
|
|
const mockSetSecondValue = jest.fn();
|
|
|
|
let mockStorageHookViewedResult: [string | null, jest.Mock] = [
|
|
|
|
null,
|
|
|
|
mockSetSecondValue,
|
|
|
|
];
|
2023-06-06 13:10:03 +00:00
|
|
|
jest.mock('@vegaprotocol/logger');
|
2023-04-26 15:17:23 +00:00
|
|
|
jest.mock('@vegaprotocol/react-helpers', () => ({
|
|
|
|
...jest.requireActual('@vegaprotocol/react-helpers'),
|
2023-09-01 09:00:20 +00:00
|
|
|
useLocalStorage: jest.fn((key: string) => {
|
|
|
|
if (key === 'vega_telemetry_approval') {
|
|
|
|
return mockStorageHookApprovalResult;
|
|
|
|
}
|
|
|
|
return mockStorageHookViewedResult;
|
|
|
|
}),
|
2023-04-26 15:17:23 +00:00
|
|
|
}));
|
2023-09-01 09:00:20 +00:00
|
|
|
let mockVegaEnv = 'test';
|
2023-06-27 06:40:42 +00:00
|
|
|
jest.mock('@vegaprotocol/environment', () => ({
|
2023-09-01 09:00:20 +00:00
|
|
|
...jest.requireActual('@vegaprotocol/environment'),
|
|
|
|
useEnvironment: jest.fn(() => ({
|
|
|
|
VEGA_ENV: mockVegaEnv,
|
|
|
|
SENTRY_DSN: 'sentry-dsn',
|
|
|
|
})),
|
2023-06-27 06:40:42 +00:00
|
|
|
}));
|
2023-04-26 15:17:23 +00:00
|
|
|
|
|
|
|
describe('useTelemetryApproval', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
2023-06-27 06:40:42 +00:00
|
|
|
|
2023-09-01 09:00:20 +00:00
|
|
|
it('when empty hook should return proper array', () => {
|
2023-06-27 06:40:42 +00:00
|
|
|
const { result } = renderHook(() => useTelemetryApproval());
|
2023-09-01 09:00:20 +00:00
|
|
|
expect(result.current[0]).toEqual('');
|
2023-06-27 06:40:42 +00:00
|
|
|
expect(result.current[1]).toEqual(expect.any(Function));
|
2023-09-01 09:00:20 +00:00
|
|
|
expect(result.current[2]).toEqual(true);
|
|
|
|
expect(result.current[3]).toEqual(expect.any(Function));
|
2023-04-26 15:17:23 +00:00
|
|
|
expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_KEY);
|
2023-09-01 09:00:20 +00:00
|
|
|
expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_SECOND_KEY);
|
|
|
|
expect(mockSetValue).toHaveBeenCalledWith('true');
|
|
|
|
expect(mockSetSecondValue).not.toHaveBeenCalledWith('true');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when approval not empty but viewed is empty should return proper array', () => {
|
|
|
|
mockStorageHookApprovalResult = ['false', mockSetValue];
|
|
|
|
const { result } = renderHook(() => useTelemetryApproval());
|
|
|
|
expect(result.current[0]).toEqual('false');
|
|
|
|
expect(result.current[1]).toEqual(expect.any(Function));
|
|
|
|
expect(result.current[2]).toEqual(true);
|
|
|
|
expect(result.current[3]).toEqual(expect.any(Function));
|
|
|
|
expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_KEY);
|
|
|
|
expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_SECOND_KEY);
|
|
|
|
expect(mockSetValue).not.toHaveBeenCalled();
|
|
|
|
expect(mockSetSecondValue).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when NOT empty hook should return proper array', () => {
|
|
|
|
mockStorageHookApprovalResult = ['false', mockSetValue];
|
|
|
|
mockStorageHookViewedResult = ['true', mockSetSecondValue];
|
|
|
|
const { result } = renderHook(() => useTelemetryApproval());
|
|
|
|
expect(result.current[0]).toEqual('false');
|
|
|
|
expect(result.current[1]).toEqual(expect.any(Function));
|
|
|
|
expect(result.current[2]).toEqual(false);
|
|
|
|
expect(result.current[3]).toEqual(expect.any(Function));
|
|
|
|
expect(useLocalStorage).toHaveBeenCalledWith(STORAGE_KEY);
|
|
|
|
expect(mockSetValue).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on mainnet hook should init properly', () => {
|
|
|
|
mockStorageHookApprovalResult = [null, mockSetValue];
|
|
|
|
mockVegaEnv = Networks.MAINNET;
|
|
|
|
renderHook(() => useTelemetryApproval());
|
|
|
|
expect(mockSetValue).toHaveBeenCalledWith('false');
|
2023-04-26 15:17:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('hook should init stuff properly', async () => {
|
2023-06-27 06:40:42 +00:00
|
|
|
const { result } = renderHook(() => useTelemetryApproval());
|
2023-04-26 15:17:23 +00:00
|
|
|
await act(() => {
|
2023-09-01 09:00:20 +00:00
|
|
|
result.current[1]('true');
|
2023-04-26 15:17:23 +00:00
|
|
|
});
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(SentryInit).toHaveBeenCalled();
|
2023-09-01 09:00:20 +00:00
|
|
|
expect(mockSetValue).toHaveBeenCalledWith('true');
|
|
|
|
expect(mockSetSecondValue).toHaveBeenCalledWith('true');
|
2023-04-26 15:17:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hook should close stuff properly', async () => {
|
2023-06-27 06:40:42 +00:00
|
|
|
const { result } = renderHook(() => useTelemetryApproval());
|
2023-04-26 15:17:23 +00:00
|
|
|
await act(() => {
|
2023-09-01 09:00:20 +00:00
|
|
|
result.current[1]('false');
|
2023-04-26 15:17:23 +00:00
|
|
|
});
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(SentryClose).toHaveBeenCalled();
|
2023-09-01 09:00:20 +00:00
|
|
|
expect(mockSetValue).toHaveBeenCalledWith('false');
|
|
|
|
expect(mockSetSecondValue).toHaveBeenCalledWith('true');
|
2023-04-26 15:17:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|