Last active
September 27, 2017 19:37
-
-
Save nicksteffens/2cc4250fe1656a441b4aae7a19f5b91f to your computer and use it in GitHub Desktop.
This is a test helper for checking allows / rejects in ember-cp-validations
This file contains hidden or 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
import Ember from 'ember'; | |
import { test } from 'ember-qunit'; | |
function convertValuesToObjects(value) { | |
const data = { value }; | |
if ( value === undefined || value === null ) { | |
data.display = `${value}`; | |
} else { | |
data.display = `'${value}'`; | |
} | |
return data; | |
} | |
function callWithStore(store, functionToCall, defaultValue = {}) { | |
if (typeof functionToCall === 'function') { | |
return functionToCall(typeof store === 'function' && store()); | |
} else { | |
return defaultValue; | |
} | |
} | |
function runValidatorTestForKey(keyName, values, initialSetup, equalsValue) { | |
const allowsOrRejects = (equalsValue ? 'allow' : 'reject'); | |
const valuesStrings = values.map(convertValuesToObjects); | |
const testName = `${keyName} ${allowsOrRejects} [${ valuesStrings.map(({ display }) => display).join(', ')}]`; | |
test(testName, function(assert) { | |
let subject; | |
let options = {}; | |
Ember.run(function() { | |
options = callWithStore(this.store, initialSetup); | |
subject = this.subject(); | |
subject.setProperties(options); | |
}.bind(this)); | |
valuesStrings.forEach(function({ value, display }) { | |
Ember.run(function() { | |
const propValue = callWithStore(this.store, value, value); | |
subject.set(keyName, propValue); | |
}.bind(this)); | |
const isValid = subject.get(`validations.attrs.${keyName}.isValid`); | |
const messages = subject.get(`validations.attrs.${keyName}.messages`); | |
const additionalInformation = messages.length > 0 ? '. Errors: ' + messages : ''; | |
assert.equal( | |
isValid, equalsValue, | |
`Should ${allowsOrRejects} ${display}${additionalInformation}` | |
); | |
}.bind(this)); | |
}); | |
} | |
// NOTE: | |
// initialSetup has the store passed when calling use something like this: | |
// function(store) { | |
// return { | |
// attributeName: store.create('model'); | |
// }; | |
// } | |
// Store is available for model tests only! In other instances | |
// testForValidator('attrName', function(store) { | |
// Return a hash that will be passed to subject's `setProperties` | |
// return { | |
// someModel: store.createRecord('some-model'), | |
// someHasManyRelationship: [ | |
// store.createRecord('something'), | |
// store.createRecord('something'), | |
// ] | |
// }; | |
// }) | |
// Testing belongsTo or HasMany in toAllow: | |
// .toAllow( | |
// store => store.createRecord('model_name') | |
// ) | |
export function testForValidator(keyName, initialSetup) { | |
return { | |
toAllow: function() { | |
runValidatorTestForKey(keyName, [...arguments], initialSetup, true); | |
return this; | |
}, | |
toReject: function() { | |
runValidatorTestForKey(keyName, [...arguments], initialSetup, false); | |
return this; | |
}, | |
}; | |
} | |
export const FALSY_VALUES = [ | |
null, | |
'', | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment