I have a question regarding unit tests for a model.
The model is a photo album with photos, and the photos can have tags. The photo model has an attribute: amountOfTags.
the amountOfTaggedPhotos getter is supposed to return the amount of photos in the album that have tags
import Model, { hasMany, attr, belongsTo } from '@ember-data/model';
export default class PhotoAlbum extends Model {
// Properties
// Relations
@hasMany photos;
get amountOfTaggedPhotos() {
var counter = 0;
for (var photo of this.photos._objects) {
counter += photo.amountOfTags > 0 ? 1 : 0;
}
return counter;
}
get amountOfPhotos() {
return this.photos.length;
}
}
this works, but my test doesn’t
import { run } from '@ember/runloop';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
let album;
let photos = [];
module('Unit | Model | photo-album', function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
album = run(() => this.owner.lookup('service:store').createRecord('PhotoAlbum'));
photos[0] = run(() => this.owner.lookup('service:store').createRecord('Photo'));
photos[1] = run(() => this.owner.lookup('service:store').createRecord('Photo'));
photos[2] = run(() => this.owner.lookup('service:store').createRecord('Photo'));
});
test('Photo count', function (assert) {
assert.expect(1);
run(() => {
photos[0].setProperties({amountOfTags: 0});
photos[1].setProperties({amountOfTags: 1});
photos[2].setProperties({amountOfTags: 5});
album.setProperties({photos: photos});
assert.equal(album.get('amountOfTaggedPhotos'), 2, "Amount of tags in album is correct");
});
});
});
Now i know that setProperties({photos: photos}) doesn’t work here, but i don’t know how i should do it instead. Also is the rest of the code okay? or is there a better way of doing this?