-
Notifications
You must be signed in to change notification settings - Fork 144
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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' }, | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
@@ -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' } }, | ||
|
@@ -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( | ||
|
@@ -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' } }, | ||
|
@@ -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'); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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' } }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
@@ -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', | ||
|
@@ -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', () => { | ||
|
@@ -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', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it working ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes its working fine.