4c7f4545c35ca4199d7e5775778e31d922de00ec
[JavaScriptForFun] / angularjs / showcase / src / showcase / app / cars / cars.service.spec.js
1 describe('app.cars', function() {
2   'use strict';
3
4   var $httpBackend;
5   var $log;
6   var id = 666;
7   var carResponseSuccess = {
8     content: 'Car: false',
9     id: id
10   };
11   var carsResponseSuccess = [
12     carResponseSuccess
13   ];
14   var carsResponseError = 'Error: connect ECONNREFUSED';
15   var cars;
16   var API;
17
18   beforeEach(function() {
19     module('app.cars');
20
21     inject(function(_$httpBackend_, _$log_, _cars_, _API_) {
22       $httpBackend = _$httpBackend_;
23       $log = _$log_;
24       cars = _cars_;
25       API = _API_;
26     });
27   });
28
29   describe('cars service', function () {
30
31     it('should invoke GET all cars without error', function () {
32       var respValue;
33
34       $httpBackend.expectGET(API.CARS).respond(200, carsResponseSuccess);
35       cars.getAll().then(function(resp) {
36         respValue = resp;
37       });
38       $httpBackend.flush();
39
40       expect(carsResponseSuccess).toEqual(respValue);
41     });
42
43     it('should invoke GET all cars with error', function () {
44       var reasonValue;
45
46       $httpBackend.expectGET(API.CARS).respond(400, carsResponseError);
47       cars.getAll().then(function() {}, function(reason) {
48         reasonValue = reason;
49       });
50       $httpBackend.flush();
51
52       expect(carsResponseError).toEqual(reasonValue);
53     });
54
55     it('should invoke GET car by id without error', function () {
56       var respValue;
57
58       $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(200, carResponseSuccess);
59       cars.getById(id).then(function(resp) {
60         respValue = resp.data;
61       });
62
63       $httpBackend.flush();
64
65       expect(carResponseSuccess).toEqual(respValue);
66     });
67
68     it('should invoke GET car by id with error', function () {
69       var reasonValue;
70
71       $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(400, carsResponseError);
72       cars.getById(id).then(function() {}, function(reason) {
73         reasonValue = reason.data;
74       });
75
76       $httpBackend.flush();
77
78       expect(carsResponseError).toEqual(reasonValue);
79     });
80
81   });
82
83   afterEach(function() {
84     $httpBackend.verifyNoOutstandingExpectation();
85     $httpBackend.verifyNoOutstandingRequest();
86   });
87
88 });