Merge pull request #4880 from jaswilli/client-model-tests

Add tests for admin client models
This commit is contained in:
Hannah Wolfe 2015-02-02 19:03:40 +00:00
commit 31880e7cfa
6 changed files with 303 additions and 1 deletions

View File

@ -1,6 +1,6 @@
/* global ic */
var ajax = window.ajax = function () {
var ajax = function () {
return ic.ajax.request.apply(null, arguments);
};

View File

@ -0,0 +1,80 @@
/* jshint expr:true */
import {
describeModel,
it
} from 'ember-mocha';
describeModel('post',
{
needs:['model:user', 'model:tag', 'model:role']
},
function () {
it('has a validation type of "post"', function () {
var model = this.subject();
expect(model.validationType).to.equal('post');
});
it('isPublished and isDraft are correct', function () {
var model = this.subject({
status: 'published'
});
expect(model.get('isPublished')).to.be.ok;
expect(model.get('isDraft')).to.not.be.ok;
Ember.run(function () {
model.set('status', 'draft');
expect(model.get('isPublished')).to.not.be.ok;
expect(model.get('isDraft')).to.be.ok;
});
});
it('isAuthoredByUser is correct', function () {
var model = this.subject({
author_id: 15
}),
user = Ember.Object.create({id: '15'});
expect(model.isAuthoredByUser(user)).to.be.ok;
Ember.run(function () {
model.set('author_id', 1);
expect(model.isAuthoredByUser(user)).to.not.be.ok;
});
});
it('updateTags removes and deletes old tags', function () {
var model = this.subject();
Ember.run(this, function () {
var modelTags = model.get('tags'),
tag1 = this.store().createRecord('tag', {id: '1'}),
tag2 = this.store().createRecord('tag', {id: '2'}),
tag3 = this.store().createRecord('tag');
// During testing a record created without an explicit id will get
// an id of 'fixture-n' instead of null
tag3.set('id', null);
modelTags.pushObject(tag1);
modelTags.pushObject(tag2);
modelTags.pushObject(tag3);
expect(model.get('tags.length')).to.equal(3);
model.updateTags();
expect(model.get('tags.length')).to.equal(2);
expect(model.get('tags.firstObject.id')).to.equal('1');
expect(model.get('tags').objectAt(1).get('id')).to.equal('2');
expect(tag1.get('isDeleted')).to.not.be.ok;
expect(tag2.get('isDeleted')).to.not.be.ok;
expect(tag3.get('isDeleted')).to.be.ok;
});
});
}
);

View File

@ -0,0 +1,22 @@
import {
describeModel,
it
} from 'ember-mocha';
describeModel('role', function () {
it('provides a lowercase version of the name', function () {
var model = this.subject({
name: 'Author'
});
expect(model.get('name')).to.equal('Author');
expect(model.get('lowerCaseName')).to.equal('author');
Ember.run(function () {
model.set('name', 'Editor');
expect(model.get('name')).to.equal('Editor');
expect(model.get('lowerCaseName')).to.equal('editor');
});
});
});

View File

@ -0,0 +1,12 @@
import {
describeModel,
it
} from 'ember-mocha';
describeModel('setting', function () {
it('has a validation type of "setting"', function () {
var model = this.subject();
expect(model.get('validationType')).to.equal('setting');
});
});

View File

@ -0,0 +1,12 @@
import {
describeModel,
it
} from 'ember-mocha';
describeModel('tag', function () {
it('has a validation type of "tag"', function () {
var model = this.subject();
expect(model.get('validationType')).to.equal('tag');
});
});

View File

@ -0,0 +1,176 @@
/*jshint expr:true */
import {
describeModel,
it
} from 'ember-mocha';
describeModel('user',
{
needs: ['model:role']
},
function () {
it('has a validation type of "user"', function () {
var model = this.subject();
expect(model.get('validationType')).to.equal('user');
});
it('active property is correct', function () {
var model = this.subject({
status: 'active'
});
expect(model.get('active')).to.be.ok;
['warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].forEach(function (status) {
Ember.run(function () {
model.set('status', status);
expect(model.get('status')).to.be.ok;
});
});
Ember.run(function () {
model.set('status', 'inactive');
expect(model.get('active')).to.not.be.ok;
});
Ember.run(function () {
model.set('status', 'invited');
expect(model.get('active')).to.not.be.ok;
});
});
it('invited property is correct', function () {
var model = this.subject({
status: 'invited'
});
expect(model.get('invited')).to.be.ok;
Ember.run(function () {
model.set('status', 'invited-pending');
expect(model.get('invited')).to.be.ok;
});
Ember.run(function () {
model.set('status', 'active');
expect(model.get('invited')).to.not.be.ok;
});
Ember.run(function () {
model.set('status', 'inactive');
expect(model.get('invited')).to.not.be.ok;
});
});
it('pending property is correct', function () {
var model = this.subject({
status: 'invited-pending'
});
expect(model.get('pending')).to.be.ok;
Ember.run(function () {
model.set('status', 'invited');
expect(model.get('pending')).to.not.be.ok;
});
Ember.run(function () {
model.set('status', 'inactive');
expect(model.get('pending')).to.not.be.ok;
});
});
it('role property is correct', function () {
var model,
role;
model = this.subject();
Ember.run(this, function () {
role = this.store().createRecord('role', {name: 'Author'});
model.get('roles').pushObject(role);
expect(model.get('role.name')).to.equal('Author');
});
Ember.run(this, function () {
role = this.store().createRecord('role', {name: 'Editor'});
model.set('role', role);
expect(model.get('role.name')).to.equal('Editor');
});
});
it('isAuthor property is correct', function () {
var model = this.subject();
Ember.run(this, function () {
var role = this.store().createRecord('role', {name: 'Author'});
model.set('role', role);
expect(model.get('isAuthor')).to.be.ok;
expect(model.get('isEditor')).to.not.be.ok;
expect(model.get('isAdmin')).to.not.be.ok;
expect(model.get('isOwner')).to.not.be.ok;
});
});
it('isEditor property is correct', function () {
var model = this.subject();
Ember.run(this, function () {
var role = this.store().createRecord('role', {name: 'Editor'});
model.set('role', role);
expect(model.get('isEditor')).to.be.ok;
expect(model.get('isAuthor')).to.not.be.ok;
expect(model.get('isAdmin')).to.not.be.ok;
expect(model.get('isOwner')).to.not.be.ok;
});
});
it('isAdmin property is correct', function () {
var model = this.subject();
Ember.run(this, function () {
var role = this.store().createRecord('role', {name: 'Administrator'});
model.set('role', role);
expect(model.get('isAdmin')).to.be.ok;
expect(model.get('isAuthor')).to.not.be.ok;
expect(model.get('isEditor')).to.not.be.ok;
expect(model.get('isOwner')).to.not.be.ok;
});
});
it('isOwner property is correct', function () {
var model = this.subject();
Ember.run(this, function () {
var role = this.store().createRecord('role', {name: 'Owner'});
model.set('role', role);
expect(model.get('isOwner')).to.be.ok;
expect(model.get('isAuthor')).to.not.be.ok;
expect(model.get('isAdmin')).to.not.be.ok;
expect(model.get('isEditor')).to.not.be.ok;
});
});
}
);