blob: a5d7cfecefca867ecd824e55b0625d1bf4f72cf3 [file] [log] [blame]
Matteo Scandoloef969922016-08-11 13:49:12 -07001'use strict';
2
3describe('The ExploreCmd service', () => {
4
5 var ExploreCmd, ErrorHandler, ResponseHandler, done, shell, ResourceMock, rootScope;
6
7 beforeEach(module('xos.UITutorial'));
8 beforeEach(module('templates'));
9
10 beforeEach(() => {
11 module(function ($provide) {
12 $provide.value('Resource', ResourceMock);
13 })
14 });
15
16 beforeEach(inject(function (_ExploreCmd_, _ErrorHandler_, _ResponseHandler_, $rootScope) {
17 // The injector unwraps the underscores (_) from around the parameter names when matching
18 ExploreCmd = _ExploreCmd_;
19 ErrorHandler = _ErrorHandler_;
20 ResponseHandler = _ResponseHandler_;
21 rootScope = $rootScope;
22 done = jasmine.createSpy('done');
23 shell = {
24 setCommandHandler: jasmine.createSpy('setCommandHandler'),
25 bestMatch: jasmine.createSpy('bestMatch')
26 };
27 // binding the mock shell to the service (for easy testing)
28 ExploreCmd.shell = shell;
29 spyOn(console, 'error');
30
31 ResourceMock = {
32 query: jasmine.createSpy('query').and.callFake(() => {
33 var deferred = $q.defer();
34 deferred.resolve('Remote call result');
35 return {$promise: deferred.promise};
36 })
37 };
38 }));
39
40 it('should set the resouce command handler', () => {
41 ExploreCmd.setup(shell);
42 expect(shell.setCommandHandler).toHaveBeenCalledWith('resource', { exec: ExploreCmd.resourceExec, completion: ExploreCmd.resourceCompletion })
43 });
44
45 describe('the resourceCompletion function', () => {
46
47 beforeEach(() => {
48 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Sites', 'Slices']);
49 });
50
51 it('should suggest a resource list', () => {
52 ExploreCmd.resourceCompletion('resource', '', {text: 'resource'}, done)
53 expect(shell.bestMatch).toHaveBeenCalledWith('', [ 'list', 'Sites', 'Slices' ]);
54
55 ExploreCmd.resourceCompletion('resource', 'S', {text: 'resource S'}, done)
56 expect(shell.bestMatch).toHaveBeenCalledWith('S', [ 'list', 'Sites', 'Slices' ]);
57 });
58
59 it('should suggest a method list', () => {
60 ExploreCmd.resourceCompletion('resource', '', {text: 'resource Sites '}, done)
61 expect(shell.bestMatch).toHaveBeenCalledWith('', [ 'query', 'get', 'save', '$save', 'delete' ]);
62
63 ExploreCmd.resourceCompletion('resource', 'q', {text: 'resource Sites q'}, done)
64 expect(shell.bestMatch).toHaveBeenCalledWith('q', [ 'query', 'get', 'save', '$save', 'delete' ]);
65 });
66 });
67
68 describe('the resourceExec function', () => {
69
70 beforeEach(() => {
71 spyOn(ExploreCmd, 'listAvailableResources');
72 spyOn(ExploreCmd, 'consumeResource');
73 });
74
75 it('should list available resources', () => {
76 ExploreCmd.resourceExec('explore', ['list'], done);
77 expect(ExploreCmd.listAvailableResources).toHaveBeenCalledWith(done);
78 });
79
80 it('should use a resource', () => {
81 ExploreCmd.resourceExec('explore', ['Resource', 'query'], done);
82 expect(ExploreCmd.consumeResource).toHaveBeenCalledWith('Resource', 'query', [], done);
83 });
84 });
85
86 describe('the getAvailableResources function', () => {
87
88 beforeEach(() => {
89 spyOn(angular, 'module').and.returnValue({
90 _invokeQueue: [
91 ['$provide', 'service', ['Sites', ['$resource']]],
92 ['$provide', 'service', ['Slices', ['$q', '$resource']]],
93 ['$provide', 'factory', ['_', []]],
94 ['$provide', 'service', ['helper', ['Slices']]]
95 ]
96 });
97 });
98
99 it('should return a list of resources in the angular app', () => {
100 const resources = ExploreCmd.getAvailableResources();
101 expect(resources).toEqual(['Sites', 'Slices']);
102 });
103 });
104
105 describe('the listAvailableResources function', () => {
106 beforeEach(() => {
107 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Sites', 'Slices']);
108 });
109
110 it('should format resource in an html template', () => {
111 ExploreCmd.listAvailableResources(done);
112 expect(ExploreCmd.getAvailableResources).toHaveBeenCalled();
113 expect(done).toHaveBeenCalledWith(`Sites<br/>Slices<br/>`);
114 });
115 });
116
117 describe('the consumeResource function', () => {
118 beforeEach(() => {
119 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Resource', 'Fake']);
120 spyOn(ErrorHandler, 'print');
121 });
122
123 it('should notify that a resource does not exists', () => {
124 ExploreCmd.consumeResource('Test', null, null, done);
125 expect(ErrorHandler.print).toHaveBeenCalledWith(`Resource "Test" does not exists`, done)
126 });
127
128 it('should notify that a method does not exists', () => {
129 ExploreCmd.consumeResource('Resource', 'test', null, done);
130 expect(ErrorHandler.print).toHaveBeenCalledWith(`Method "test" not allowed`, done)
131 });
132
133 const methodsWithParams = ['get', '$save', 'delete'];
134 methodsWithParams.forEach(method => {
135 it(`should notify that the ${method} method require parameters`, () => {
136 ExploreCmd.consumeResource('Resource', method, [], done);
137 expect(ErrorHandler.print).toHaveBeenCalledWith(`Method "${method}" require parameters`, done)
138 });
139 });
140
141 it('should not accept id as parameter for the query method', () => {
142 ExploreCmd.consumeResource('Resource', 'query', ['{id:1}'], done);
143 expect(ErrorHandler.print).toHaveBeenCalledWith(`Is not possible to use "id" as filter in method "query", use "get" instead!`, done)
144 });
145
146 it('should notify the user in case of malformed parameters', () => {
147 ExploreCmd.consumeResource('Resource', 'query', ['{child: 3}'], done);
148 expect(ErrorHandler.print).toHaveBeenCalledWith(`Parameter is not valid, it shoudl be in the form of: <code>{id:1}</code>, with no spaces`, done)
149 pending();
150 });
151
152 describe('when called with the correct parameters', () => {
153
154 let deferred;
155 beforeEach(inject(($q) => {
156 spyOn(ResponseHandler, 'parse');
157
158 deferred = $q.defer();
159 ResourceMock = {
160 query: jasmine.createSpy('query').and.callFake(function(){
161 return {$promise: deferred.promise};
162 })
163 };
164 }));
165
166 it('should notify the user if an error occurred while loading the resource', () => {
167 ExploreCmd.consumeResource('Fake', 'query', [], done);
168 expect(console.error).toHaveBeenCalled();
169 expect(ErrorHandler.print).toHaveBeenCalledWith('Failed to inject resource "Fake"', done);
170 });
171
172 it('should call a resource and return the results', () => {
173 ExploreCmd.consumeResource('Resource', 'query', [], done);
174 deferred.resolve([]);
175 rootScope.$apply();
176 expect(ErrorHandler.print).not.toHaveBeenCalled()
177 expect(ResponseHandler.parse).toHaveBeenCalledWith([], 'Resource.query()', done);
178 });
179
180 it('should call a resource with parameters and return the results', () => {
181 ExploreCmd.consumeResource('Resource', 'query', ['{child:3}'], done);
182 deferred.resolve([]);
183 rootScope.$apply();
184 expect(ErrorHandler.print).not.toHaveBeenCalled()
185 expect(ResponseHandler.parse).toHaveBeenCalledWith([], 'Resource.query({"child":3})', done);
186 });
187
188 it('should call a resource and display a not found message', () => {
189 ExploreCmd.consumeResource('Resource', 'query', [], done);
190 deferred.reject({status: 404, data: {detail: 'Not Found.'}});
191 rootScope.$apply();
192 expect(ErrorHandler.print).toHaveBeenCalledWith('Resource with method "query" and parameters {} Not Found.', done)
193 expect(ResponseHandler.parse).not.toHaveBeenCalled();
194 });
195 });
196 });
197});