-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move validated-input from ember-osf-preprints to this repo. Adding pa…
…ckages and a bunch of new tests.
- Loading branch information
Yuhuai Liu
authored and
Yuhuai Liu
committed
Nov 30, 2017
1 parent
6b4f40d
commit 9d9d9b6
Showing
8 changed files
with
403 additions
and
32 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 @@ | ||
--ignore-engines true |
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,67 @@ | ||
// This component is derived from ember-cp-validations. See https://github.com/offirgolan/ember-cp-validations for more information | ||
import Ember from 'ember'; | ||
import layout from './template'; | ||
const { | ||
isEmpty, | ||
computed, | ||
defineProperty, | ||
} = Ember; | ||
|
||
export default Ember.Component.extend({ | ||
layout, | ||
classNames: ['validated-input'], | ||
classNameBindings: ['showErrorClass:has-error', 'isValid:has-success'], | ||
model: null, | ||
value: null, | ||
type: 'text', | ||
//inputType === 'text' by default. Can be to set to other types if needed. | ||
//Currently supports 'text', 'textarea' and 'date'. | ||
inputType: 'text', | ||
valuePath: '', | ||
placeholder: '', | ||
validation: null, | ||
isTyping: false, | ||
|
||
init() { | ||
this._super(...arguments); | ||
var valuePath = this.get('valuePath'); | ||
defineProperty(this, 'validation', computed.oneWay(`model.validations.attrs.${valuePath}`)); | ||
defineProperty(this, 'value', computed.alias(`model.${valuePath}`)); | ||
}, | ||
|
||
isTextArea: computed('inputType', function () { | ||
return (this.get('inputType') === 'textarea'); | ||
}), | ||
isTextField: computed('inputType', function () { | ||
return (this.get('inputType') === 'text'); | ||
}), | ||
isDateField: computed('inputType', function () { | ||
return (this.get('inputType') === 'date'); | ||
}), | ||
showErrorMessage: computed('validation.isDirty', 'isInvalid', 'didValidate', function() { | ||
return (this.get('validation.isDirty') || this.get('didValidate')) && this.get('isInvalid'); | ||
}), | ||
|
||
notValidating: computed.not('validation.isValidating'), | ||
didValidate: computed.oneWay('targetObject.didValidate'), | ||
hasContent: computed.notEmpty('value'), | ||
|
||
isValid: computed.and('hasContent', 'validation.isValid', 'notValidating'), | ||
isInvalid: computed.oneWay('validation.isInvalid'), | ||
|
||
showWarningMessage: computed('validation.isDirty', 'validation.warnings.[]', 'isValid', 'didValidate', function() { | ||
return (this.get('validation.isDirty') || this.get('didValidate')) && this.get('isValid') && !isEmpty(this.get('validation.warnings')); | ||
}), | ||
|
||
keyPress(e) { | ||
if (e.keyCode === 13 && !this.get('large')) { | ||
e.preventDefault(); | ||
this.send('pressSubmit'); | ||
} | ||
}, | ||
actions: { | ||
pressSubmit() { | ||
this.sendAction('pressSubmit'); | ||
} | ||
} | ||
}); |
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,25 @@ | ||
<div class="form-group"> | ||
{{#if isTextArea}} | ||
{{textarea value=value type=type placeholder=placeholder class="form-control" name=valuePath}} | ||
{{/if}} | ||
|
||
{{#if isTextField}} | ||
{{input type=type value=value placeholder=placeholder class="form-control" name=valuePath}} | ||
{{/if}} | ||
|
||
{{#if isDateField}} | ||
{{bootstrap-datepicker type=type value=value placeholder=placeholder class="form-control" name=valuePath}} | ||
{{/if}} | ||
|
||
{{#if isValid}} | ||
<span class="valid-input"></span> | ||
{{/if}} | ||
|
||
<div class="input-error"> | ||
{{#if showErrorMessage}} | ||
<div class="error"> | ||
{{v-get model valuePath 'message'}} | ||
</div> | ||
{{/if}} | ||
</div> | ||
</div> |
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,2 @@ | ||
|
||
export { default } from 'ember-osf/components/validated-input/component'; |
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
58 changes: 58 additions & 0 deletions
58
tests/integration/components/validated-input/validated-input-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,58 @@ | ||
import Ember from 'ember'; | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
|
||
moduleForComponent('validated-input', 'Integration | Component | validated input', { | ||
integration: true | ||
}); | ||
|
||
function render(context, componentArgs) { | ||
return context.render(Ember.HTMLBars.compile(`{{validated-input | ||
model=context | ||
valuePath='fullName' | ||
placeholder='Full Name' | ||
value='' | ||
${componentArgs || ''} | ||
}}`)); | ||
} | ||
|
||
test('this renders', function(assert) { | ||
// checks that the component renders | ||
render(this); | ||
assert.ok(this.$('div').length); | ||
|
||
assert.equal(this.$('.valid-input').length, 0); | ||
assert.equal(this.$('.error').length, 0); | ||
assert.equal(this.$('.warning').length, 0); | ||
|
||
}); | ||
|
||
test('render valid', function(assert) { | ||
// simulates that the success element renders on success | ||
render(this, 'isValid=true'); | ||
|
||
assert.equal(this.$('.valid-input').length, 1); | ||
assert.equal(this.$('.error').length, 0); | ||
assert.equal(this.$('.warning').length, 0); | ||
}); | ||
|
||
test('render error message', function(assert) { | ||
// checks that the error message renders | ||
render(this, 'showErrorMessage=true'); | ||
|
||
assert.equal(this.$('.valid-input').length, 0); | ||
assert.equal(this.$('.error').length, 1); | ||
assert.equal(this.$('.warning').length, 0); | ||
}); | ||
|
||
// TODO: Test currently cannot find '.warning' | ||
/* | ||
test('render warning message', function(assert) { | ||
// checks that the warnng message renders | ||
render(this, 'showWarningMessage=true'); | ||
assert.equal(this.$('.valid-input').length, 0); | ||
assert.equal(this.$('.error').length, 0); | ||
assert.equal(this.$('.warning').length, 1); | ||
}); | ||
*/ |
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 |
---|---|---|
@@ -1,12 +1,148 @@ | ||
import Ember from 'ember'; | ||
import { moduleForModel, test } from 'ember-qunit'; | ||
|
||
moduleForModel('preprint', 'Unit | Model | preprint', { | ||
// Specify the other units that are required for this test. | ||
needs: ['model:contributor', 'model:file', 'model:file-provider', 'model:preprint-provider', 'model:action'] | ||
needs: ['model:contributor', | ||
'model:file', | ||
'model:file-provider', | ||
'model:preprint-provider', | ||
'model:action', | ||
'model:node', | ||
'model:license', | ||
'transform:fixstring', | ||
'transform:links', | ||
'transform:embed',] | ||
}); | ||
|
||
test('it exists', function(assert) { | ||
let model = this.subject(); | ||
// let store = this.store(); | ||
assert.ok(!!model); | ||
}); | ||
|
||
test('it has an attribute: title', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('title') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: subjects', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('subjects') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: dateCreated', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('dateCreated') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: datePublished', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('datePublished') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: originalPublicationDate', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('originalPublicationDate') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: dateModified', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('dateModified') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: doi', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('doi') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: isPublished', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('isPublished') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: isPreprintOrphan', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('isPreprintOrphan') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: licenseRecord', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('licenseRecord') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: reviewsState', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('reviewsState') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('it has an attribute: dateLastTransitioned', function(assert) { | ||
var model = this.subject(); | ||
var hasAttr = Object.keys(model.toJSON()).indexOf('dateLastTransitioned') > -1; | ||
assert.ok(hasAttr); | ||
}); | ||
|
||
test('node relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('node'); | ||
|
||
assert.equal(relationship.key, 'node'); | ||
assert.equal(relationship.type, 'node'); | ||
assert.equal(relationship.kind, 'belongsTo'); | ||
}); | ||
|
||
test('license relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('license'); | ||
|
||
assert.equal(relationship.key, 'license'); | ||
assert.equal(relationship.type, 'license'); | ||
assert.equal(relationship.kind, 'belongsTo'); | ||
}); | ||
|
||
test('file relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('primaryFile'); | ||
|
||
assert.equal(relationship.key, 'primaryFile'); | ||
assert.equal(relationship.type, 'file'); | ||
assert.equal(relationship.kind, 'belongsTo'); | ||
}); | ||
|
||
test('preprint-provider relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('provider'); | ||
|
||
assert.equal(relationship.key, 'provider'); | ||
assert.equal(relationship.type, 'preprint-provider'); | ||
assert.equal(relationship.kind, 'belongsTo'); | ||
}); | ||
|
||
test('action relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('actions'); | ||
|
||
assert.equal(relationship.key, 'actions'); | ||
assert.equal(relationship.type, 'action'); | ||
assert.equal(relationship.kind, 'hasMany'); | ||
}); | ||
|
||
test('contributors relationship', function(assert) { | ||
var model = this.store().modelFor('preprint'); | ||
var relationship = Ember.get(model, 'relationshipsByName').get('contributors'); | ||
|
||
assert.equal(relationship.key, 'contributors'); | ||
assert.equal(relationship.type, 'contributor'); | ||
assert.equal(relationship.kind, 'hasMany'); | ||
}); |
Oops, something went wrong.