da834ba69f4319d577c33c8c45728ac8520ba6bb
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / modals / report-modal / report-modal.controller.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 require('./report-modal.controller');
22 const jestMock = require('jest-mock');
23
24 describe('Testing error report creation', () => {
25     let $notNeeded;
26     let $controller;
27
28     let mockHttp;
29
30     let mockModalInstance;
31     let mockWindow;
32     let mockReportService;
33     let testErrorMsg;
34
35     let correctResponse;
36     let failResponse;
37
38     beforeEach(
39         angular.mock.module('app')
40     );
41
42     beforeEach(inject(function (_$controller_) {
43         $notNeeded = jestMock.fn();
44         mockHttp = jestMock.fn();
45
46         mockModalInstance = {};
47         mockWindow = {
48             webkitURL: {
49                     createObjectURL: function (blob) {
50                         return blob;
51                 }
52             }
53         };
54
55         correctResponse = {data:{report:"test-error-report",status:202}};
56         failResponse = {data:{report:"test-fail-report",status:404}};
57
58         mockReportService = {
59
60             getReportData: function() {
61                 return Promise.resolve(correctResponse);
62             },
63             getReportTimeStamp: function () {
64                 return "testTime";
65             }
66         };
67
68         testErrorMsg = 'testing message';
69
70         $controller = _$controller_('reportModalController',{
71             $uibModalInstance: mockModalInstance,
72             $scope: $notNeeded,
73             $window: mockWindow,
74             ReportService: mockReportService,
75             errorMsg: testErrorMsg
76         });
77     }));
78
79     test('Verify close will call close in modal instance', () => {
80         mockModalInstance.close = jestMock.fn();
81
82         $controller.close();
83
84         expect(mockModalInstance.close).toHaveBeenCalled();
85     });
86
87     test('Verify report was constructed properly', () => {
88
89         $controller.saveReportData(correctResponse);
90
91         expect($controller.report).toEqual(testErrorMsg + "\n\n Collected data from API:\n" + JSON.stringify(correctResponse.data,  null, "\t"));
92         expect($controller.downloadEnable).toBeTruthy();
93         expect($controller.download).toEqual(new Blob([ $controller.report ], { type : 'text/plain' }));
94     });
95
96     test('Verify report contains error if API did not respond', () => {
97
98         $controller.printReportFail(failResponse);
99
100         expect($controller.report).toEqual(testErrorMsg + "\n\n API error:\n" + JSON.stringify(failResponse.data,  null, "\t"));
101         expect($controller.downloadEnable).toBeFalsy();
102     });
103
104 });