Skip to content

Commit d125b0b

Browse files
committed
Validate Before Displaying Errors
When we show error messages because of a blur event we can sometimes show messages that aren't valid for the current state. By running the validator we ensure this doesn't happen. The blur event that displays the validator only needs to fire when focus leaves the element the first time, on subsequent focusout we'd be doing extra validation work. Adding the once option here causes the event to be removed as soon as it is called, which is nice because it's one less thing.
1 parent 748a3ce commit d125b0b

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

packages/frontend/tests/integration/components/curriculum-inventory/report-header-test.gjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,26 @@ module('Integration | Component | curriculum-inventory/report-header', function
107107
);
108108
await component.finalize();
109109
});
110+
111+
test('#i6159 validation status', async function (assert) {
112+
this.set('report', this.report);
113+
await render(
114+
<template>
115+
<ReportHeader @report={{this.report}} @canUpdate={{true}} @finalize={{(noop)}} />
116+
</template>,
117+
);
118+
assert.ok(component.name.isVisible);
119+
assert.strictEqual(component.name.value, 'Report name');
120+
await component.name.edit();
121+
await component.name.set('');
122+
await component.name.save();
123+
assert.ok(component.name.hasError);
124+
await component.name.cancel();
125+
assert.strictEqual(component.name.value, 'Report name');
126+
await component.name.edit();
127+
assert.notOk(component.name.hasError);
128+
assert.strictEqual(component.name.inputValue, 'Report name');
129+
await component.name.blur();
130+
assert.notOk(component.name.hasError);
131+
});
110132
});

packages/frontend/tests/pages/components/curriculum-inventory/report-header.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
attribute,
3+
blurrable,
34
clickable,
45
create,
56
fillable,
67
isPresent,
78
isVisible,
89
property,
910
text,
11+
value,
1012
} from 'ember-cli-page-object';
1113

1214
const definition = {
@@ -22,6 +24,8 @@ const definition = {
2224
save: clickable('.done'),
2325
cancel: clickable('.cancel'),
2426
hasError: isPresent('[data-test-name-validation-error-message]'),
27+
inputValue: value('input'),
28+
blur: blurrable('input'),
2529
},
2630
downloadLink: {
2731
scope: '[data-test-download]',

packages/ilios-common/addon-test-support/ilios-common/page-objects/components/course/header.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { create, clickable, fillable, text, isVisible } from 'ember-cli-page-object';
1+
import {
2+
blurrable,
3+
create,
4+
clickable,
5+
fillable,
6+
text,
7+
isVisible,
8+
value,
9+
} from 'ember-cli-page-object';
210
import publicationMenu from './publication-menu';
311
import publicationStatus from '../publication-status';
412
const definition = {
@@ -10,6 +18,8 @@ const definition = {
1018
set: fillable('input'),
1119
save: clickable('.done'),
1220
cancel: clickable('.cancel'),
21+
inputValue: value('input'),
22+
blur: blurrable('input'),
1323
hasError: isVisible('.validation-error-message'),
1424
},
1525
academicYear: text('[data-test-academic-year]'),

packages/ilios-common/addon/classes/yup-validations.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ export default class YupValidations {
128128
element.addEventListener(
129129
'focusout',
130130
() => {
131+
this.#validate();
131132
this.makeErrorsVisibleFor.perform([field]);
132133
},
133-
{ passive: true },
134+
{ passive: true, once: true },
134135
);
135136
element.addEventListener(
136137
'input',

packages/test-app/tests/integration/components/course/header-test.gjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,25 @@ module('Integration | Component | course/header', function (hooks) {
147147
await render(<template><Header @course={{this.course}} @editable={{true}} /></template>);
148148
assert.strictEqual(component.academicYear, '2021 - 2022');
149149
});
150+
151+
test('#i6159 validation status', async function (assert) {
152+
const course = this.server.create('course');
153+
const courseModel = await this.store.findRecord('course', course.id);
154+
this.set('course', courseModel);
155+
await render(<template><Header @course={{this.course}} @editable={{true}} /></template>);
156+
157+
assert.ok(component.title.isVisible);
158+
assert.strictEqual(component.title.value, 'course 0');
159+
await component.title.edit();
160+
await component.title.set('');
161+
await component.title.save();
162+
assert.ok(component.title.hasError);
163+
await component.title.cancel();
164+
assert.strictEqual(component.title.value, 'course 0');
165+
await component.title.edit();
166+
assert.notOk(component.title.hasError);
167+
assert.strictEqual(component.title.inputValue, 'course 0');
168+
await component.title.blur();
169+
assert.notOk(component.title.hasError);
170+
});
150171
});

0 commit comments

Comments
 (0)