|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | +global jasmine, describe, it, expect, beforeEach, afterEach, xdescribe, xit, |
| 5 | +spyOn |
| 6 | +*/ |
| 7 | +// Get the code you want to test |
| 8 | +var Controller = require('../instructor.controller'); |
| 9 | +var $ = require('jquery'); |
| 10 | +var matchers = require('jasmine-jquery-matchers'); |
| 11 | +// Test suite |
| 12 | +console.log('test instructor.controller'); |
| 13 | +describe('Instructor controller', function(){ |
| 14 | + var controller; |
| 15 | + |
| 16 | + beforeEach(function(){ |
| 17 | + controller = new Controller(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('can be created', function(){ |
| 21 | + expect(controller).toBeDefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('when it is created', function(){ |
| 25 | + |
| 26 | + it('has the expected routes', function(){ |
| 27 | + expect(controller.routes).toEqual(jasmine.objectContaining({ |
| 28 | + 'instructors/:id': 'showInstructor' |
| 29 | + })); |
| 30 | + }); |
| 31 | + |
| 32 | + it('without a container option, uses body as the container', function(){ |
| 33 | + expect(controller.options.container).toEqual('body'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('with a container option, uses specified container', function(){ |
| 37 | + var ctrl = new Controller({container: '.newcontainer'}); |
| 38 | + expect(ctrl.options.container).toEqual('.newcontainer'); |
| 39 | + }); |
| 40 | + |
| 41 | + }); |
| 42 | + |
| 43 | + describe('when calling showInstructor', function(){ |
| 44 | + |
| 45 | + beforeEach(function(){ |
| 46 | + jasmine.addMatchers(matchers); |
| 47 | + }); |
| 48 | + |
| 49 | + var success = function(callbacks){ |
| 50 | + controller.model.set({'firstName': 'valid firstName', |
| 51 | + 'lastName': 'valid lastName', 'skills':'valid skills'}); |
| 52 | + callbacks.success(controller.model); |
| 53 | + }; |
| 54 | + |
| 55 | + var err = function(callbacks){ |
| 56 | + callbacks.error('error', controller.model); |
| 57 | + }; |
| 58 | + |
| 59 | + it('with a valid instructor id, fetches the model', function(){ |
| 60 | + spyOn(controller.model, 'fetch').and.callFake(success); |
| 61 | + var cb = function(err, view){ |
| 62 | + expect(err).toBeNull(); |
| 63 | + expect(controller.model.get('firstName')).toEqual('valid firstName'); |
| 64 | + expect(controller.model.get('lastName')).toEqual('valid lastName'); |
| 65 | + expect(controller.model.get('skills')).toEqual('valid skills'); |
| 66 | + }; |
| 67 | + |
| 68 | + controller.showInstructor(1, cb); |
| 69 | + |
| 70 | + }); |
| 71 | + |
| 72 | + it('with a valid instructor id, renders the view', function(){ |
| 73 | + spyOn(controller.model, 'fetch').and.callFake(success); |
| 74 | + spyOn(controller.view, 'render').and.callFake(function(){ |
| 75 | + controller.view.$el = 'fake render'; |
| 76 | + return controller.view; |
| 77 | + }); |
| 78 | + var cb = function(err, view){ |
| 79 | + expect($('body')).toHaveText(''); |
| 80 | + expect(view.cid).toEqual(controller.view.cid); |
| 81 | + }; |
| 82 | + controller.showInstructor(1, cb); |
| 83 | + }); |
| 84 | + |
| 85 | + it('with an invalid instructor id, renders an error message', function(){ |
| 86 | + spyOn(controller.model, 'fetch').and.callFake(err); |
| 87 | + var cb = function(err, view){ |
| 88 | + expect(err).toBeTruthy(); |
| 89 | + expect($('body')).toHaveText( |
| 90 | + 'There was a problem rendering this instructor'); |
| 91 | + }; |
| 92 | + controller.showInstructor('whatid', cb); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments