Skip to content

Commit

Permalink
ERM-3395, Complete absent test cases for stripes-erm-components docum…
Browse files Browse the repository at this point in the history
…ent filter (#709)

* extend DocumentFilter tests

* extend tests

* extend tests for DocumentFilterFieldArray

* extend tests  for DocumentFilterRule

* tweak

* fix tests

* fix tests for DocumentFilterFieldArray

* test: DocumentFilterField test tweaks

* ci: Update centralised workflow to 1.6

* test: DocumentFilterFieldArray test tweaks

* test: Monireh's changes (not pushed originally)

* test: DocumentFilterForm test tweaks

* test: stripes-erm-testing testSelect

Make use of new testSelect feature in stripes-erm-testing (Bump major version of stripes-erm-testing)

* test: DocumentFilterRule test

Tweaked test case for DocumentFilterRule to align with describe-action, test-outcome paradigm

ERM-3395

* chore: Javascript-ify some JSON

---------

Co-authored-by: Ethan Freestone <[email protected]>
  • Loading branch information
MonireRasouli and EthanFreestone authored Nov 19, 2024
1 parent 241dc35 commit accf810
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 221 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
ui:
# Use the shared workflow from https://github.com/folio-org/.github
uses: folio-org/.github/.github/workflows/ui.yml@v1
uses: folio-org/.github/.github/workflows/ui.yml@v1.6
with:
sonar-sources: ./lib
generate-module-descriptor: false # Shared libraries do not need module descriptors, they're an okapi thing
Expand Down
52 changes: 19 additions & 33 deletions lib/DocumentFilter/DocumentFilter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MemoryRouter } from 'react-router-dom';
import userEvent from '@folio/jest-config-stripes/testing-library/user-event';

