-
-
Save jniechcial/9950a55393fdaceedf589cdf8c66ce4e to your computer and use it in GitHub Desktop.
Example of unit testing - Computed Properties
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
// ================= MODEL FILE | |
export default DS.Model.extend({ | |
// attributes | |
fields: attr(''), | |
values: attr(''), | |
types: attr(''), | |
options: attr(''), | |
// computed | |
properties: computed('fields.[]', 'values.[]', function() { | |
if(!this.get('fields')) { return []; } | |
return this.get('fields').map((item, index) => { | |
return ServiceDetailProperty.create({ | |
name: item, | |
value: this.get('values')[index], | |
type: this.get('types')[index], | |
options: this.get('options')[index] | |
}); | |
}); | |
}), | |
}); | |
// ================= END MODEL FILE | |
// ================= TEST FILE | |
test('it properly computes #properties', function(assert) { | |
const types = ['type1', 'type2']; | |
const values = ['value1', 'value2']; | |
const fields = ['name1', 'name2']; | |
const options = [['1', '2'], ['3', '4']]; | |
const model = this.subject({ types, values, fields, options }); | |
assert.equal(model.get('properties').length, 2, 'there are two properties computed'); | |
assert.equal( | |
model.get('properties.firstObject.name'), | |
'name1', | |
'property have proper name' | |
); | |
assert.equal( | |
model.get('properties.firstObject.value'), | |
'value1', | |
'property have proper value' | |
); | |
assert.equal( | |
model.get('properties.firstObject.type'), | |
'type1', | |
'property have proper type' | |
); | |
assert.deepEqual( | |
model.get('properties.firstObject.options'), | |
['1', '2'], | |
'property have proper options' | |
); | |
}); | |
// ================= END TEST FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment