|
| 1 | +import React from 'react'; |
| 2 | +import { Provider } from 'react-redux'; |
| 3 | + |
| 4 | +import { mergeConfig } from '@edx/frontend-platform'; |
| 5 | +import { injectIntl, IntlProvider } from '@edx/frontend-platform/i18n'; |
| 6 | +import { mount } from 'enzyme'; |
| 7 | +import { BrowserRouter as Router } from 'react-router-dom'; |
| 8 | +import configureStore from 'redux-mock-store'; |
| 9 | + |
| 10 | +import { COUNTRY_CODE_KEY, COUNTRY_DISPLAY_KEY } from './validator'; |
| 11 | +import { CountryField } from '../index'; |
| 12 | + |
| 13 | +const IntlCountryField = injectIntl(CountryField); |
| 14 | +const mockStore = configureStore(); |
| 15 | + |
| 16 | +jest.mock('react-router-dom', () => { |
| 17 | + const mockNavigation = jest.fn(); |
| 18 | + |
| 19 | + // eslint-disable-next-line react/prop-types |
| 20 | + const Navigate = ({ to }) => { |
| 21 | + mockNavigation(to); |
| 22 | + return <div />; |
| 23 | + }; |
| 24 | + |
| 25 | + return { |
| 26 | + ...jest.requireActual('react-router-dom'), |
| 27 | + Navigate, |
| 28 | + mockNavigate: mockNavigation, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +describe('CountryField', () => { |
| 33 | + let props = {}; |
| 34 | + let store = {}; |
| 35 | + |
| 36 | + const reduxWrapper = children => ( |
| 37 | + <IntlProvider locale="en"> |
| 38 | + <Provider store={store}>{children}</Provider> |
| 39 | + </IntlProvider> |
| 40 | + ); |
| 41 | + |
| 42 | + const routerWrapper = children => ( |
| 43 | + <Router> |
| 44 | + {children} |
| 45 | + </Router> |
| 46 | + ); |
| 47 | + |
| 48 | + const initialState = { |
| 49 | + register: {}, |
| 50 | + }; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + store = mockStore(initialState); |
| 54 | + props = { |
| 55 | + countryList: [{ |
| 56 | + [COUNTRY_CODE_KEY]: 'PK', |
| 57 | + [COUNTRY_DISPLAY_KEY]: 'Pakistan', |
| 58 | + }], |
| 59 | + selectedCountry: { |
| 60 | + countryCode: '', |
| 61 | + displayValue: '', |
| 62 | + }, |
| 63 | + errorMessage: '', |
| 64 | + onChangeHandler: jest.fn(), |
| 65 | + handleErrorChange: jest.fn(), |
| 66 | + onFocusHandler: jest.fn(), |
| 67 | + }; |
| 68 | + window.location = { search: '' }; |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + jest.clearAllMocks(); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Test Country Field', () => { |
| 76 | + mergeConfig({ |
| 77 | + SHOW_CONFIGURABLE_EDX_FIELDS: true, |
| 78 | + }); |
| 79 | + |
| 80 | + const emptyFieldValidation = { |
| 81 | + country: 'Select your country or region of residence', |
| 82 | + }; |
| 83 | + |
| 84 | + it('should run country field validation when onBlur is fired', () => { |
| 85 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 86 | + countryField.find('input[name="country"]').simulate('blur', { target: { value: '', name: 'country' } }); |
| 87 | + expect(props.handleErrorChange).toHaveBeenCalledTimes(1); |
| 88 | + expect(props.handleErrorChange).toHaveBeenCalledWith( |
| 89 | + 'country', |
| 90 | + emptyFieldValidation.country, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not run country field validation when onBlur is fired by drop-down arrow icon click', () => { |
| 95 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 96 | + countryField.find('input[name="country"]').simulate('blur', { |
| 97 | + target: { value: '', name: 'country' }, |
| 98 | + relatedTarget: { type: 'button', className: 'btn-icon pgn__form-autosuggest__icon-button' }, |
| 99 | + }); |
| 100 | + expect(props.handleErrorChange).toHaveBeenCalledTimes(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should update errors for frontend validations', () => { |
| 104 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 105 | + |
| 106 | + countryField.find('input[name="country"]').simulate('blur', { target: { value: '', name: 'country' } }); |
| 107 | + expect(props.handleErrorChange).toHaveBeenCalledTimes(1); |
| 108 | + expect(props.handleErrorChange).toHaveBeenCalledWith( |
| 109 | + 'country', |
| 110 | + emptyFieldValidation.country, |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should clear error on focus', () => { |
| 115 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 116 | + |
| 117 | + countryField.find('input[name="country"]').simulate('focus', { target: { value: '', name: 'country' } }); |
| 118 | + expect(props.handleErrorChange).toHaveBeenCalledTimes(1); |
| 119 | + expect(props.handleErrorChange).toHaveBeenCalledWith( |
| 120 | + 'country', |
| 121 | + '', |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should update state from country code present in redux store', () => { |
| 126 | + store = mockStore({ |
| 127 | + ...initialState, |
| 128 | + register: { |
| 129 | + ...initialState.register, |
| 130 | + backendCountryCode: 'PK', |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 135 | + expect(props.onChangeHandler).toHaveBeenCalledTimes(1); |
| 136 | + expect(props.onChangeHandler).toHaveBeenCalledWith( |
| 137 | + { target: { name: 'country' } }, |
| 138 | + { countryCode: 'PK', displayValue: 'Pakistan' }, |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should set option on dropdown menu item click', () => { |
| 143 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 144 | + |
| 145 | + countryField.find('.pgn__form-autosuggest__icon-button').first().simulate('click'); |
| 146 | + countryField.find('.dropdown-item').first().simulate('click'); |
| 147 | + |
| 148 | + expect(props.onChangeHandler).toHaveBeenCalledTimes(1); |
| 149 | + expect(props.onChangeHandler).toHaveBeenCalledWith( |
| 150 | + { target: { name: 'country' } }, |
| 151 | + { countryCode: 'PK', displayValue: 'Pakistan' }, |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should set value on change', () => { |
| 156 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 157 | + |
| 158 | + countryField.find('input[name="country"]').simulate( |
| 159 | + 'change', { target: { value: 'pak', name: 'country' } }, |
| 160 | + ); |
| 161 | + |
| 162 | + expect(props.onChangeHandler).toHaveBeenCalledTimes(1); |
| 163 | + expect(props.onChangeHandler).toHaveBeenCalledWith( |
| 164 | + { target: { name: 'country' } }, |
| 165 | + { countryCode: '', displayValue: 'pak' }, |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should display error on invalid country input', () => { |
| 170 | + props = { |
| 171 | + ...props, |
| 172 | + errorMessage: 'country error message', |
| 173 | + }; |
| 174 | + |
| 175 | + const countryField = mount(routerWrapper(reduxWrapper(<IntlCountryField {...props} />))); |
| 176 | + |
| 177 | + expect(countryField.find('div[feedback-for="country"]').text()).toEqual('country error message'); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments