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

fix: Migrate Registration tests to RTL #1122

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Provider } from 'react-redux';

import { mergeConfig } from '@edx/frontend-platform';
import { injectIntl, IntlProvider } from '@edx/frontend-platform/i18n';
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import configureStore from 'redux-mock-store';

Expand Down Expand Up @@ -82,8 +82,13 @@ describe('CountryField', () => {
};

it('should run country field validation when onBlur is fired', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
countryField.find('input[name="country"]').simulate('blur', { target: { value: '', name: 'country' } });
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const countryInput = container.querySelector('input[name="country"]');

fireEvent.blur(countryInput, {
target: { value: '', name: 'country' },
});

expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'country',
Expand All @@ -92,8 +97,13 @@ describe('CountryField', () => {
});

it('should run country field validation when country name is invalid', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
countryField.find('input[name="country"]').simulate('blur', { target: { value: 'Pak', name: 'country' } });
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const countryInput = container.querySelector('input[name="country"]');

fireEvent.blur(countryInput, {
target: { value: 'Pak', name: 'country' },
});

expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'country',
Expand All @@ -102,34 +112,36 @@ describe('CountryField', () => {
});

it('should not run country field validation when onBlur is fired by drop-down arrow icon click', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
countryField.find('input[name="country"]').simulate('blur', {
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const countryInput = container.querySelector('input[name="country"]');
const dropdownArrowIcon = container.querySelector('.btn-icon.pgn__form-autosuggest__icon-button');

fireEvent.blur(countryInput, {
target: { value: '', name: 'country' },
relatedTarget: { type: 'button', className: 'btn-icon pgn__form-autosuggest__icon-button' },
Copy link
Contributor

Choose a reason for hiding this comment

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

is it working ?

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 its working fine.

relatedTarget: dropdownArrowIcon,
});

expect(props.handleErrorChange).toHaveBeenCalledTimes(0);
});

it('should update errors for frontend validations', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const countryInput = container.querySelector('input[name="country"]');

fireEvent.blur(countryInput, { target: { value: '', name: 'country' } });

countryField.find('input[name="country"]').simulate('blur', { target: { value: '', name: 'country' } });
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'country',
emptyFieldValidation.country,
);
expect(props.handleErrorChange).toHaveBeenCalledWith('country', emptyFieldValidation.country);
});

it('should clear error on focus', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const countryInput = container.querySelector('input[name="country"]');

fireEvent.focus(countryInput);
Copy link
Contributor

Choose a reason for hiding this comment

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

In one of test case focus was not working because it behaves async so we need to put in 'act' just want to confirm that is it working fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup. working perfectly without getting a console error.


countryField.find('input[name="country"]').simulate('focus', { target: { value: '', name: 'country' } });
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'country',
'',
);
expect(props.handleErrorChange).toHaveBeenCalledWith('country', '');
});

it('should update state from country code present in redux store', () => {
Expand All @@ -141,7 +153,9 @@ describe('CountryField', () => {
},
});

mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));

container.querySelector('input[name="country"]');
expect(props.onChangeHandler).toHaveBeenCalledTimes(1);
expect(props.onChangeHandler).toHaveBeenCalledWith(
{ target: { name: 'country' } },
Expand All @@ -150,10 +164,13 @@ describe('CountryField', () => {
});

it('should set option on dropdown menu item click', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));

const dropdownButton = container.querySelector('.pgn__form-autosuggest__icon-button');
fireEvent.click(dropdownButton);

countryField.find('.pgn__form-autosuggest__icon-button').first().simulate('click');
countryField.find('.dropdown-item').first().simulate('click');
const dropdownItem = container.querySelector('.dropdown-item');
fireEvent.click(dropdownItem);

expect(props.onChangeHandler).toHaveBeenCalledTimes(1);
expect(props.onChangeHandler).toHaveBeenCalledWith(
Expand All @@ -163,12 +180,13 @@ describe('CountryField', () => {
});

it('should set value on change', () => {
const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));

countryField.find('input[name="country"]').simulate(
'change', { target: { value: 'pak', name: 'country' } },
const { container } = render(
routerWrapper(reduxWrapper(<IntlCountryField {...props} />)),
);

const countryInput = container.querySelector('input[name="country"]');
fireEvent.change(countryInput, { target: { value: 'pak', name: 'country' } });

expect(props.onChangeHandler).toHaveBeenCalledTimes(1);
expect(props.onChangeHandler).toHaveBeenCalledWith(
{ target: { name: 'country' } },
Expand All @@ -182,9 +200,11 @@ describe('CountryField', () => {
errorMessage: 'country error message',
};

const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlCountryField {...props} />)));

expect(countryField.find('div[feedback-for="country"]').text()).toEqual('country error message');
const feedbackElement = container.querySelector('div[feedback-for="country"]');
expect(feedbackElement).toBeTruthy();
expect(feedbackElement.textContent).toEqual('country error message');
});
});
});
100 changes: 62 additions & 38 deletions src/register/RegistrationFields/EmailField/EmailField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Provider } from 'react-redux';

import { getConfig } from '@edx/frontend-platform';
import { injectIntl, IntlProvider } from '@edx/frontend-platform/i18n';
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import configureStore from 'redux-mock-store';

Expand Down Expand Up @@ -73,9 +73,10 @@ describe('EmailField', () => {
};

it('should run email field validation when onBlur is fired', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

emailField.find('input#email').simulate('blur', { target: { value: '', name: 'email' } });
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: '', name: 'email' } });
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'email',
Expand All @@ -84,9 +85,11 @@ describe('EmailField', () => {
});

it('should update errors for frontend validations', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'ab', name: 'email' } });

emailField.find('input#email').simulate('blur', { target: { value: 'ab', name: 'email' } });
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'email',
Expand All @@ -95,9 +98,11 @@ describe('EmailField', () => {
});

it('should clear error on focus', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

const emailInput = container.querySelector('input#email');
fireEvent.focus(emailInput, { target: { value: '', name: 'email' } });
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


emailField.find('input#email').simulate('focus', { target: { value: '', name: 'email' } });
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'email',
Expand All @@ -107,48 +112,59 @@ describe('EmailField', () => {

it('should call backend validation api on blur event, if frontend validations have passed', () => {
store.dispatch = jest.fn(store.dispatch);
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

// Enter a valid email so that frontend validations are passed
emailField.find('input#email').simulate('blur', { target: { value: 'test@gmail.com', name: 'email' } });
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'test@gmail.com', name: 'email' } });

expect(store.dispatch).toHaveBeenCalledWith(fetchRealtimeValidations({ email: 'test@gmail.com' }));
});

it('should give email suggestions for common service provider domain typos', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

emailField.find('input#email').simulate('blur', { target: { value: 'john@yopmail.com', name: 'email' } });
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'john@yopmail.com', name: 'email' } });

expect(emailField.find('#email-warning').text()).toEqual('Did you mean: john@hotmail.com?');
const emailWarning = container.querySelector('#email-warning');
expect(emailWarning.textContent).toEqual('Did you mean: john@hotmail.com?');
});

it('should be able to click on email suggestions and set it as value', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'john@yopmail.com', name: 'email' } });

const emailSuggestion = container.querySelector('.email-suggestion-alert-warning');
fireEvent.click(emailSuggestion);

emailField.find('input#email').simulate('blur', { target: { value: 'john@yopmail.com', name: 'email' } });
emailField.find('.email-suggestion-alert-warning').first().simulate('click');
expect(props.handleChange).toHaveBeenCalledTimes(1);
expect(props.handleChange).toHaveBeenCalledWith(
{ target: { name: 'email', value: 'john@hotmail.com' } },
);
});

it('should give error for common top level domain mistakes', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

emailField.find('input#email').simulate(
'blur', { target: { value: 'john@gmail.mistake', name: 'email' } },
);
expect(emailField.find('.alert-danger').text()).toEqual('Did you mean john@gmail.com?');
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'john@gmail.mistake', name: 'email' } });

const errorElement = container.querySelector('.alert-danger');
expect(errorElement.textContent).toEqual('Did you mean john@gmail.com?');
});

it('should give error and suggestion for invalid email', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'john@gmail', name: 'email' } });

const errorElement = container.querySelector('.alert-danger');
expect(errorElement.textContent).toEqual('Did you mean john@gmail.com?');

emailField.find('input#email').simulate(
'blur', { target: { value: 'john@gmail', name: 'email' } },
);
expect(emailField.find('.alert-danger').text()).toEqual('Did you mean john@gmail.com?');
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'email',
Expand All @@ -170,21 +186,29 @@ describe('EmailField', () => {
});

store.dispatch = jest.fn(store.dispatch);
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
emailField.find('input#email').simulate('focus', { target: { value: 'a@gmail.com', name: 'email' } });

const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

const emailInput = container.querySelector('input#email');
fireEvent.focus(emailInput, { target: { value: 'a@gmail.com', name: 'email' } });

expect(store.dispatch).toHaveBeenCalledWith(clearRegistrationBackendError('email'));
});

it('should clear email suggestions when close icon is clicked', () => {
const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));

emailField.find('input#email').simulate(
'blur', { target: { value: 'john@gmail.mistake', name: 'email' } },
);
expect(emailField.find('.alert-danger').text()).toEqual('Did you mean john@gmail.com?');
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'john@gmail.mistake', name: 'email' } });

emailField.find('.email-suggestion__close').at(0).simulate('click');
expect(emailField.find('.alert-danger').exists()).toBeFalsy();
const suggestionText = container.querySelector('.alert-danger');
expect(suggestionText.textContent).toEqual('Did you mean john@gmail.com?');

const closeButton = container.querySelector('.email-suggestion__close');
fireEvent.click(closeButton);

const closedSuggestionText = container.querySelector('.alert-danger');
expect(closedSuggestionText).toBeNull();
});

it('should set confirm email error if it exist', () => {
Expand All @@ -193,10 +217,10 @@ describe('EmailField', () => {
confirmEmailValue: 'confirmEmail@yopmail.com',
};

const emailField = mount(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
emailField.find('input#email').simulate(
'blur', { target: { value: 'differentEmail@yopmail.com', name: 'email' } },
);
const { container } = render(routerWrapper(reduxWrapper(<IntlEmailField {...props} />)));
const emailInput = container.querySelector('input#email');
fireEvent.blur(emailInput, { target: { value: 'differentEmail@yopmail.com', name: 'email' } });

expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'confirm_email',
Expand Down
Loading