Last active
August 20, 2018 20:10
-
-
Save VivekNayyar/46f96987cdd576e589c82a73b19c67f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('change and blur events', () => { | |
const noop = () => {}; | |
describe('handleCahange and handleBlur events', () => { | |
beforeAll(() => { | |
wrapper = mount( | |
<FormikEnhancedSessionForm | |
fields={{ | |
nationalId: '', | |
mobileNumber: '', | |
channel: '', | |
productType: 'cashloan' | |
}} | |
t={() => translatedText} | |
/> | |
); | |
}); | |
beforeEach(() => { | |
SessionFormInner = wrapper.find(SessionForm); | |
}); | |
test('should initialize Formik state and pass down props', () => { | |
const fields = { | |
nationalId: '', | |
mobileNumber: '', | |
channel: '', | |
productType: 'cashloan' | |
}; | |
expect(SessionFormInner.props().isSubmitting).toBe(false); | |
expect(SessionFormInner.props().touched).toEqual({}); | |
expect(SessionFormInner.props().values).toEqual(fields); | |
expect(SessionFormInner.props().errors).toEqual({}); | |
expect(SessionFormInner.props().dirty).toBe(false); | |
expect(SessionFormInner.props().isValid).toBe(false); | |
expect(SessionFormInner.props().submitCount).toBe(0); | |
}); | |
test('handleChange - should change the value in any form field and the update the field value', async () => { | |
let field = SessionFormInner.find('input').at(0); | |
let mockedOnChange = jest.spyOn(field.props(), 'onChange'); | |
field.simulate('change', { | |
persist: noop, | |
target: { | |
id: 'nationalId', | |
value: 'test' | |
} | |
}); | |
expect(mockedOnChange).toHaveBeenCalledTimes(1); | |
await asyncFlush(); | |
expect( | |
wrapper | |
.find(SessionForm) | |
.find('input') | |
.at(0) | |
.props().value | |
).toEqual('test'); | |
field = SessionFormInner.find('input').at(1); | |
field.simulate('change', { | |
persist: noop, | |
target: { | |
id: 'mobileNumber', | |
value: '+84567897650' | |
} | |
}); | |
await asyncFlush(); | |
expect( | |
wrapper | |
.find(SessionForm) | |
.find('input') | |
.at(1) | |
.props().value | |
).toEqual('+84567897650'); | |
}); | |
test('handleBlur - should set touched to true for every field that is blurred after touched', async () => { | |
let field = SessionFormInner.find('input').at(0); | |
field.simulate('blur', { | |
persist: noop, | |
target: { | |
id: 'nationalId', | |
value: 'test' | |
} | |
}); | |
await asyncFlush(); | |
wrapper.update(); | |
expect(wrapper.find(SessionForm).props().touched).toBeTruthy(); | |
}); | |
}); | |
}); | |
describe('validation events', () => { | |
beforeAll(() => { | |
wrapper = shallow( | |
<FormikEnhancedSessionForm | |
fields={{ | |
nationalId: '', | |
mobileNumber: '', | |
channel: '', | |
productType: 'cashloan' | |
}} | |
t={() => translatedText} | |
/> | |
); | |
}); | |
test('should call validate if present', async () => { | |
const validationSchema = jest.fn(); | |
console.log(wrapper.dive().prop('validationSchema')); | |
const mockedFormik = jest.spyOn(wrapper.dive().props(), 'validationSchema'); | |
mockedFormik.mockImplementation(validationSchema); | |
await wrapper | |
.find(SessionForm) | |
.props() | |
.submitForm(); | |
expect(validationSchema).toHaveBeenCalled(); | |
}); | |
}); | |
}); | |
const asyncFlush = () => new Promise(resolve => setTimeout(resolve, 0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment