Skip to content

Commit

Permalink
enhance alignment - ProductsQueryBuilder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richie-ni committed Jan 29, 2025
1 parent ed83a0b commit 6141fd0
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { render } from "@testing-library/react";
describe('ProductsQueryBuilder', () => {
describe('useEffects', () => {
let reactNode: ReactNode

const containerClass = 'smart-filter-group-condition-container';
const workspace = { id: '1', name: 'Selected workspace' } as Workspace;
const partNumber = ['partNumber1', 'partNumber2'];

function renderElement(workspaces: Workspace[], partNumbers: string[], filter?: string, globalVariableOptions: QueryBuilderOption[] = []) {
reactNode = React.createElement(ProductsQueryBuilder, { filter, workspaces, partNumbers, globalVariableOptions, onChange: jest.fn(), });
const renderResult = render(reactNode);
Expand All @@ -20,36 +22,46 @@ describe('ProductsQueryBuilder', () => {

it('should render empty query builder', () => {
const { renderResult, conditionsContainer } = renderElement([], [], '');

expect(conditionsContainer.length).toBe(1);
expect(renderResult.findByLabelText('Empty condition row')).toBeTruthy();
})

it('should select workspace in query builder', () => {
const { conditionsContainer } = renderElement([workspace], partNumber, 'Workspace = "1" && PartNumber = "partNumber1"');

expect(conditionsContainer?.length).toBe(2);
expect(conditionsContainer.item(0)?.textContent).toContain(workspace.name);
expect(conditionsContainer.item(1)?.textContent).toContain("partNumber1");
})

it('should select part number in query builder', () => {
const { conditionsContainer } = renderElement([workspace], partNumber, 'PartNumber = "partNumber1"');

expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.textContent).toContain("partNumber1");
});

it('should select global variable option', () => {
const globalVariableOption = { label: 'Global variable', value: 'global_variable' };
const { conditionsContainer } = renderElement([workspace], partNumber, 'PartNumber = \"global_variable\"', [globalVariableOption]);

expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.textContent).toContain(globalVariableOption.label);
});

[['${__from:date}', 'From'], ['${__to:date}', 'To'], ['${__now:date}', 'Now']].forEach(([value, label]) => {
it(`should select user friendly value for updated date`, () => {
const { conditionsContainer } = renderElement([workspace], partNumber, `UpdatedAt > \"${value}\"`);

expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.textContent).toContain(label);
});
});

it('should sanitize fields in query builder', () => {
const { conditionsContainer } = renderElement([workspace], partNumber, 'Family = "<script>alert(\'Family\')</script>"');

expect(conditionsContainer?.length).toBe(1);
expect(conditionsContainer.item(0)?.innerHTML).not.toContain('alert(\'Family\')');
})
Expand Down

0 comments on commit 6141fd0

Please sign in to comment.