From 2109197fc7492e90ae9fa42db4d93d10fd500d3c Mon Sep 17 00:00:00 2001 From: Soorya Kumaran C <90232857+SooryaKumaranC-tw@users.noreply.github.com> Date: Fri, 4 Aug 2023 11:56:06 +0530 Subject: [PATCH] BAH-3139 | Fix DOB flakiness (#154) --- src/containers/CreatePerson.js | 97 +++++++++++++---------------- src/containers/CreatePerson.test.js | 70 +++++++++++++++++++++ 2 files changed, 113 insertions(+), 54 deletions(-) diff --git a/src/containers/CreatePerson.js b/src/containers/CreatePerson.js index ad78d8e..6f3e735 100644 --- a/src/containers/CreatePerson.js +++ b/src/containers/CreatePerson.js @@ -26,6 +26,11 @@ class CreatePerson extends Component { lastName: '', gender: '', birthdate: moment(), + age: { + years: 0, + months: 0, + days: 0 + }, birthdateEstimated: false, organization: '', email: '', @@ -85,7 +90,28 @@ class CreatePerson extends Component { handleChange = ({ target: input }) => { const person = { ...this.state.person }; - person[input.name] = input.value; + if (input.name === 'birthdate') { + var birthdate = input.value; + const today = moment(); + person.age.years = moment.duration(today.diff(birthdate)).years(); + person.age.months = moment.duration(today.diff(birthdate)).months(); + person.age.days = moment.duration(today.diff(birthdate)).days(); + person.birthdate = birthdate; + } else if ( + input.name === 'years' || + input.name === 'months' || + input.name === 'days' + ) { + person.age[input.name] = input.value; + const now = moment(); + const currentDOB = moment() + .year(now.year() - person.age.years) + .month(now.month() - person.age.months) + .date(now.date() - person.age.days); + person.birthdate = currentDOB.format('YYYY-MM-DD'); + } else { + person[input.name] = input.value; + } this.setState({ person }); }; @@ -101,50 +127,6 @@ class CreatePerson extends Component { }); }; - fromAgetoDate = e => { - // input name: years, months or days - let inputName = e.target.name; - // the user input for the years or months or days - let inputValue = e.target.value; - - // mapping the values with the momentsjs required format - const getMomentFormat = { - year: 'years', - month: 'months', - day: 'days' - }; - // takes two dates (now and current birthdate input) and calculates - // the difference between them in years, months and days - function toAge(date) { - let now = moment(); - let userPickedDate = moment(date); - const diffDuration = moment.duration(now.diff(userPickedDate)); - const age = { - year: diffDuration.years(), - month: diffDuration.months(), - day: diffDuration.days() - }; - return age; - } - - this.setState(prevState => { - const prevBirthdate = prevState.person.birthdate; - - const toAgeObject = toAge(prevBirthdate); - let diff = inputValue - toAgeObject[inputName]; - - const person = { ...this.state.person }; - person.birthdate = moment(prevBirthdate) - .subtract(diff, getMomentFormat[inputName]) - .subtract(1, 'days') - .format('YYYY-MM-DD'); - - return { - person - }; - }); - }; - handleClearForm() { this.setState({ person: { @@ -160,6 +142,11 @@ class CreatePerson extends Component { occupation: '', gender: '', birthdate: moment(), + age: { + years: 0, + months: 0, + days: 0 + }, birthdateEstimated: false }, isRequestError: false @@ -347,6 +334,8 @@ class CreatePerson extends Component { birthdateEstimated } = this.state.person; + const { years, months, days } = this.state.person.age; + const { isRequestError, isRequestLoading, @@ -457,11 +446,11 @@ class CreatePerson extends Component { { let wrapper; @@ -58,4 +59,73 @@ describe('CreatePerson', () => { expect(wrapper.state().person.gender).toEqual(exampleGenderSelected); }); }); // end of gender options describe + + describe('the user populates birthdate', () => { + const exampleDOB = '2019-07-01'; + let dobInput; + + beforeEach(() => { + dobInput = wrapper.find('#birthdate'); + dobInput.simulate('change', { + target: { value: exampleDOB, name: 'birthdate' } + }); + }); + + it('should update the state property birthdate', () => { + expect(wrapper.state().person.birthdate).toEqual(exampleDOB); + }); + + it('requires birthdate input', () => { + expect(dobInput.props().required).toBe(true); + }); + + it('should update the state property age', () => { + const age = { years: 0, months: 0, days: 0 }; + const today = moment(); + age.years = moment.duration(today.diff(exampleDOB)).years(); + age.months = moment.duration(today.diff(exampleDOB)).months(); + age.days = moment.duration(today.diff(exampleDOB)).days(); + expect(wrapper.state().person.age.years).toEqual(age.years); + expect(wrapper.state().person.age.months).toEqual(age.months); + expect(wrapper.state().person.age.days).toEqual(age.days); + }); + }); // end of birthdate describe + + describe('the user populates age (year, month and day)', () => { + const exampleYears = 4; + const exampleMonths = 1; + const exampleDays = 2; + let yearsInput, monthsInput, daysInput; + beforeEach(() => { + yearsInput = wrapper.find('#age'); + monthsInput = wrapper.find('#months'); + daysInput = wrapper.find('#days'); + yearsInput.simulate('change', { + target: { value: exampleYears, name: 'years' } + }); + monthsInput.simulate('change', { + target: { value: exampleMonths, name: 'months' } + }); + daysInput.simulate('change', { + target: { value: exampleDays, name: 'days' } + }); + }); + + it('should update the state property years, months and days', () => { + expect(wrapper.state().person.age.years).toEqual(exampleYears); + expect(wrapper.state().person.age.months).toEqual(exampleMonths); + expect(wrapper.state().person.age.days).toEqual(exampleDays); + }); + + it('should update the state property age', () => { + const now = moment(); + const dob = moment() + .year(now.year() - exampleYears) + .month(now.month() - exampleMonths) + .date(now.date() - exampleDays); + expect(wrapper.state().person.birthdate).toEqual( + dob.format('YYYY-MM-DD') + ); + }); + }); // end of birthdate describe }); // end of outer describe