-
Notifications
You must be signed in to change notification settings - Fork 30
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
16700 Fixed validation in LimitedRestorationPanel component #258
Changes from 2 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 |
---|---|---|
|
@@ -45,7 +45,6 @@ | |
hide-spin-buttons | ||
:rules="monthsRules" | ||
:disabled="(radioValue !== 'customMonths')" | ||
@input="onMonthsInput($event)" | ||
/> | ||
</v-form> | ||
<span class="ml-2 mt-2 month-text">month(s)</span> | ||
|
@@ -84,9 +83,9 @@ export default class LimitedRestorationPanel extends Vue { | |
|
||
/** | ||
* Called when the component is mounted. | ||
* Sets the initial radio button and months input (if applicable). | ||
*/ | ||
mounted (): void { | ||
// set the initial radio button and months input (if applicable) | ||
if ([24, 18, 12, 6].includes(this.months)) { | ||
this.radioValue = this.months.toString() | ||
} else { | ||
|
@@ -96,49 +95,38 @@ export default class LimitedRestorationPanel extends Vue { | |
} | ||
|
||
/** | ||
* Called when months input has changed. | ||
* Called when radio value or input value have changed. | ||
*/ | ||
async onMonthsInput (input: string): Promise<void> { | ||
// ignore non-inputs | ||
if (input !== null) { | ||
// wait for component updates | ||
@Watch('radioValue') | ||
@Watch('inputValue') | ||
private async onValueChanged (): Promise<void> { | ||
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. The previous 2 methods fired alternately and not always in the same order due to waiting for the input component to update/validate. This now handles things more cleanly. |
||
if (this.radioValue === 'customMonths') { | ||
// wait for component updates then validate the input field | ||
await this.$nextTick() | ||
// validate the input | ||
const valid = this.$refs.monthsRef.validate() | ||
// emit validity | ||
|
||
// emit months and validity | ||
this.emitMonths(valid ? +this.inputValue : null) // emit null if invalid | ||
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. Nice. 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. Thanks. Yes, this way this component always emits something reasonable without events depending on other events (eg, only emit month if valid). 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. The other reason I did this is that it allows the parent component to do something different if invalid. In this case (next PR), the parent will clear any existing expiry date. |
||
this.emitValid(valid) | ||
// if valid, emit new value | ||
if (valid) this.emitMonths(Number(this.inputValue)) | ||
} | ||
} | ||
} else { | ||
// reset input field validation | ||
this.$refs.monthsRef.resetValidation() | ||
|
||
/** | ||
* Called when radio button has changed. | ||
*/ | ||
@Watch('radioValue') | ||
private onRadioValueChanged () { | ||
if (this.radioValue !== 'customMonths') { | ||
// reset months text field when another radio button is selected | ||
this.$refs.monthsRef.reset() | ||
// emit months and validity | ||
this.emitMonths(Number(this.radioValue)) | ||
this.emitMonths(+this.radioValue) | ||
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. The unary plus operator converts a string to a number more cleanly, imho. |
||
this.emitValid(true) | ||
} else if (!this.inputValue) { | ||
// as there is no input value, this component is invalid | ||
// otherwise, validation will be done in onMonthsInput() | ||
this.emitValid(false) | ||
} | ||
} | ||
|
||
/** | ||
* Emits the numbed of months selected. | ||
* Emits the number of months selected. | ||
*/ | ||
@Emit('months') | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
private emitMonths (months: number): void {} | ||
|
||
/** | ||
* Emits whether the number of months selected is valid. | ||
* Emits the component validity. | ||
*/ | ||
@Emit('valid') | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
|
@@ -148,6 +136,7 @@ export default class LimitedRestorationPanel extends Vue { | |
|
||
<style lang="scss" scoped> | ||
@import '@/assets/styles/theme.scss'; | ||
|
||
.month-text { | ||
color: $gray7; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,25 +27,23 @@ function createDefaultComponent ( | |
|
||
describe('Initialize RelationshipsPanel component', () => { | ||
it('loads the component', () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent() | ||
const wrapper = createDefaultComponent() | ||
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.
|
||
|
||
expect(wrapper.findComponent(LimitedRestorationPanel).exists()).toBe(true) | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
it('loads with a preset expiry (24 months)', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent(24) | ||
await Vue.nextTick() | ||
it('loads with a preset expiry (24 months)', () => { | ||
const wrapper = createDefaultComponent(24) | ||
|
||
expect(wrapper.vm.$data.radioValue).toEqual('24') | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
it('loads with a custom expiry (1 month)', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent(1) | ||
await Vue.nextTick() | ||
it('loads with a custom expiry (1 month)', () => { | ||
const wrapper = createDefaultComponent(1) | ||
|
||
expect(wrapper.vm.$data.radioValue).toEqual('customMonths') | ||
expect(wrapper.vm.$data.inputValue).toEqual('1') | ||
|
@@ -54,48 +52,49 @@ describe('Initialize RelationshipsPanel component', () => { | |
}) | ||
|
||
it('emits events when we select a preset expiry (24 months)', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent() | ||
await Vue.nextTick() | ||
const wrapper = createDefaultComponent() | ||
|
||
await wrapper.find('#radio-24').setChecked() | ||
|
||
expect(wrapper.emitted('valid').pop()[0]).toBe(true) | ||
expect(wrapper.emitted('months').pop()[0]).toEqual(24) | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
it('emits events when we select a custom expiry (1 month)', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent() | ||
await Vue.nextTick() | ||
const vm = wrapper.vm as any | ||
const wrapper = createDefaultComponent() | ||
|
||
await wrapper.find('#radio-custom').setChecked() | ||
await wrapper.find('#text-field-months').setValue('1') | ||
await Vue.nextTick() | ||
|
||
expect(wrapper.emitted('valid').pop()[0]).toBe(true) | ||
expect(wrapper.emitted('months').pop()[0]).toEqual(1) | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
it('emits valid=false when we select 25 months with a max of 24', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent() | ||
await Vue.nextTick() | ||
const vm = wrapper.vm as any | ||
const wrapper = createDefaultComponent() | ||
|
||
await wrapper.find('#radio-custom').setChecked() | ||
await wrapper.find('#text-field-months').setValue('25') | ||
|
||
expect(wrapper.emitted('valid').pop()[0]).toEqual(false) | ||
expect(wrapper.emitted('months').pop()[0]).toEqual(null) | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
it('emits valid=true when we select 25 months with a max of 36', async () => { | ||
const wrapper: Wrapper<LimitedRestorationPanel> = createDefaultComponent(undefined, 36) | ||
await Vue.nextTick() | ||
const vm = wrapper.vm as any | ||
const wrapper = createDefaultComponent(undefined, 36) | ||
|
||
await wrapper.find('#radio-custom').setChecked() | ||
await wrapper.find('#text-field-months').setValue('25') | ||
|
||
expect(wrapper.emitted('valid').pop()[0]).toEqual(true) | ||
expect(wrapper.emitted('months').pop()[0]).toEqual(25) | ||
|
||
wrapper.destroy() | ||
}) | ||
|
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.
Instead of this, I now watch the v-model property (inputValue).