2.Testing the app - dkapexhiu/to-do-app GitHub Wiki

Testing framework

The testing framework used is the Open Source Jasmine JavaScript Testing Framework. Jasmine allows for writing test in a behavior-driven development style and includes an assertion (expections) library and a test double (spy) library.

Test location

All tests are contained in the file located at test/ControllerSpec.js. New tests can be added (and existing edited) in this file.

Running the tests

The tests are easily run by opening the file test/SpecRunner.html in a browser.

Screenshot showing the tests completed

tests

Test 1: Should show entries on startup

it('should show entries on start-up', function () {
		var todo = {title: 'my todo'};
		setUpModel([todo]);
		subject.setView('');
		expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
});

Test 2: Should show active entries

it('should show active entries', function () {
		var todo = {title: 'my todo'};
		setUpModel([todo]);
		subject.setView('#/active');
		expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
});

Test 3: Should show completed entries

it('should show completed entries', function () {
		var todo = {title: 'my todo'};
		setUpModel([todo]);			
                subject.setView('#/completed');
		expect(view.render).toHaveBeenCalledWith('showEntries', [todo]);
});

Test 4: Should highlight "All" filter by default

it('should highlight "All" filter by default', function () {
		setUpModel([]);
		subject.setView('');
		expect(view.render).toHaveBeenCalledWith('setFilter', '');
});

Test 5: Should highlight "All" filter when switching to active view

it('should highlight "Active" filter when switching to active view', function () {
		setUpModel([]);
		subject.setView('#/active');
		expect(view.render).toHaveBeenCalledWith('setFilter', 'active');
});

Test 6: Should toggle all todos to completed

it('should toggle all todos to completed', function () {
		var todos = [{
		        id: 42,
		        title: 'my todo',
			completed: false
		}, {
			id: 21,
			title: 'another todo',
			completed: false
		}];

		setUpModel(todos);
		subject.setView('');
		view.trigger('toggleAll', {completed: true});

		expect(model.update).toHaveBeenCalledWith(42, {completed: true}, jasmine.any(Function));
		expect(model.update).toHaveBeenCalledWith(21, {completed: true}, jasmine.any(Function));
});

Test 7: Should update the view

it('should update the view', function () {
		var todos = [{
			id: 42,
			title: 'my todo',
			completed: true
		}];

		setUpModel(todos);
		subject.setView('');

		view.trigger('toggleAll', {completed: false});

		expect(view.render).toHaveBeenCalledWith('elementComplete', {id: 42, completed: false});
});

Test 8: Should add new todo to the view

it('should add a new todo to the model', function () {
		setUpModel([]);
		subject.setView('');

		view.trigger('newTodo', 'a new todo');

		expect(model.create).toHaveBeenCalledWith('a new todo', jasmine.any(Function));
});

Test 9: Should remove an entry from the model

it('should remove an entry from the model', function () {
		var todo = {id: 42, title: 'my todo', completed: true};
		setUpModel([todo]);

		subject.setView('');
		view.trigger('itemRemove', {id: 42});

		expect(model.remove).toHaveBeenCalledWith(42, jasmine.any(Function));
});