-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1854 from jrjohnson/1845-comtitle
Validate competency title
- Loading branch information
Showing
11 changed files
with
327 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Ember from 'ember'; | ||
import { validator, buildValidations } from 'ember-cp-validations'; | ||
import ValidationErrorDisplay from 'ilios/mixins/validation-error-display'; | ||
import { task } from 'ember-concurrency'; | ||
|
||
const { Component, RSVP, isPresent } = Ember; | ||
const { Promise } = RSVP; | ||
|
||
const Validations = buildValidations({ | ||
title: [ | ||
validator('presence', true), | ||
validator('length', { | ||
max: 200 | ||
}), | ||
], | ||
}); | ||
|
||
export default Component.extend(Validations, ValidationErrorDisplay, { | ||
didReceiveAttrs(){ | ||
this._super(...arguments); | ||
const competency = this.get('competency'); | ||
if (isPresent(competency)) { | ||
this.set('title', competency.get('title')); | ||
} | ||
}, | ||
title: null, | ||
competency: null, | ||
classNames: ['competency-title-editor'], | ||
tagName: 'span', | ||
save: task(function * (){ | ||
this.send('addErrorDisplayFor', 'title'); | ||
let {validations} = yield this.validate(); | ||
|
||
return new Promise((resolve, reject) => { | ||
if (validations.get('isInvalid')) { | ||
reject(); | ||
} else { | ||
const competency = this.get('competency'); | ||
const title = this.get('title'); | ||
if (isPresent(competency)) { | ||
competency.set('title', title); | ||
} | ||
this.send('clearErrorDisplay'); | ||
resolve(); | ||
} | ||
}); | ||
|
||
}), | ||
actions: { | ||
revert() { | ||
this.set('title', null); | ||
const competency = this.get('competency'); | ||
if (isPresent(competency)) { | ||
this.set('title', competency.get('title')); | ||
} | ||
}, | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Ember from 'ember'; | ||
import { validator, buildValidations } from 'ember-cp-validations'; | ||
import ValidationErrorDisplay from 'ilios/mixins/validation-error-display'; | ||
import { task, timeout } from 'ember-concurrency'; | ||
|
||
const { Component } = Ember; | ||
|
||
const Validations = buildValidations({ | ||
title: [ | ||
validator('presence', true), | ||
validator('length', { | ||
max: 200 | ||
}), | ||
], | ||
}); | ||
|
||
export default Component.extend(Validations, ValidationErrorDisplay, { | ||
title: null, | ||
classNames: ['new-competency'], | ||
save: task(function * (){ | ||
this.send('addErrorDisplayFor', 'title'); | ||
let {validations} = yield this.validate(); | ||
if (validations.get('isInvalid')) { | ||
return; | ||
} | ||
yield timeout(10); | ||
const title = this.get('title'); | ||
yield this.get('add')(title); | ||
this.send('clearErrorDisplay'); | ||
this.set('title', null); | ||
}) | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{#editable-field | ||
value=title | ||
save=(perform save) | ||
close=(action 'revert') | ||
as |isSaving save close| | ||
}} | ||
{{one-way-input | ||
value=title | ||
update=(action (mut title)) | ||
onenter=save | ||
onescape=close | ||
disabled=isSaving | ||
focusOut=(action 'addErrorDisplayFor' 'title') | ||
}} | ||
{{/editable-field}} | ||
{{#if (and (v-get this 'title' 'isInvalid') (is-in showErrorsFor 'title'))}} | ||
<span class="validation-error-message">{{v-get this 'title' 'message'}}</span> | ||
{{/if}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{{one-way-input | ||
value=title | ||
update=(action (mut title)) | ||
onenter=(perform save) | ||
onescape=(pipe (action 'removeErrorDisplayFor' 'title') (action (mut title) '')) | ||
focusOut=(action 'addErrorDisplayFor' 'title') | ||
keyDown=(action 'addErrorDisplayFor' 'title') | ||
placeholder=(t 'general.title') | ||
}} | ||
<button class='save text' {{action (perform save)}}> | ||
{{#if save.isRunning}} | ||
{{fa-icon 'spinner' spin=true}} | ||
{{else}} | ||
{{t 'general.add'}} | ||
{{/if}} | ||
</button> | ||
{{#if (and (v-get this 'title' 'isInvalid') (is-in showErrorsFor 'title'))}} | ||
<span class="validation-error-message">{{v-get this 'title' 'message'}}</span> | ||
{{/if}} |
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
34 changes: 34 additions & 0 deletions
34
tests/integration/components/competency-title-editor-test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Ember from 'ember'; | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
const { Object } = Ember; | ||
|
||
moduleForComponent('competency-title-editor', 'Integration | Component | competency title editor', { | ||
integration: true | ||
}); | ||
|
||
test('validation errors do not show up initially', function(assert) { | ||
assert.expect(1); | ||
let competency = Object.create({ | ||
title: 'test' | ||
}); | ||
this.set('competency', competency); | ||
this.render(hbs`{{competency-title-editor competency=competency}}`); | ||
assert.equal(this.$('.validation-error-message').length, 0); | ||
}); | ||
|
||
test('validation errors show up when saving', function(assert) { | ||
assert.expect(1); | ||
let competency = Object.create({ | ||
title: 'test' | ||
}); | ||
this.set('competency', competency); | ||
this.render(hbs`{{competency-title-editor competency=competency}}`); | ||
this.$('.content span:eq(0)').click(); | ||
this.$('input').val('').change(); | ||
this.$('button.done').click(); | ||
assert.equal(this.$('.validation-error-message').length, 1); | ||
return wait(); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
moduleForComponent('new-competency', 'Integration | Component | new competency', { | ||
integration: true | ||
}); | ||
|
||
test('it renders', function(assert) { | ||
this.render(hbs`{{new-competency}}`); | ||
|
||
assert.equal(this.$('input').length, 1); | ||
assert.equal(this.$('button').text().trim(), 'Add'); | ||
}); | ||
|
||
test('save', function(assert) { | ||
assert.expect(1); | ||
this.set('add', (value) => { | ||
assert.equal(value, 'new co'); | ||
}); | ||
this.render(hbs`{{new-competency add=(action add)}}`); | ||
this.$('input').val('new co').change(); | ||
this.$('button').click(); | ||
|
||
return wait(); | ||
}); | ||
|
||
test('validation errors do not show up initially', function(assert) { | ||
assert.expect(1); | ||
|
||
this.render(hbs`{{new-competency}}`); | ||
assert.equal(this.$('.validation-error-message').length, 0); | ||
}); | ||
|
||
test('validation errors show up when saving', function(assert) { | ||
assert.expect(1); | ||
|
||
this.render(hbs`{{new-competency}}`); | ||
this.$('button.save').click(); | ||
assert.equal(this.$('.validation-error-message').length, 1); | ||
return wait(); | ||
}); |
Oops, something went wrong.