|
9 | 9 | $httpBackend,
|
10 | 10 | $stateParams,
|
11 | 11 | $location,
|
12 |
| - Authentication; |
| 12 | + Authentication, |
| 13 | + Articles, |
| 14 | + mockArticle; |
13 | 15 |
|
14 | 16 | // The $resource service augments the response object with methods for updating and deleting the resource.
|
15 | 17 | // If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
|
|
36 | 38 | // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
37 | 39 | // This allows us to inject a service but then attach it to a variable
|
38 | 40 | // with the same name as the service.
|
39 |
| - beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_) { |
| 41 | + beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_, _Articles_) { |
40 | 42 | // Set a new global scope
|
41 | 43 | scope = $rootScope.$new();
|
42 | 44 |
|
|
45 | 47 | $httpBackend = _$httpBackend_;
|
46 | 48 | $location = _$location_;
|
47 | 49 | Authentication = _Authentication_;
|
| 50 | + Articles = _Articles_; |
| 51 | + |
| 52 | + // create mock article |
| 53 | + mockArticle = new Articles({ |
| 54 | + _id: '525a8422f6d0f87f0e407a33', |
| 55 | + title: 'An Article about MEAN', |
| 56 | + content: 'MEAN rocks!' |
| 57 | + }); |
48 | 58 |
|
49 | 59 | // Mock logged in user
|
50 | 60 | Authentication.user = {
|
|
58 | 68 | }));
|
59 | 69 |
|
60 | 70 | it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Articles) {
|
61 |
| - // Create sample article using the Articles service |
62 |
| - var sampleArticle = new Articles({ |
63 |
| - title: 'An Article about MEAN', |
64 |
| - content: 'MEAN rocks!' |
65 |
| - }); |
66 |
| - |
67 | 71 | // Create a sample articles array that includes the new article
|
68 |
| - var sampleArticles = [sampleArticle]; |
| 72 | + var sampleArticles = [mockArticle]; |
69 | 73 |
|
70 | 74 | // Set GET response
|
71 | 75 | $httpBackend.expectGET('api/articles').respond(sampleArticles);
|
|
79 | 83 | }));
|
80 | 84 |
|
81 | 85 | it('$scope.findOne() should create an array with one article object fetched from XHR using a articleId URL parameter', inject(function(Articles) {
|
82 |
| - // Define a sample article object |
83 |
| - var sampleArticle = new Articles({ |
84 |
| - title: 'An Article about MEAN', |
85 |
| - content: 'MEAN rocks!' |
86 |
| - }); |
87 |
| - |
88 | 86 | // Set the URL parameter
|
89 |
| - $stateParams.articleId = '525a8422f6d0f87f0e407a33'; |
| 87 | + $stateParams.articleId = mockArticle._id; |
90 | 88 |
|
91 | 89 | // Set GET response
|
92 |
| - $httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(sampleArticle); |
| 90 | + $httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(mockArticle); |
93 | 91 |
|
94 | 92 | // Run controller functionality
|
95 | 93 | scope.findOne();
|
96 | 94 | $httpBackend.flush();
|
97 | 95 |
|
98 | 96 | // Test scope value
|
99 |
| - expect(scope.article).toEqualData(sampleArticle); |
| 97 | + expect(scope.article).toEqualData(mockArticle); |
100 | 98 | }));
|
101 | 99 |
|
102 |
| - it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Articles) { |
103 |
| - // Create a sample article object |
104 |
| - var sampleArticlePostData = new Articles({ |
105 |
| - title: 'An Article about MEAN', |
106 |
| - content: 'MEAN rocks!' |
107 |
| - }); |
| 100 | + describe('$scope.craete()', function() { |
| 101 | + var sampleArticlePostData; |
108 | 102 |
|
109 |
| - // Create a sample article response |
110 |
| - var sampleArticleResponse = new Articles({ |
111 |
| - _id: '525cf20451979dea2c000001', |
112 |
| - title: 'An Article about MEAN', |
113 |
| - content: 'MEAN rocks!' |
| 103 | + beforeEach(function() { |
| 104 | + // Create a sample article object |
| 105 | + sampleArticlePostData = new Articles({ |
| 106 | + title: 'An Article about MEAN', |
| 107 | + content: 'MEAN rocks!' |
| 108 | + }); |
| 109 | + |
| 110 | + // Fixture mock form input values |
| 111 | + scope.title = 'An Article about MEAN'; |
| 112 | + scope.content = 'MEAN rocks!'; |
| 113 | + |
| 114 | + spyOn($location, 'path'); |
114 | 115 | });
|
115 | 116 |
|
116 |
| - // Fixture mock form input values |
117 |
| - scope.title = 'An Article about MEAN'; |
118 |
| - scope.content = 'MEAN rocks!'; |
| 117 | + it('should send a POST request with the form input values and then locate to new object URL', inject(function(Articles) { |
| 118 | + // Set POST response |
| 119 | + $httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(mockArticle); |
119 | 120 |
|
120 |
| - // Set POST response |
121 |
| - $httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(sampleArticleResponse); |
| 121 | + // Run controller functionality |
| 122 | + scope.create(); |
| 123 | + $httpBackend.flush(); |
122 | 124 |
|
123 |
| - // Run controller functionality |
124 |
| - scope.create(); |
125 |
| - $httpBackend.flush(); |
| 125 | + // Test form inputs are reset |
| 126 | + expect(scope.title).toEqual(''); |
| 127 | + expect(scope.content).toEqual(''); |
126 | 128 |
|
127 |
| - // Test form inputs are reset |
128 |
| - expect(scope.title).toEqual(''); |
129 |
| - expect(scope.content).toEqual(''); |
| 129 | + // Test URL redirection after the article was created |
| 130 | + expect($location.path.calls.mostRecent().args[0]).toBe('articles/' + mockArticle._id); |
| 131 | + })); |
130 | 132 |
|
131 |
| - // Test URL redirection after the article was created |
132 |
| - expect($location.path()).toBe('/articles/' + sampleArticleResponse._id); |
133 |
| - })); |
| 133 | + it('should set scope.error if save error', function() { |
| 134 | + var errorMessage = 'this is an error message'; |
| 135 | + $httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(400, { |
| 136 | + message: errorMessage |
| 137 | + }); |
134 | 138 |
|
135 |
| - it('$scope.update() should update a valid article', inject(function(Articles) { |
136 |
| - // Define a sample article put data |
137 |
| - var sampleArticlePutData = new Articles({ |
138 |
| - _id: '525cf20451979dea2c000001', |
139 |
| - title: 'An Article about MEAN', |
140 |
| - content: 'MEAN Rocks!' |
| 139 | + scope.create(); |
| 140 | + $httpBackend.flush(); |
| 141 | + |
| 142 | + expect(scope.error).toBe(errorMessage); |
141 | 143 | });
|
| 144 | + }); |
142 | 145 |
|
143 |
| - // Mock article in scope |
144 |
| - scope.article = sampleArticlePutData; |
| 146 | + describe('$scope.update()', function() { |
| 147 | + beforeEach(function() { |
| 148 | + // Mock article in scope |
| 149 | + scope.article = mockArticle; |
| 150 | + }); |
145 | 151 |
|
146 |
| - // Set PUT response |
147 |
| - $httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(); |
| 152 | + it('should update a valid article', inject(function(Articles) { |
| 153 | + // Set PUT response |
| 154 | + $httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(); |
148 | 155 |
|
149 |
| - // Run controller functionality |
150 |
| - scope.update(); |
151 |
| - $httpBackend.flush(); |
| 156 | + // Run controller functionality |
| 157 | + scope.update(); |
| 158 | + $httpBackend.flush(); |
152 | 159 |
|
153 |
| - // Test URL location to new object |
154 |
| - expect($location.path()).toBe('/articles/' + sampleArticlePutData._id); |
155 |
| - })); |
| 160 | + // Test URL location to new object |
| 161 | + expect($location.path()).toBe('/articles/' + mockArticle._id); |
| 162 | + })); |
| 163 | + |
| 164 | + it('should set scope.error to error response message', inject(function(Articles) { |
| 165 | + var errorMessage = 'error'; |
| 166 | + $httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(400, { |
| 167 | + message: errorMessage |
| 168 | + }); |
| 169 | + |
| 170 | + scope.update(); |
| 171 | + $httpBackend.flush(); |
| 172 | + |
| 173 | + expect(scope.error).toBe(errorMessage); |
| 174 | + })); |
| 175 | + }); |
156 | 176 |
|
157 |
| - it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) { |
158 |
| - // Create new article object |
159 |
| - var sampleArticle = new Articles({ |
160 |
| - _id: '525a8422f6d0f87f0e407a33' |
| 177 | + describe('$scope.remove(article)', function() { |
| 178 | + beforeEach(function() { |
| 179 | + // Create new articles array and include the article |
| 180 | + scope.articles = [mockArticle, {}]; |
| 181 | + |
| 182 | + // Set expected DELETE response |
| 183 | + $httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204); |
| 184 | + |
| 185 | + // Run controller functionality |
| 186 | + scope.remove(mockArticle); |
161 | 187 | });
|
162 | 188 |
|
163 |
| - // Create new articles array and include the article |
164 |
| - scope.articles = [sampleArticle]; |
| 189 | + it('should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) { |
| 190 | + expect(scope.articles.length).toBe(1); |
| 191 | + })); |
| 192 | + }); |
165 | 193 |
|
166 |
| - // Set expected DELETE response |
167 |
| - $httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204); |
| 194 | + describe('scope.remove()', function() { |
| 195 | + beforeEach(function() { |
| 196 | + spyOn($location, 'path'); |
| 197 | + scope.article = mockArticle; |
168 | 198 |
|
169 |
| - // Run controller functionality |
170 |
| - scope.remove(sampleArticle); |
171 |
| - $httpBackend.flush(); |
| 199 | + $httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204); |
172 | 200 |
|
173 |
| - // Test array after successful delete |
174 |
| - expect(scope.articles.length).toBe(0); |
175 |
| - })); |
| 201 | + scope.remove(); |
| 202 | + $httpBackend.flush(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should redirect to articles', function() { |
| 206 | + expect($location.path).toHaveBeenCalledWith('articles'); |
| 207 | + }); |
| 208 | + }); |
176 | 209 | });
|
177 | 210 | }());
|
0 commit comments