import {
Button,
Accordion,
Expand All @@ -9,6 +11,7 @@ import { Button as MockStripesButton } from '@folio/stripes/components';

import { waitFor } from '@folio/jest-config-stripes/testing-library/react';
import { translationsProperties } from '../../test/jest/helpers';
import { categoryValues } from '../../test/jest/resources';
import DocumentFilter from './DocumentFilter';

import { documentFilterParsing } from './testResources';
Expand Down Expand Up @@ -37,43 +40,22 @@ const filterHandlers = {
reset: () => {},
};


// Todo these should be centralised
const categoryValues = [
{
'id': '2c9180a09262108601926219be050022',
'value': 'consortium_negotiation_document',
'label': 'Consortium negotiation document',
},
{
'id': '2c9180a09262108601926219bdfc0020',
'value': 'license',
'label': 'License',
},
{
'id': '2c9180a09262108601926219be010021',
'value': 'misc',
'label': 'Misc',
},
];


let renderComponent;

describe('DocumentFilter', () => {
describe.each([
[
'with active filters',
documentFilterParsing.find(dfp => dfp.name === 'simple').deparsed,
'1 document filters applied',
documentFilterParsing.find(dfp => dfp.name === 'simple').parsed,
],
[
'without active filters',
{ docs: [] },
null,
[]
],
[
'with active filters',
documentFilterParsing.find(dfp => dfp.name === 'simple').deparsed,
'1 document filters applied',
documentFilterParsing.find(dfp => dfp.name === 'simple').parsed,
],
[
'with multiple active filters',
documentFilterParsing.find(dfp => dfp.name === 'complex').deparsed,
Expand Down Expand Up @@ -101,11 +83,11 @@ describe('DocumentFilter', () => {
);
});

test('renders the document filters accordion', async () => {
it('renders the document filters accordion', async () => {
await Accordion('Documents').exists();
});

test('renders expected filters text', async () => {
it('renders expected filters text', async () => {
const baseLayoutText = /document filters applied/;
const { queryByText } = renderComponent;
if (expectedLayoutText != null) {
Expand All @@ -120,9 +102,14 @@ describe('DocumentFilter', () => {
expect(getByText('DocumentFilterForm')).toBeInTheDocument();
});

// TODO
// Add test for clearing accordion, whether clear button exists etc (mock filterHandlers.state)
// and check the right value are passed when it's clicked
it('should clear the selected document filters by clicking on the clear button', () => {
const { getByRole, queryAllByTestId } = renderComponent;
if (expectedLayoutText !== null) {
expect(getByRole('button', { name: 'stripes-components.filterGroups.clearFilterSetLabel' })).toBeInTheDocument();
userEvent.click(getByRole('button', { name: 'stripes-components.filterGroups.clearFilterSetLabel' }));
expect(queryAllByTestId(/document filters applied\[.*\]/i).length).toEqual(0);
}
});

describe('testing parsedFilterData', () => {
beforeEach(async () => {
Expand All @@ -139,7 +126,6 @@ describe('DocumentFilter', () => {
});
});
});

// TODO
// Add test for passing in/not passing in labels
});
1 change: 1 addition & 0 deletions lib/DocumentFilter/DocumentFilterField.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const DocumentFilterField = ({
</FieldArray>
<Button
data-test-add-rule-btn
id={`add-rule-button-${name}`}
onClick={() => push(`${name}.rules`)}
>
<FormattedMessage id="stripes-erm-components.documentFilter.addRule" />
Expand Down
158 changes: 87 additions & 71 deletions lib/DocumentFilter/DocumentFilterField.test.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,112 @@
import { MemoryRouter } from 'react-router-dom';
import { FieldArray } from 'react-final-form-arrays';

import { renderWithIntl, Button, TestForm } from '@folio/stripes-erm-testing';
import { screen, waitFor } from '@folio/jest-config-stripes/testing-library/react';

Check warning on line 4 in lib/DocumentFilter/DocumentFilterField.test.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'screen' is defined but never used. Allowed unused vars must match /React/u
import { renderWithIntl, TestForm, Button } from '@folio/stripes-erm-testing';

import { translationsProperties } from '../../test/jest/helpers';
import { categoryValues } from '../../test/jest/resources';

import DocumentFilterField from './DocumentFilterField';
import { documentFilterParsing } from './testResources';

const onSubmit = jest.fn();

// These should be centralised
const categoryValues = [
{
'id': '2c9180a09262108601926219be050022',
'value': 'consortium_negotiation_document',
'label': 'Consortium negotiation document',
},
{
'id': '2c9180a09262108601926219bdfc0020',
'value': 'license',
'label': 'License',
},
{
'id': '2c9180a09262108601926219be010021',
'value': 'misc',
'label': 'Misc',
},
];
jest.mock('./DocumentFilterRule', () => () => <div>DocumentFilterRule</div>);

let renderComponent;
describe('DocumentFilterField', () => {
describe.each([
{
initalFilters: documentFilterParsing.find(dfp => dfp.name === 'complex').parsed,
expectedFields: 2
initialFilters: [],
expectedFields: 0,
expectedRules: 0,
},
{
initalFilters: documentFilterParsing.find(dfp => dfp.name === 'simple').parsed,
expectedFields: 1
initialFilters: documentFilterParsing.find((dfp) => dfp.name === 'simple')
.parsed,
expectedFields: 1,
expectedRules: 1,
},
{
initalFilters: [],
expectedFields: 0
}
])('Render DocumentFilterField with $expectedFields in the array', ({ initalFilters, expectedFields }) => {
beforeEach(() => {
renderComponent = renderWithIntl(
<MemoryRouter>
<TestForm
initialValues={{
filters: initalFilters
}}
onSubmit={onSubmit}
>
<FieldArray name="filters">
{({ fields }) => fields.map((name, index) => (
<DocumentFilterField
key={`${name}[${index}]`}
categoryValues={categoryValues}
filterName="docs"
index={index}
name={name}
/>
))}
</FieldArray>
</TestForm>
,
</MemoryRouter>,
translationsProperties
);
});
initialFilters: documentFilterParsing.find((dfp) => dfp.name === 'complex')
.parsed,
expectedFields: 2,
expectedRules: 3,
},
])(
'Render $expectedFields DocumentFilterField component(s)',
({ initialFilters, expectedFields, expectedRules }) => {
beforeEach(() => {
renderComponent = renderWithIntl(
<MemoryRouter>
<TestForm
initialValues={{
filters: initialFilters,
}}
onSubmit={onSubmit}
>
<FieldArray name="filters">
{({ fields }) => fields.map((name, index) => (
<DocumentFilterField
key={`${name}[${index}]`}
categoryValues={categoryValues}
filterName="docs"
index={index}
name={name}
/>
))
}
</FieldArray>
</TestForm>
,
</MemoryRouter>,
translationsProperties
);
});

it('displays attibute label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Attribute')).toHaveLength(expectedFields);
});
it('displays attribute label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Attribute')).toHaveLength(expectedFields);
});

it('displays operator label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Operator')).toHaveLength(expectedFields);
});

it('displays value label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Value')).toHaveLength(expectedFields);
});

it('displays operator label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Operator')).toHaveLength(expectedFields);
});
describe('DocumentFilterRule interactions', () => {
it(`renders ${expectedRules} DocumentFilterRule component${expectedRules !== 1 ? 's' : ''}`, () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('DocumentFilterRule')).toHaveLength(expectedRules);
});

it('displays value label(s)', () => {
const { queryAllByText } = renderComponent;
expect(queryAllByText('Value')).toHaveLength(expectedFields);
});
if (expectedFields === 0) {
it('does not render the add rule button', async () => {
await Button('Add rule').absent();
});
} else {
describe('clicking add rule button', () => {
beforeEach(async () => {
await waitFor(async () => {
// There will be multiple add rules for multiple DocumentFilterFields
await Button({ id: 'add-rule-button-filters[0]' }).click();
});
});

if (expectedFields > 0) {
test('renders the add rule button', async () => {
await Button('Add rule').exists();
test('a rule has been added', async () => {
const { queryAllByText } = renderComponent;
await waitFor(() => {
expect(queryAllByText('DocumentFilterRule')).toHaveLength(expectedRules + 1);
});
});
});
}
});
// TODO Mock DocumentFilterRule and check that you can add rules, that the right number show up and that they're separated by ANDs
}
});
);
});
5 changes: 3 additions & 2 deletions lib/DocumentFilter/DocumentFilterFieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const DocumentFilterFieldArray = ({
<FieldArray name="filters">
{({ fields }) => fields.map((name, index) => {
return (
<>
<div key={`document-filter-field-array-${name}`}>
<Card
key={`document-filter-card[${index}]`}
data-testid={`document-filter-field[${index}]`}
headerEnd={
fields?.length > 1 ? (
<Tooltip
Expand Down Expand Up @@ -79,7 +80,7 @@ const DocumentFilterFieldArray = ({
<FormattedMessage id="stripes-erm-components.OR" />
</Layout>
)}
</>
</div>
);
})
}
Expand Down
Loading

0 comments on commit accf810

Please sign in to comment.