Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.8 KB
Raw Blame
// eslint-disable-next-line no-restricted-imports
import { Provider } from 'react-redux';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import set from 'api/browser/localstorage/set';
import { DASHBOARD_TIME_IN_DURATION } from 'constants/app';
import configureStore, { MockStoreEnhanced } from 'redux-mock-store';
import { AppState } from 'store/reducers';
import { UPDATE_TIME_INTERVAL } from 'types/actions/globalTime';
import { TestRouter } from 'tests/router';
import { GlobalReducer } from 'types/reducer/globalTime';

import AutoRefresh from '../index';

const mockStore = configureStore<Partial<AppState>>([]);

const PATHNAME = '/dashboard/test-id';
const randomTime = 1700000000000000000;

function createGlobalTimeState(
	overrides: Partial<GlobalReducer> = {},
): GlobalReducer {
	return {
		minTime: randomTime,
		maxTime: randomTime,
		loading: false,
		selectedTime: '15m',
		isAutoRefreshDisabled: false,
		selectedAutoRefreshInterval: '5s',
		...overrides,
	};
}

function renderAutoRefresh(
	globalTime: GlobalReducer,
	props: { disabled?: boolean } = {},
): MockStoreEnhanced<Partial<AppState>> {
	const store = mockStore({ globalTime });

	render(
		<TestRouter initialRoute={PATHNAME}>
			<Provider store={store}>
				<AutoRefresh {...props} />
			</Provider>
		</TestRouter>,
	);

	return store;
}

function tickCount(store: MockStoreEnhanced<Partial<AppState>>): number {
	return store.getActions().filter((a) => a.type === UPDATE_TIME_INTERVAL)
		.length;
}

describe('AutoRefresh', () => {
	beforeEach(() => {
		jest.useFakeTimers();
		localStorage.clear();
	});

	afterEach(() => {
		jest.useRealTimers();
	});

	it('renders the trigger and ticks on the persisted interval', () => {
		set(DASHBOARD_TIME_IN_DURATION, JSON.stringify({ [PATHNAME]: '5s' }));

		const store = renderAutoRefresh(createGlobalTimeState());

		expect(screen.getByTitle('Set auto refresh')).toBeInTheDocument();

		act(() => {
			jest.advanceTimersByTime(15_000);
		});

		expect(tickCount(store)).toBe(3);
	});

	it('does not tick when auto refresh was never enabled for the route', () => {
		const store = renderAutoRefresh(createGlobalTimeState());

		act(() => {
			jest.advanceTimersByTime(60_000);
		});

		expect(tickCount(store)).toBe(0);
	});

	it('does not tick while the disabled prop is set', () => {
		set(DASHBOARD_TIME_IN_DURATION, JSON.stringify({ [PATHNAME]: '5s' }));

		const store = renderAutoRefresh(createGlobalTimeState(), { disabled: true });

		act(() => {
			jest.advanceTimersByTime(60_000);
		});

		expect(tickCount(store)).toBe(0);
	});

	it('renders nothing on a custom time range', () => {
		set(DASHBOARD_TIME_IN_DURATION, JSON.stringify({ [PATHNAME]: '5s' }));

		const store = renderAutoRefresh(
			createGlobalTimeState({ selectedTime: 'custom' }),
		);

		expect(screen.queryByTitle('Set auto refresh')).not.toBeInTheDocument();

		act(() => {
			jest.advanceTimersByTime(60_000);
		});

		expect(tickCount(store)).toBe(0);
	});
});

describe('AutoRefresh disabled checkbox', () => {
	beforeEach(() => {
		localStorage.clear();
	});

	it('explains why it is disabled and ignores clicks', async () => {
		const user = userEvent.setup();
		const store = renderAutoRefresh(
			createGlobalTimeState({ isAutoRefreshDisabled: true }),
		);

		await user.click(screen.getByTitle('Set auto refresh'));
		const checkbox = await screen.findByRole('checkbox', {
			name: 'Auto Refresh',
		});
		expect(checkbox).toHaveAttribute('aria-disabled', 'true');

		await user.hover(checkbox);
		await expect(
			screen.findByText('Auto refresh is not available for a custom time range'),
		).resolves.toBeInTheDocument();

		await user.click(checkbox);

		expect(checkbox).not.toBeChecked();
		expect(localStorage.getItem(DASHBOARD_TIME_IN_DURATION)).toBeNull();
		expect(tickCount(store)).toBe(0);
	});
});