Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIREQ-937 create JEST/RTL test cases for RequestsFilters.js #1044

Closed
wants to merge 8 commits into from

Conversation

ssandupatla
Copy link
Contributor

UIREQ-937 create JEST/RTL test cases for RequestsFilters.js
URL: https://issues.folio.org/browse/UIREQ-937

@github-actions
Copy link

github-actions bot commented May 17, 2023

Jest Unit Test Statistics

    1 files  ±0    42 suites  +1   1m 40s ⏱️ +25s
441 tests +9  441 ✔️ +9  0 💤 ±0  0 ±0 
442 runs  +9  442 ✔️ +9  0 💤 ±0  0 ±0 

Results for commit 3f25922. ± Comparison against base commit 9c88a35.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented May 17, 2023

BigTest Unit Test Statistics

132 tests  ±0   132 ✔️ ±0   27s ⏱️ +8s
    1 suites ±0       0 💤 ±0 
    1 files   ±0       0 ±0 

Results for commit 3f25922. ± Comparison against base commit 9c88a35.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@artem-blazhko artem-blazhko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add changes to changelog file?
Could you check that RequestLevelFilter is rendered/not rendered?
Could you check that all necessary MultiSelectionFilter rendered?

@@ -0,0 +1,74 @@
import React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to import.

Comment on lines 6 to 13
jest.mock('@folio/stripes/components', () => ({
AccordionSet: jest.fn(({ children }) => <div>{children}</div>),
Accordion: jest.fn(({ displayClearButton, name, children, onClearFilter }) => (
<div>
<div>{displayClearButton ? <button type="button" onClick={() => onClearFilter()} data-testid={`clear-${name}`}>Clear</button> : null}</div>
<div data-testid={`accordion-${name}`}>{children}</div>
</div>
)),
FilterAccordionHeader: jest.fn(() => <div>FilterAccordionHeader</div>),
}));

jest.mock('@folio/stripes/smart-components', () => ({
CheckboxFilter: jest.fn(() => <div>CheckboxFilter</div>),
MultiSelectionFilter: jest.fn(() => <div>MultiSelectionFilter</div>),
}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have separate files with mocks - test/jest/__mock__.
You can add your mocks there if those mocks do not exist yet.

const { getByTestId } = render(<RequestsFilters {...props} />);
const requestTypeButton = getByTestId('clear-requestType');
expect(requestTypeButton).toBeInTheDocument();
userEvent.click(requestTypeButton);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to trigger click event here?
Is it necessary only for increasing code coverage?

const { getByTestId } = render(<RequestsFilters {...props} />);
const requestStatusButton = getByTestId('clear-requestStatus');
expect(requestStatusButton).toBeInTheDocument();
userEvent.click(requestStatusButton);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to trigger click event here?
Is it necessary only for increasing code coverage?

const { getByTestId } = render(<RequestsFilters {...props} />);
const tagsButton = getByTestId('clear-tags');
expect(tagsButton).toBeInTheDocument();
userEvent.click(tagsButton);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to trigger click event here?
Is it necessary only for increasing code coverage?

Comment on lines 42 to 51
it('renders the request type filter', () => {
const { getByTestId } = render(<RequestsFilters {...props} />);
const requestTypeButton = getByTestId('clear-requestType');
expect(requestTypeButton).toBeInTheDocument();
userEvent.click(requestTypeButton);
});
it('renders the request status filter', () => {
const { getByTestId } = render(<RequestsFilters {...props} />);
const requestStatusButton = getByTestId('clear-requestStatus');
expect(requestStatusButton).toBeInTheDocument();
userEvent.click(requestStatusButton);
});
it('renders the tags filter', () => {
const { getByTestId } = render(<RequestsFilters {...props} />);
const tagsButton = getByTestId('clear-tags');
expect(tagsButton).toBeInTheDocument();
userEvent.click(tagsButton);
});
Copy link
Contributor

@artem-blazhko artem-blazhko May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the first three tests have the same render approach.
Could you add beforeEach statement and call render(<RequestsFilters {...props} />) inside?

expect(tagsButton).toBeInTheDocument();
userEvent.click(tagsButton);
});
it('render the accordion pickup service points', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you mock PickupServicePointFilter and check that it is rendered. There is no sence to check rendering of inner components of PickupServicePointFilter. PickupServicePointFilter has or will have in future its own tests.

@sonarcloud
Copy link

sonarcloud bot commented Jun 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@artem-blazhko artem-blazhko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the rendering of CheckboxFilter?
From the code perspective, I see that CheckboxFilter rendered twice. Would be great to check each render.

Comment on lines 38 to 37
it('should click the onClear filters', () => {
userEvent.click(screen.getByTestId('clear-requestType'));
userEvent.click(screen.getByTestId('clear-requestStatus'));
userEvent.click(screen.getByTestId('clear-tags'));
expect(props.onClear).toHaveBeenCalled();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create 3 separate tests (1 for each "click" action) and check that onClear was called with correct argument?

it('should render the PickupServicePointFilter', () => {
expect(screen.getByText(/PickupServicePointFilter/i)).toBeInTheDocument();
});
describe('MultiSelectionFilter and RequestLevelFilter', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this describe is redundant.
Or at least if you want to have describe it makes sense to have one describe for each filter.

);
expect(getAllByText('MultiSelectionFilter')).toBeTruthy();
});
it('should render RequestLevelFilter', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a case when titleLevelRequestsFeatureEnabled is false and check that RequestLevelFilter is not rendered?

<div>
{label}
{children}
{/* <div>{<button type="button" onClick={() => onClearFilter()} data-testid={`clear-${name}`}>Clear</button> : null}</div> */}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need that comment?

@@ -8,4 +8,6 @@ jest.mock('@folio/stripes/smart-components', () => ({
SearchAndSort: jest.fn(() => null),
ViewMetaData: jest.fn(() => null),
withTags: jest.fn((WrappedComponent) => (props) => <WrappedComponent {...props} />),
// CheckboxFilter: jest.fn(() => <div>CheckboxFilter</div>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need that comment?

Comment on lines +35 to +34
beforeEach(() => {
render(<RequestsFilters {...props} />);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 51 and 65 have their own renders. But here on the line 36 you will have one more render. I think it is not the best approach.

I would suggest wrapping your first two tests in describe and executing beforeEach inside describe.

<div>
<button
type="button"
onClick={() => onClearFilter()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onClick={() => onClearFilter()}
onClick={onClearFilter}

@@ -2,14 +2,30 @@ import React from 'react';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert your changes that don't need for current pull request (mock for AccordionSet, ConfirmationModal, ErrorModal, Select)?
CC: @artem-blazhko

Copy link
Contributor

@Dmitriy-Litvinenko Dmitriy-Litvinenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please fix feedbacks?

@Dmitriy-Litvinenko
Copy link
Contributor

Could you please fix feedbacks?

Comment on lines 5 to 18
Accordion: jest.fn(({ children, label, name, onClearFilter }) => (
<div>
{label}
{children}
<div>
<button
type="button"
onClick={onClearFilter}
data-testid={`clear-${name}`}
>Clear
</button>
</div>
<div data-testid={`accordion-${name}`} />
</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Accordion: jest.fn(({ children, label, name, onClearFilter }) => (
<div>
{label}
{children}
<div>
<button
type="button"
onClick={onClearFilter}
data-testid={`clear-${name}`}
>Clear
</button>
</div>
<div data-testid={`accordion-${name}`} />
</div>
Accordion: jest.fn(({ children, label, name, onClearFilter }) => (
<div data-testid={`accordion-${name}`} >
{label}
{children}
<div>
<button
type="button"
onClick={onClearFilter}
data-testid={`clear-${name}`}
>Clear
</button>
</div>
</div>

{...props}
/>
)),
Checkbox: jest.fn(() => <div>Checkbox</div>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see the usage of that component. Do we really need to mock it?

Col: jest.fn(({ children }) => (
<div data-test-col>
{children}
</div>
)),
ConfirmationModal: jest.fn(() => <div>ConfirmationModal</div>),
ConfirmationModal: jest.fn(({ heading, confirmLabel, cancelLabel, onConfirm, onCancel }) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see the usage of that component. Do we really need to change the mock?

Datepicker: jest.fn(() => <div>Datepicker</div>),
ErrorModal: jest.fn(() => <div>ErrorModal</div>),
ErrorModal: jest.fn(({ label, content, buttonLabel, onClose }) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see the usage of that component. Do we really need to change the mock?

@@ -75,7 +99,6 @@ jest.mock('@folio/stripes-components', () => ({
)),
MultiColumnList: jest.fn(({ children }) => (
<div>
<div>MultiColumnList</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see the usage of that component. Do we really need to change the mock?

Comment on lines -133 to -180
TextField: jest.fn(({
label,
onChange,
validate = jest.fn(),
...rest
}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why do we need to change that mock?

onChange(e);
};

Select: ({ name, label, onChange, required, children, value }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why we need to change that mock?

</div>
);
}),
},
TextArea: jest.fn(() => <div>TextArea</div>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why we need to change that mock?

}),
},
TextArea: jest.fn(() => <div>TextArea</div>),
TextField: jest.fn(() => <div>TextField</div>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why we need to change that mock?

expect(screen.getByText(/PickupServicePointFilter/i)).toBeInTheDocument();
});
it('should render the MultiSelectionFilter', () => {
screen.debug();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need it here?

CHANGELOG.md Outdated
@@ -37,6 +37,7 @@
* Cover ItemInformation by jest/RTL tests. Refs UIREQ-949.
* Cover InstanceInformation by jest/RTL tests. Refs UIREQ-950.
* Fix inconsistency in RTL/Jest tests. Refs UIREQ-979.
* Create Jest/RTL test for RequestsFilters.js Refs UIREQ-937.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move it to the bottom of the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in Changelog.md

Comment on lines +8 to +35
Accordion.mockImplementation(jest.fn(({ children, label, name, onClearFilter }) => (
<div>
{label}
{children}
<div>
<button
type="button"
onClick={onClearFilter}
data-testid={`clear-${name}`}
>
Clear
</button>
</div>
<div data-testid={`accordion-${name}`} />
</div>
)));

AccordionSet.mockImplementation(jest.fn(({ children, onToggle }) => (
<div>
<button type="button" onClick={() => onToggle({ id: 'fulfillment-in-progress' })}>
Toggle in Progress
</button>
<button type="button" onClick={() => onToggle({ id: 'not-yet-filled' })}>
Toggle Not Yet Filled
</button>
{children}
</div>
)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add your changes to test/__mock__stripesComponents.mock.js file?
That file store all popular mock from components library.

Copy link
Contributor Author

@ssandupatla ssandupatla Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artem-blazhko - We have added the mock functions to the test file because other test cases in this module will fail if added test/__mock__stripesComponents.mock.js

@sonarcloud
Copy link

sonarcloud bot commented Jul 18, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@Dmitriy-Litvinenko
Copy link
Contributor

Thank you for yours pull request. We will use it for increase code coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants