Skip to content

Instantly share code, notes, and snippets.

@Sinled
Last active March 22, 2017 10:06
Show Gist options
  • Save Sinled/d144ef6f4651d243115d590f55456868 to your computer and use it in GitHub Desktop.
Save Sinled/d144ef6f4651d243115d590f55456868 to your computer and use it in GitHub Desktop.
ember-cp-validations-validate-relations-with-debounce
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import DS from 'ember-data';
import {
validator, buildValidations
}
from 'ember-cp-validations';
const Validations = buildValidations({
type: {
description: 'Order Line Type',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
order: {
description: 'Order',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
selections: {
description: 'Order Selections',
validators: [
validator('ds-error'),
validator('has-many'),
validator('presence', true)
]
}
});
export default DS.Model.extend(Validations, {
order: DS.belongsTo('order', { async: true }),
selections: DS.hasMany('order-selection', { async: true }),
type: DS.attr('string')
});
import DS from 'ember-data';
import {
validator, buildValidations
}
from 'ember-cp-validations';
const Validations = buildValidations({
order: {
description: 'Order',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
selection: {
description: 'Order Selection',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
text: {
description: 'Question Text',
validators: [
validator('ds-error'),
validator('presence', {
presence: true,
debounce: 500
})
]
}
});
export default DS.Model.extend(Validations, {
order: DS.belongsTo('order', { async: true }),
selection: DS.belongsTo('order-selection', { async: true }),
text: DS.attr('string')
});
import DS from 'ember-data';
import {
validator, buildValidations
}
from 'ember-cp-validations';
const Validations = buildValidations({
quantity: {
description: 'Quantity',
validators: [
validator('ds-error'),
validator('number', {
gte: 1
})
]
},
order: {
description: 'Order',
validators: [
validator('ds-error'),
validator('belongs-to'),
validator('presence', true)
]
},
line: {
description: 'Order Line',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
questions: {
description: 'Order Selection Questions',
validators: [
validator('ds-error'),
validator('has-many'),
validator('length', {
min: 1
})
]
}
});
export default DS.Model.extend(Validations, {
order: DS.belongsTo('order', { async: true }),
line: DS.belongsTo('order-line', { async: true }),
questions: DS.hasMany('order-selection-question', { async: true }),
quantity: DS.attr('number')
});
import DS from 'ember-data';
import {
validator, buildValidations
}
from 'ember-cp-validations';
const Validations = buildValidations({
source: {
description: 'Order Source',
validators: [
validator('ds-error'),
validator('presence', true)
]
},
lines: {
description: 'Order Lines',
validators: [
validator('ds-error'),
validator('has-many'),
validator('presence', true)
]
}
});
export default DS.Model.extend(Validations, {
source: DS.attr('string'),
lines: DS.hasMany('order-line', { async: true })
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import Ember from 'ember';
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('order', 'Unit | Validations | Nested Model Relationships', {
needs: [
'validator:messages',
'validator:ds-error',
'validator:presence',
'validator:length',
'validator:number',
'validator:belongs-to',
'validator:has-many',
'model:order-line',
'model:order-selection',
'model:order-selection-question'
]
});
test('order with invalid question shows invalid', function(assert) {
assert.expect(11);
let order = this.subject({ source: 'external' });
let orderLine, orderSelection, orderSelectionQuestion;
let store = this.store();
Ember.run(() => {
orderLine = store.createRecord('order-line', { order, type: 'item' });
orderSelection = store.createRecord('order-selection', { order, line: orderLine, quantity: 1 });
orderSelectionQuestion = store.createRecord('order-selection-question', { order, selection: orderSelection });
store.createRecord('order-selection-question', { order, selection: orderSelection, text: 'foo' });
});
assert.equal(order.get('lines.length'), 1, 'Order has 1 Order Line');
assert.equal(orderLine.get('selections.length'), 1, 'Order Line has 1 Order Selection');
assert.equal(orderSelection.get('questions.length'), 2, 'Order Selection has 2 Order Selection Questions');
let done = assert.async();
order.validate().then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), false, 'Order should not be valid because of Order Selection Question');
return orderLine.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), false, 'Order Line should not be valid because of Order Selection Question');
return orderSelection.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), false, 'Order Selection should not be valid because of Order Selection Question');
return orderSelectionQuestion.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), false, 'Order Selection Question should not be valid');
done();
});
});
test('order with valid question shows valid', function(assert) {
assert.expect(11);
let order = this.subject({ source: 'external' });
let orderLine, orderSelection, orderSelectionQuestion;
let store = this.store();
Ember.run(() => {
orderLine = store.createRecord('order-line', { order, type: 'item' });
orderSelection = store.createRecord('order-selection', { order, line: orderLine, quantity: 1 });
orderSelectionQuestion = store.createRecord('order-selection-question', { order, selection: orderSelection, text: 'answer' });
});
assert.equal(order.get('lines.length'), 1, 'Order has 1 Order Line');
assert.equal(orderLine.get('selections.length'), 1, 'Order Line has 1 Order Selection');
assert.equal(orderSelection.get('questions.length'), 1, 'Order Selection has 1 Order Selection Question');
let done = assert.async();
order.validate().then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), true, 'Order should be valid because of Order Selection Question');
return orderLine.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), true, 'Order Line should be valid because of Order Selection Question');
return orderSelection.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), true, 'Order Selection should be valid because of Order Selection Question');
return orderSelectionQuestion.validate();
}).then(({ validations }) => {
assert.equal(validations.get('isValidating'), false, 'All promises should be resolved');
assert.equal(validations.get('isValid'), true, 'Order Selection Question should be valid');
done();
});
});
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-cp-validations": "3.2.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment