Skip to content

Instantly share code, notes, and snippets.

@duggiefresh
Created April 6, 2015 18:17
Show Gist options
  • Save duggiefresh/c9c6833927436c225e61 to your computer and use it in GitHub Desktop.
Save duggiefresh/c9c6833927436c225e61 to your computer and use it in GitHub Desktop.
Test all the properties!
import Ember from 'ember';
import { test } from 'ember-qunit';
// var map = Ember.EnumerableUtils.map;
var forEach = Ember.EnumerableUtils.forEach;
var run = Ember.run;
function validateValues(object, propertyName, values, isTestForValid) {
var promise = null;
var validatedValues = [];
forEach(values, function(value) {
// return validateValue(object, propertyName, value, isTestForValid, promise, thing);
function handleValidation(errors) {
var hasErrors = object.get('errors.' + propertyName + '.firstObject');
if ((hasErrors && !isTestForValid) || (!hasErrors && isTestForValid)) {
validatedValues.push(value);
}
}
run(object, 'set', propertyName, value);
var objectPromise = null;
run(function() {
objectPromise = object.validate().then(handleValidation, handleValidation);
});
promise = promise ? promise.then(objectPromise) : objectPromise;
});
return promise.then(function() {
return validatedValues;
});
}
function testPropertyValues(propertyName, values, isTestForValid, context) {
var validOrInvalid = (isTestForValid ? 'Valid' : 'Invalid');
var testName = validOrInvalid + ' ' + propertyName;
test(testName, function() {
var object = this.subject();
if (context && typeof context === 'function') {
context(object);
}
// Use QUnit.dump.parse so null and undefined can be printed as literal 'null' and
// 'undefined' strings in the assert message.
var valuesString = QUnit.dump.parse(values).replace(/\n(\s+)?/g, '').replace(/,/g, ', ');
var assertMessage = 'Expected ' + propertyName + ' to have ' + validOrInvalid.toLowerCase() +
' values: ' + valuesString;
return validateValues(object, propertyName, values, isTestForValid)
.then(function(validatedValues) {
deepEqual(validatedValues, values, assertMessage);
});
});
}
export function testValidPropertyValues(propertyName, values, context) {
testPropertyValues(propertyName, values, true, context);
}
export function testInvalidPropertyValues(propertyName, values, context) {
testPropertyValues(propertyName, values, false, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment