Commit 8f009e26 authored by Arthur Verschaeve's avatar Arthur Verschaeve

Vanillajs: fix indentation in `ControllerSpec.js`

parent fc9fdea9
/*global app, jasmine, describe, it, beforeEach, expect */ /*global app, jasmine, describe, it, beforeEach, expect */
describe('controller', function () { describe('controller', function () {
'use strict'; 'use strict';
var subject, model, view; var subject, model, view;
var setUpModel = function (todos) { var setUpModel = function (todos) {
model.read.and.callFake(function (query, callback) { model.read.and.callFake(function (query, callback) {
callback = callback || query; callback = callback || query;
callback(todos); callback(todos);
}); });
model.getCount.and.callFake(function (callback) { model.getCount.and.callFake(function (callback) {
var todoCounts = { var todoCounts = {
active: todos.filter(function (todo) { active: todos.filter(function (todo) {
return !todo.completed; return !todo.completed;
}).length, }).length,
completed: todos.filter(function (todo) { completed: todos.filter(function (todo) {
return !!todo.completed; return !!todo.completed;
}).length, }).length,
total: todos.length total: todos.length
}; };
callback(todoCounts); callback(todoCounts);
}); });
model.remove.and.callFake(function (id, callback) { model.remove.and.callFake(function (id, callback) {
callback(); callback();
}); });
model.create.and.callFake(function (title, callback) { model.create.and.callFake(function (title, callback) {
callback(); callback();
}); });
model.update.and.callFake(function (id, updateData, callback) { model.update.and.callFake(function (id, updateData, callback) {
callback(); callback();
}); });
}; };
var createViewStub = function () { var createViewStub = function () {
var eventRegistry = {}; var eventRegistry = {};
return { return {
render: jasmine.createSpy('render'), render: jasmine.createSpy('render'),
bind: function (event, handler) { bind: function (event, handler) {
eventRegistry[event] = handler; eventRegistry[event] = handler;
}, },
trigger: function (event, parameter) { trigger: function (event, parameter) {
eventRegistry[event](parameter); eventRegistry[event](parameter);
} }
}; };
}; };
beforeEach(function () { beforeEach(function () {
model = jasmine.createSpyObj('model', ['read', 'getCount', 'remove', 'create', 'update']); model = jasmine.createSpyObj('model', ['read', 'getCount', 'remove', 'create', 'update']);
view = createViewStub(); view = createViewStub();
subject = new app.Controller(model, view); subject = new app.Controller(model, view);
}); });
it('should show entries on start-up', function () { it('should show entries on start-up', function () {
setUpModel([]); setUpModel([]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('showEntries', []); expect(view.render).toHaveBeenCalledWith('showEntries', []);
}); });
describe('routing', function () { describe('routing', function () {
it('should show all entries without a route', function () { it('should show all entries without a route', function () {
var todo = {title: 'my todo'}; var todo = {title: 'my todo'};
setUpModel([todo]); setUpModel([todo]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('showEntries', [todo]); expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
}); });
it('should show all entries without "all" route', function () { it('should show all entries without "all" route', function () {
var todo = {title: 'my todo'}; var todo = {title: 'my todo'};
setUpModel([todo]); setUpModel([todo]);
subject.setView('#/'); subject.setView('#/');
expect(view.render).toHaveBeenCalledWith('showEntries', [todo]); expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
}); });
it('should show active entries', function () { it('should show active entries', function () {
var todo = {title: 'my todo', completed: false}; var todo = {title: 'my todo', completed: false};
setUpModel([todo]); setUpModel([todo]);
subject.setView('#/active'); subject.setView('#/active');
expect(model.read).toHaveBeenCalledWith({completed: false}, jasmine.any(Function)); expect(model.read).toHaveBeenCalledWith({completed: false}, jasmine.any(Function));
expect(view.render).toHaveBeenCalledWith('showEntries', [todo]); expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
}); });
it('should show completed entries', function () { it('should show completed entries', function () {
var todo = {title: 'my todo', completed: true}; var todo = {title: 'my todo', completed: true};
setUpModel([todo]); setUpModel([todo]);
subject.setView('#/completed'); subject.setView('#/completed');
expect(model.read).toHaveBeenCalledWith({completed: true}, jasmine.any(Function)); expect(model.read).toHaveBeenCalledWith({completed: true}, jasmine.any(Function));
expect(view.render).toHaveBeenCalledWith('showEntries', [todo]); expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
}); });
}); });
it('should show the content block when todos exists', function () { it('should show the content block when todos exists', function () {
setUpModel([{title: 'my todo', completed: true}]); setUpModel([{title: 'my todo', completed: true}]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('contentBlockVisibility', { expect(view.render).toHaveBeenCalledWith('contentBlockVisibility', {
visible: true visible: true
}); });
}); });
it('should hide the content block when no todos exists', function () { it('should hide the content block when no todos exists', function () {
setUpModel([]); setUpModel([]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('contentBlockVisibility', { expect(view.render).toHaveBeenCalledWith('contentBlockVisibility', {
visible: false visible: false
}); });
}); });
it('should check the toggle all button, if all todos are completed', function () { it('should check the toggle all button, if all todos are completed', function () {
setUpModel([{title: 'my todo', completed: true}]); setUpModel([{title: 'my todo', completed: true}]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('toggleAll', { expect(view.render).toHaveBeenCalledWith('toggleAll', {
checked: true checked: true
}); });
}); });
it('should set the "clear completed" button', function () { it('should set the "clear completed" button', function () {
var todo = {id: 42, title: 'my todo', completed: true}; var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]); setUpModel([todo]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('clearCompletedButton', { expect(view.render).toHaveBeenCalledWith('clearCompletedButton', {
completed: 1, completed: 1,
visible: true visible: true
}); });
}); });
it('should highlight "All" filter by default', function () { it('should highlight "All" filter by default', function () {
setUpModel([]); setUpModel([]);
subject.setView(''); subject.setView('');
expect(view.render).toHaveBeenCalledWith('setFilter', ''); expect(view.render).toHaveBeenCalledWith('setFilter', '');
}); });
it('should highlight "Active" filter when switching to active view', function () { it('should highlight "Active" filter when switching to active view', function () {
setUpModel([]); setUpModel([]);
subject.setView('#/active'); subject.setView('#/active');
expect(view.render).toHaveBeenCalledWith('setFilter', 'active'); expect(view.render).toHaveBeenCalledWith('setFilter', 'active');
}); });
describe('toggle all', function () { describe('toggle all', function () {
it('should toggle all todos to completed', function () { it('should toggle all todos to completed', function () {
var todos = [{ var todos = [{
id: 42, id: 42,
title: 'my todo', title: 'my todo',
completed: false completed: false
}, { }, {
id: 21, id: 21,
title: 'another todo', title: 'another todo',
completed: false completed: false
}]; }];
setUpModel(todos);
subject.setView('');
view.trigger('toggleAll', {completed: true}); setUpModel(todos);
subject.setView('');
expect(model.update).toHaveBeenCalledWith(42, {completed: true}, jasmine.any(Function)); view.trigger('toggleAll', {completed: true});
expect(model.update).toHaveBeenCalledWith(21, {completed: true}, jasmine.any(Function));
});
it('should update the view', function () { expect(model.update).toHaveBeenCalledWith(42, {completed: true}, jasmine.any(Function));
var todos = [{ expect(model.update).toHaveBeenCalledWith(21, {completed: true}, jasmine.any(Function));
id: 42, });
title: 'my todo',
completed: true
}];
setUpModel(todos);
subject.setView('');
view.trigger('toggleAll', {completed: false}); it('should update the view', function () {
var todos = [{
id: 42,
title: 'my todo',
completed: true
}];
expect(view.render).toHaveBeenCalledWith('elementComplete', {id : 42, completed : false}); setUpModel(todos);
}); subject.setView('');
});
describe('new todo', function () { view.trigger('toggleAll', {completed: false});
it('should add a new todo to the model', function () {
setUpModel([]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('elementComplete', {id : 42, completed : false});
});
});
view.trigger('newTodo', 'a new todo'); describe('new todo', function () {
it('should add a new todo to the model', function () {
setUpModel([]);
expect(model.create).toHaveBeenCalledWith('a new todo', jasmine.any(Function)); subject.setView('');
});
it('should add a new todo to the view', function () { view.trigger('newTodo', 'a new todo');
setUpModel([]);
subject.setView(''); expect(model.create).toHaveBeenCalledWith('a new todo', jasmine.any(Function));
});
view.render.calls.reset(); it('should add a new todo to the view', function () {
model.read.calls.reset(); setUpModel([]);
model.read.and.callFake(function (callback) {
callback([{
title: 'a new todo',
completed: false
}]);
});
view.trigger('newTodo', 'a new todo'); subject.setView('');
expect(model.read).toHaveBeenCalled(); view.render.calls.reset();
model.read.calls.reset();
model.read.and.callFake(function (callback) {
callback([{
title: 'a new todo',
completed: false
}]);
});
expect(view.render).toHaveBeenCalledWith('showEntries', [{ view.trigger('newTodo', 'a new todo');
title: 'a new todo',
completed: false
}]);
});
it('should clear the input field when a new todo is added', function () { expect(model.read).toHaveBeenCalled();
setUpModel([]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('showEntries', [{
title: 'a new todo',
completed: false
}]);
});
view.trigger('newTodo', 'a new todo'); it('should clear the input field when a new todo is added', function () {
setUpModel([]);
expect(view.render).toHaveBeenCalledWith('clearNewTodo'); subject.setView('');
});
});
describe('element removal', function () { view.trigger('newTodo', 'a new todo');
it('should remove an entry from the model', function () {
var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('clearNewTodo');
view.trigger('itemRemove', {id: 42}); });
});
expect(model.remove).toHaveBeenCalledWith(42, jasmine.any(Function)); describe('element removal', function () {
}); it('should remove an entry from the model', function () {
var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
it('should remove an entry from the view', function () { subject.setView('');
var todo = {id: 42, title: 'my todo', completed: true}; view.trigger('itemRemove', {id: 42});
setUpModel([todo]);
subject.setView(''); expect(model.remove).toHaveBeenCalledWith(42, jasmine.any(Function));
view.trigger('itemRemove', {id: 42}); });
expect(view.render).toHaveBeenCalledWith('removeItem', 42); it('should remove an entry from the view', function () {
}); var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
it('should update the element count', function () { subject.setView('');
var todo = {id: 42, title: 'my todo', completed: true}; view.trigger('itemRemove', {id: 42});
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('removeItem', 42);
view.trigger('itemRemove', {id: 42}); });
expect(view.render).toHaveBeenCalledWith('updateElementCount', 0); it('should update the element count', function () {
}); var todo = {id: 42, title: 'my todo', completed: true};
}); setUpModel([todo]);
describe('remove completed', function () { subject.setView('');
it('should remove a completed entry from the model', function () { view.trigger('itemRemove', {id: 42});
var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('updateElementCount', 0);
view.trigger('removeCompleted'); });
});
expect(model.read).toHaveBeenCalledWith({completed: true}, jasmine.any(Function)); describe('remove completed', function () {
expect(model.remove).toHaveBeenCalledWith(42, jasmine.any(Function)); it('should remove a completed entry from the model', function () {
}); var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
it('should remove a completed entry from the view', function () { subject.setView('');
var todo = {id: 42, title: 'my todo', completed: true}; view.trigger('removeCompleted');
setUpModel([todo]);
subject.setView(''); expect(model.read).toHaveBeenCalledWith({completed: true}, jasmine.any(Function));
view.trigger('removeCompleted'); expect(model.remove).toHaveBeenCalledWith(42, jasmine.any(Function));
});
expect(view.render).toHaveBeenCalledWith('removeItem', 42); it('should remove a completed entry from the view', function () {
}); var todo = {id: 42, title: 'my todo', completed: true};
}); setUpModel([todo]);
describe('element complete toggle', function () { subject.setView('');
it('should update the model', function () { view.trigger('removeCompleted');
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView('');
view.trigger('itemToggle', {id: 21, completed: true}); expect(view.render).toHaveBeenCalledWith('removeItem', 42);
});
});
expect(model.update).toHaveBeenCalledWith(21, {completed: true}, jasmine.any(Function)); describe('element complete toggle', function () {
}); it('should update the model', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView('');
it('should update the view', function () { view.trigger('itemToggle', {id: 21, completed: true});
var todo = {id: 42, title: 'my todo', completed: true};
setUpModel([todo]);
subject.setView('');
view.trigger('itemToggle', {id: 42, completed: false}); expect(model.update).toHaveBeenCalledWith(21, {completed: true}, jasmine.any(Function));
});
expect(view.render).toHaveBeenCalledWith('elementComplete', {id: 42, completed: false}); it('should update the view', function () {
}); var todo = {id: 42, title: 'my todo', completed: true};
}); setUpModel([todo]);
subject.setView('');
describe('edit item', function () { view.trigger('itemToggle', {id: 42, completed: false});
it('should switch to edit mode', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('elementComplete', {id: 42, completed: false});
});
});
view.trigger('itemEdit', {id: 21}); describe('edit item', function () {
it('should switch to edit mode', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(view.render).toHaveBeenCalledWith('editItem', {id: 21, title: 'my todo'}); subject.setView('');
});
it('should leave edit mode on done', function () { view.trigger('itemEdit', {id: 21});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('editItem', {id: 21, title: 'my todo'});
});
view.trigger('itemEditDone', {id: 21, title: 'new title'}); it('should leave edit mode on done', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(view.render).toHaveBeenCalledWith('editItemDone', {id: 21, title: 'new title'}); subject.setView('');
});
it('should persist the changes on done', function () { view.trigger('itemEditDone', {id: 21, title: 'new title'});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('editItemDone', {id: 21, title: 'new title'});
});
view.trigger('itemEditDone', {id: 21, title: 'new title'}); it('should persist the changes on done', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(model.update).toHaveBeenCalledWith(21, {title: 'new title'}, jasmine.any(Function)); subject.setView('');
});
it('should remove the element from the model when persisting an empty title', function () { view.trigger('itemEditDone', {id: 21, title: 'new title'});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(model.update).toHaveBeenCalledWith(21, {title: 'new title'}, jasmine.any(Function));
});
view.trigger('itemEditDone', {id: 21, title: ''}); it('should remove the element from the model when persisting an empty title', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(model.remove).toHaveBeenCalledWith(21, jasmine.any(Function)); subject.setView('');
});
it('should remove the element from the view when persisting an empty title', function () { view.trigger('itemEditDone', {id: 21, title: ''});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(model.remove).toHaveBeenCalledWith(21, jasmine.any(Function));
});
view.trigger('itemEditDone', {id: 21, title: ''}); it('should remove the element from the view when persisting an empty title', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(view.render).toHaveBeenCalledWith('removeItem', 21); subject.setView('');
});
it('should leave edit mode on cancel', function () { view.trigger('itemEditDone', {id: 21, title: ''});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('removeItem', 21);
});
view.trigger('itemEditCancel', {id: 21}); it('should leave edit mode on cancel', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(view.render).toHaveBeenCalledWith('editItemDone', {id: 21, title: 'my todo'}); subject.setView('');
});
it('should not persist the changes on cancel', function () { view.trigger('itemEditCancel', {id: 21});
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
subject.setView(''); expect(view.render).toHaveBeenCalledWith('editItemDone', {id: 21, title: 'my todo'});
});
view.trigger('itemEditCancel', {id: 21}); it('should not persist the changes on cancel', function () {
var todo = {id: 21, title: 'my todo', completed: false};
setUpModel([todo]);
expect(model.update).not.toHaveBeenCalled(); subject.setView('');
});
}); view.trigger('itemEditCancel', {id: 21});
expect(model.update).not.toHaveBeenCalled();
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment