2023-05-02 18:51:24 +05:30
|
|
|
import '@testing-library/jest-dom/extend-expect';
|
|
|
|
|
|
2023-11-30 13:56:49 +05:30
|
|
|
import {
|
|
|
|
|
act,
|
|
|
|
|
fireEvent,
|
|
|
|
|
render,
|
|
|
|
|
screen,
|
|
|
|
|
waitFor,
|
|
|
|
|
} from '@testing-library/react';
|
|
|
|
|
import MockQueryClientProvider from 'providers/test/MockQueryClientProvider';
|
2023-05-19 13:14:32 +05:30
|
|
|
import React, { useEffect } from 'react';
|
2023-05-02 18:51:24 +05:30
|
|
|
import { IDashboardVariable } from 'types/api/dashboard/getAll';
|
|
|
|
|
|
|
|
|
|
import VariableItem from './VariableItem';
|
|
|
|
|
|
|
|
|
|
const mockVariableData: IDashboardVariable = {
|
2023-12-15 13:10:02 +05:30
|
|
|
id: 'test_variable',
|
2023-05-02 18:51:24 +05:30
|
|
|
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(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={mockVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText('$testVariable')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('renders Input when the variable type is TEXTBOX', () => {
|
|
|
|
|
render(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={mockVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
expect(screen.getByPlaceholderText('Enter value')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-30 13:56:49 +05:30
|
|
|
test('calls onChange event handler when Input value changes', async () => {
|
2023-05-02 18:51:24 +05:30
|
|
|
render(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={mockVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
|
2023-11-30 13:56:49 +05:30
|
|
|
act(() => {
|
|
|
|
|
const inputElement = screen.getByPlaceholderText('Enter value');
|
|
|
|
|
fireEvent.change(inputElement, { target: { value: 'newValue' } });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
// expect(mockOnValueUpdate).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(mockOnValueUpdate).toHaveBeenCalledWith(
|
|
|
|
|
'testVariable',
|
2023-12-15 13:10:02 +05:30
|
|
|
'test_variable',
|
2023-11-30 13:56:49 +05:30
|
|
|
'newValue',
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-05-02 18:51:24 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('renders a Select element when variable type is CUSTOM', () => {
|
|
|
|
|
render(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={mockCustomVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={customVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByTitle('ALL')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('calls useEffect when the component mounts', () => {
|
|
|
|
|
render(
|
2023-11-30 13:56:49 +05:30
|
|
|
<MockQueryClientProvider>
|
|
|
|
|
<VariableItem
|
|
|
|
|
variableData={mockCustomVariableData}
|
|
|
|
|
existingVariables={{}}
|
|
|
|
|
onValueUpdate={mockOnValueUpdate}
|
2024-04-15 11:11:14 +05:30
|
|
|
variablesToGetUpdated={[]}
|
2023-11-30 13:56:49 +05:30
|
|
|
/>
|
|
|
|
|
</MockQueryClientProvider>,
|
2023-05-02 18:51:24 +05:30
|
|
|
);
|
|
|
|
|
|
2023-05-19 13:14:32 +05:30
|
|
|
expect(useEffect).toHaveBeenCalled();
|
2023-05-02 18:51:24 +05:30
|
|
|
});
|
|
|
|
|
});
|