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