blob: 941cebc927902eb20056e17705cf655363f04b67 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/**
20 * © OpenCORD
21 *
22 * Created by teone on 3/24/16.
23 */
24
25(function () {
26 'use strict';
27
28 let mockData;
29
30 describe('The xos.helper module', function(){
31 describe('The xos-smart-table component', () => {
32
33 let spy, emptySpy, scope, isolatedScope, element;
34
35 beforeEach(module('xos.helpers'));
36
37 beforeEach(function() {
38
39 // set mockData
40 mockData = [
41 {
42 id: 1,
43 first_name: 'Jon',
44 last_name: 'Snow',
45 hidden_field: 'hidden'
46 }
47 ];
48 });
49
50 // mock the service
51 beforeEach(function(){
52 module(function($provide){
53 $provide.service('MockResource', function(){
54 return {
55 query: '',
56 delete: ''
57 }
58 });
59
60 $provide.service('EmptyResource', function(){
61 return {
62 query: ''
63 }
64 });
65 });
66 })
67
68 beforeEach(inject(function ($compile, $rootScope, $q, MockResource) {
69 scope = $rootScope.$new();
70
71 scope.config = {
72 resource: 'MockResource',
73 hiddenFields: ['hidden_field']
74 };
75
76 spy = MockResource;
77
78 spyOn(MockResource, 'query').and.callFake(function() {
79 const deferred = $q.defer();
80 deferred.resolve(mockData);
81 return {$promise: deferred.promise};
82 });
83
84 spyOn(MockResource, 'delete').and.callFake(function() {
85 const deferred = $q.defer();
86 deferred.resolve();
87 return {$promise: deferred.promise};
88 });
89
90 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
91 $compile(element)(scope);
92 scope.$digest();
93 isolatedScope = element.isolateScope().vm;
94 }));
95
96 it('should query elements', () => {
97 expect(spy.query).toHaveBeenCalled();
98 expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide');
99 });
100
101 it('should hide hidden fields', () => {
102 // the 4th field is the mocked save method
103 expect($(element).find('thead th').length).toEqual(3);
104 expect($(element).find('thead th')[0]).toContainText('First name:');
105 expect($(element).find('thead th')[1]).toContainText('Last name:');
106 expect($(element).find('thead th')[2]).toContainText('Actions:');
107 });
108
109 it('should delete a model', () => {
110 // saving mockData (they are going to be deleted)
111 let mock = angular.copy(mockData);
112 $(element).find('a[title="delete"]')[0].click();
113 expect(spy.delete).toHaveBeenCalledWith({id: mock[0].id});
114 expect($(element).find('.alert')).toContainText(`MockResource with id ${mock[0].id} successfully deleted`);
115 });
116
117 it('should show the form', () => {
118 expect($(element).find('.panel')[0]).toHaveClass('ng-hide');
119 $(element).find('a[title="details"]')[0].click();
120 expect($(element).find('.panel')).not.toHaveClass('ng-hide');
121 });
122
123 it('should hide the form', () => {
124 isolatedScope.detailedItem = {
125 some: 'model'
126 };
127 scope.$apply();
128 expect($(element).find('.panel')).not.toHaveClass('ng-hide');
129 $(element).find('.panel .col-xs-1 a')[0].click();
130 expect($(element).find('.panel')[0]).toHaveClass('ng-hide');
131 });
132
133 it('should save an item', inject(($q) => {
134
135 let model = {
136 id: 1,
137 first_name: 'Jon',
138 last_name: 'Snow',
139 hidden_field: 'hidden',
140 $save: '',
141 $update: ''
142 };
143
144 spyOn(model, '$save').and.callFake(function() {
145 const deferred = $q.defer();
146 deferred.resolve();
147 return deferred.promise;
148 });
149
150 spyOn(model, '$update').and.callFake(function() {
151 const deferred = $q.defer();
152 deferred.resolve();
153 return deferred.promise;
154 });
155
156 isolatedScope.detailedItem = model;
157 scope.$apply();
158 $(element).find('xos-form .btn.btn-success').click();
159 expect(model.$update).toHaveBeenCalled();
160 }));
161
162 it('should have an add button', () => {
163 let addBtn = $(element).find('.row .btn.btn-success');
164 expect(addBtn.parent().parent()).not.toHaveClass('ng-hide');
165 });
166
167 describe('when the add button is clicked', () => {
168 beforeEach(() => {
169 let btn = $(element).find('.row .btn.btn-success')
170 btn[0].click();
171 });
172
173 xit('should create a new model', () => {
174 expect(isolatedScope.detailedItem).toBeDefined();
175 expect(isolatedScope.detailedItem).toBeInstanceOf('Resource');
176 });
177 });
178
179 describe('when fetching an empty collection', () => {
180 beforeEach(inject(function ($compile, $rootScope, $q, EmptyResource) {
181 scope = $rootScope.$new();
182
183 scope.config = {
184 resource: 'EmptyResource'
185 };
186
187 emptySpy = EmptyResource;
188
189 spyOn(EmptyResource, 'query').and.callFake(function() {
190 const deferred = $q.defer();
191 deferred.resolve([]);
192 return {$promise: deferred.promise};
193 });
194
195 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
196 $compile(element)(scope);
197 scope.$digest();
198 isolatedScope = element.isolateScope().vm;
199 }));
200
201 it('should display an alert', () => {
202 expect(emptySpy.query).toHaveBeenCalled();
203 expect($(element).find('.alert').parent().parent()).not.toHaveClass('ng-hide');
204 expect($(element).find('.alert')).toContainText('No data to show');
205 });
206
207 it('should not have an add button', () => {
208 let addBtn = $(element).find('.row .btn.btn-success');
209 expect(addBtn.parent().parent()).toHaveClass('ng-hide');
210 });
211 });
212
213
214 });
215 });
216})();