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

test(support-tools-page): add testing for support tools page #70

Merged
merged 1 commit into from
Nov 9, 2020

Conversation

awaisdar001
Copy link
Contributor

@awaisdar001 awaisdar001 commented Oct 29, 2020

Add Config for testing for the support tools app.

  • Adding Unit tests for Users app
  • Adding API error tests
  • Moving isEmail, isValidUsername to a common utils file.
  • Addressed Eslint errors
  • Move messages to messages.js (Only a few messages have been moved which are used in tests, in future, all can be moved error messages can be moved here).

Adds the following npm commands.

  • npm run debug-test => for running tests in debug mode.
  • npm run snapshot => updates the snapshots

Coverage from 0% => ~23%

https://openedx.atlassian.net/browse/PROD-2178

@codecov
Copy link

codecov bot commented Oct 29, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@5f485f8). Click here to learn what that means.
The diff coverage is 75.86%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #70   +/-   ##
=========================================
  Coverage          ?   21.86%           
=========================================
  Files             ?       28           
  Lines             ?      709           
  Branches          ?      152           
=========================================
  Hits              ?      155           
  Misses            ?      539           
  Partials          ?       15           
Impacted Files Coverage Δ
src/dates/formatDate.js 100.00% <ø> (ø)
src/index.jsx 0.00% <ø> (ø)
src/users/CourseSummary.jsx 12.50% <0.00%> (ø)
src/users/EnrollmentForm.jsx 12.50% <ø> (ø)
src/users/EntitlementForm.jsx 14.81% <ø> (ø)
src/users/Entitlements.jsx 2.94% <ø> (ø)
src/users/UserSummary.jsx 3.44% <ø> (ø)
src/users/data/api.js 26.80% <55.55%> (ø)
src/components/common/PageLoading.jsx 100.00% <100.00%> (ø)
src/support-home/SupportHomePage.jsx 100.00% <100.00%> (ø)
... and 31 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5f485f8...024293e. Read the comment docs.

Comment on lines 1 to 22
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SupportLinksPage matches snapshot 1`] = `
<main
className="container-fluid m-5"
>
<h3>
Support Tools
</h3>
<ul>
<li>
<a
href="/users"
id="search-users"
onClick={[Function]}
>
Search Users
</a>
</li>
</ul>
</main>
`;
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if this file should be committed. Is there a specific reason for this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this is the snapshot file which would be required for comparing current (at the time of running builds) and expected snapshot (this one)

Comment on lines 21 to 23
it('matches snapshot', () => {
const tree = renderer.create(<SupportLinksPage />).toJSON();
expect(tree).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this achieve?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

toMatchSnapshot(); this matches the current snapshot with expected (saved) snapshot.

@awaisdar001 awaisdar001 force-pushed the add-testing-for-support-tools branch 2 times, most recently from b8d4759 to 6025560 Compare November 3, 2020 11:03
Copy link
Contributor

@DawoudSheraz DawoudSheraz left a comment

Choose a reason for hiding this comment

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

For future reference, ideally, the component where the UI changes are more important should be tested via snapshots. For the simple components, the regular unit tests will be enough.

Comment on lines 7 to 12
it('does not render without message', () => {
const wrapper = shallow(<PageLoading srMessage="" />);

expect(wrapper.find('.sr-only')).toHaveLength(0);
});
it('does not render without message', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

both have the same description. 2nd should be renders correctly with message

expect(srElement).toHaveLength(1);
expect(srElement.text()).toEqual(message);
});
it('renders correctly', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

--> snapshot matches correctly


expect(searchUsersText).toEqual('Search Users');
});
it('have correct heading', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

has

it('correctly renders', () => {
expect(wrapper.find('li')).toHaveLength(1);
});
it('have search users link', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

has

Comment on lines 23 to 45
describe('Checking PropTypes', () => {
it('Should not throw a warning', () => {
const expectedProps = { location };
const propsError = checkProps(UserPage, expectedProps);

expect(propsError).toBeUndefined();
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there a separate describe for a single test?

});
});
//
it('Renders Correctly', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

snapshot match

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just to organize tests. I have added a separate test for proptypes tests.

);
});

it('using snapshot', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

matches snapshot

@awaisdar001 awaisdar001 force-pushed the add-testing-for-support-tools branch 3 times, most recently from 2a8f4ce to 6f244b6 Compare November 5, 2020 05:36
Copy link
Contributor

@DawoudSheraz DawoudSheraz left a comment

Choose a reason for hiding this comment

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

A few comments/questions to address


expect(formatedDate).toEqual('Sep 19, 2015 11:00 AM');
});
it('fallback to N/A if no args are passed', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: returns N/A if data is not provided

Comment on lines +1 to +4
export const USER_IDENTIFIER_INVALID_ERROR = 'The searched username or email is invalid. Please correct the username or email and try again.';
export const USERNAME_IDENTIFIER_NOT_FOUND_ERROR = 'We couldn\'t find a user with the username "{identifier}".';
export const USER_EMAIL_IDENTIFIER_NOT_FOUND_ERROR = 'We couldn\'t find a user with the email "{identifier}".';
export const UNKNOWN_API_ERROR = "There was an error loading this user's data. Check the JavaScript console for detailed errors.";
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know the current behavior but is there i18n setup for such texts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not aware of that, and these were not being translated currently. I am also not sure if there are plans adding translations for those strings.

Comment on lines +23 to +29
// Function to wait until the entire component is fully painted.
const waitForComponentToPaint = async (wrapper) => {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve));
wrapper.update();
});
};
Copy link
Contributor

Choose a reason for hiding this comment

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

What does component paint stands for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, it just means that component is fully rendered after all promise based function calls.

});
});
describe('shows expected error alert', () => {
const mockAuthApiToThrowError = (code) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

mockAuthResponseError or something similar to this.

Comment on lines 70 to 86
it('when user identifier is not found', async () => {
const validUsername = 'valid-non-existing-username';
location.search = `?username=${validUsername}`;
mockAuthApiToThrowError(404);
const expectedAlert = messages.USERNAME_IDENTIFIER_NOT_FOUND_ERROR.replace(
'{identifier}',
validUsername,
);

const wrapper = mount(<UserPageWrapper location={location} />);
await waitForComponentToPaint(wrapper);

const alert = wrapper.find('.alert');
expect(alert).toHaveLength(1);
expect(alert.text()).toEqual(expectedAlert);
});
it('when user email is not found', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't seen async usage inside tests before. Can you share an existing example from edX codebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


expect(propsError).toBeUndefined();
});
it('does throw error with undefined search handler', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

throws error. not need for does in the beginning.

const EMAIL_REGEX = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$';
const USERNAME_REGEX = '^[\\w.@_+-]+$';

export const isEmail = (value) => !!(value && value.match(EMAIL_REGEX));
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for !!. This operation is not affecting the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DawoudSheraz I have copied the methods as is, we can plan on refactoring in the other ticket. I can add todo here as well.


/* eslint-disable arrow-body-style */
export const isValidUsername = (searchValue) => {
return !!(searchValue && searchValue.match(USERNAME_REGEX));
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@awaisdar001
Copy link
Contributor Author

@DawoudSheraz updated the PR.

Copy link

@saadyousafarbi saadyousafarbi left a comment

Choose a reason for hiding this comment

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

looks good.

import formatDate from './formatDate';

describe('Format Date', () => {
it('correctly formats date', () => {
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 to test the utilities as well? I think the priority should be the React components for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, true, I did this to bump to coverage a little bit :) and they were easy enough

Copy link
Contributor

@asadazam93 asadazam93 left a comment

Choose a reason for hiding this comment

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

Looks good to me

@awaisdar001 awaisdar001 merged commit a5971d3 into master Nov 9, 2020
@awaisdar001 awaisdar001 deleted the add-testing-for-support-tools branch November 9, 2020 13:05
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