Vikrant Gupta 2f7d208eb5
fix: added time range key for query and local storage handling (#6009)
* fix: added time range key for query and local storage handling

* chore: fix jest test cases

* fix: send single element array for only variable option as well

* fix: intermediate stale data should not be shown
2024-09-19 19:23:12 +05:30

159 lines
4.0 KiB
TypeScript

import '@testing-library/jest-dom/extend-expect';
import MockQueryClientProvider from 'providers/test/MockQueryClientProvider';
import React, { useEffect } from 'react';
import { act, fireEvent, render, screen, waitFor } from 'tests/test-utils';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import VariableItem from './VariableItem';
const mockVariableData: IDashboardVariable = {
id: 'test_variable',
description: 'Test Variable',
type: 'TEXTBOX',
textboxValue: 'defaultValue',
sort: 'DISABLED',
multiSelect: false,
showALLOption: false,
name: 'testVariable',
};
// New mock data for a custom variable
const mockCustomVariableData: IDashboardVariable = {
...mockVariableData,
name: 'customVariable',
type: 'CUSTOM',
customValue: 'option1,option2,option3',
};
const mockOnValueUpdate = jest.fn();
describe('VariableItem', () => {
let useEffectSpy: jest.SpyInstance;
beforeEach(() => {
useEffectSpy = jest.spyOn(React, 'useEffect');
});
afterEach(() => {
jest.clearAllMocks();
useEffectSpy.mockRestore();
});
test('renders component with default props', () => {
render(
<MockQueryClientProvider>
<VariableItem
variableData={mockVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
expect(screen.getByText('$testVariable')).toBeInTheDocument();
});
test('renders Input when the variable type is TEXTBOX', () => {
render(
<MockQueryClientProvider>
<VariableItem
variableData={mockVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
expect(screen.getByPlaceholderText('Enter value')).toBeInTheDocument();
});
test('calls onChange event handler when Input value changes', async () => {
render(
<MockQueryClientProvider>
<VariableItem
variableData={mockVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
act(() => {
const inputElement = screen.getByPlaceholderText('Enter value');
fireEvent.change(inputElement, { target: { value: 'newValue' } });
});
await waitFor(() => {
// expect(mockOnValueUpdate).toHaveBeenCalledTimes(1);
expect(mockOnValueUpdate).toHaveBeenCalledWith(
'testVariable',
'test_variable',
'newValue',
false,
);
});
});
test('renders a Select element when variable type is CUSTOM', () => {
render(
<MockQueryClientProvider>
<VariableItem
variableData={mockCustomVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
expect(screen.getByText('$customVariable')).toBeInTheDocument();
expect(screen.getByTestId('variable-select')).toBeInTheDocument();
});
test('renders a Select element with all selected', async () => {
const customVariableData = {
...mockCustomVariableData,
allSelected: true,
showALLOption: true,
multiSelect: true,
};
render(
<MockQueryClientProvider>
<VariableItem
variableData={customVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
expect(screen.getByTitle('ALL')).toBeInTheDocument();
});
test('calls useEffect when the component mounts', () => {
render(
<MockQueryClientProvider>
<VariableItem
variableData={mockCustomVariableData}
existingVariables={{}}
onValueUpdate={mockOnValueUpdate}
variablesToGetUpdated={[]}
setVariablesToGetUpdated={(): void => {}}
/>
</MockQueryClientProvider>,
);
expect(useEffect).toHaveBeenCalled();
});
});