blob: d579ce65aafca32f59b0136439afeddda79b10ff [file] [log] [blame]
Matteo Scandoloaaa733d2016-04-26 08:42:51 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 3/24/16.
5 */
6
7(function () {
8 'use strict';
9
Matteo Scandolob7dac502016-04-28 13:14:08 -070010 let mockData;
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070011
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070012 describe('The xos.helper module', function(){
13 describe('The xos-smart-table component', () => {
14
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070015 var spy, emptySpy, scope, isolatedScope, element;
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070016
17 beforeEach(module('xos.helpers'));
18
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070019 beforeEach(function() {
Matteo Scandolob7dac502016-04-28 13:14:08 -070020
21 // set mockData
22 mockData = [
23 {
24 id: 1,
25 first_name: 'Jon',
26 last_name: 'Snow',
27 hidden_field: 'hidden'
28 }
Matteo Scandoloc01f8a52016-06-13 18:12:48 -070029 ];
Matteo Scandolob7dac502016-04-28 13:14:08 -070030
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070031 jasmine.addMatchers({
32 toBeInstanceOf: function() {
33
34 return {
35 compare: (actual, expected) => {
36 var actual = actual;
37 var result = {};
38 result.pass = actual instanceof expected.constructor;
39
40 result.message = 'Expected ' + actual + ' to be instance of ' + expected;
41
42 return result;
43 },
44 negativeCompare: (actual, expected) => {
45 var actual = actual;
46 var result = {};
47 result.pass = actual instanceof expected.constructor === false;
48
49 result.message = 'Expected ' + actual + ' to be instance of ' + expected;
50
51 return result;
52 }
53 }
54 }
55 });
56 });
57
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070058 // mock the service
59 beforeEach(function(){
60 module(function($provide){
61 $provide.service('MockResource', function(){
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070062 return {
63 query: '',
64 delete: ''
65 }
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070066 });
67
68 $provide.service('EmptyResource', function(){
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070069 return {
70 query: ''
71 }
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070072 });
73 });
74 })
75
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070076 beforeEach(inject(function ($compile, $rootScope, $q, MockResource) {
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070077 scope = $rootScope.$new();
78
79 scope.config = {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070080 resource: 'MockResource',
81 hiddenFields: ['hidden_field']
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070082 };
83
84 spy = MockResource;
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070085
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070086 spyOn(MockResource, 'query').and.callFake(function() {
87 var deferred = $q.defer();
88 deferred.resolve(mockData);
89 return {$promise: deferred.promise};
90 });
91
92 spyOn(MockResource, 'delete').and.callFake(function() {
93 var deferred = $q.defer();
94 deferred.resolve();
95 return {$promise: deferred.promise};
96 });
97
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070098 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
99 $compile(element)(scope);
100 scope.$digest();
101 isolatedScope = element.isolateScope().vm;
102 }));
103
104 it('should query elements', () => {
Matteo Scandoloaaa733d2016-04-26 08:42:51 -0700105 expect(spy.query).toHaveBeenCalled();
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700106 expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide');
Matteo Scandoloaaa733d2016-04-26 08:42:51 -0700107 });
108
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700109 it('should hide hidden fields', () => {
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700110 // the 4th field is the mocked save method
111 expect($(element).find('thead th').length).toEqual(3);
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700112 expect($(element).find('thead th')[0]).toContainText('First name:');
113 expect($(element).find('thead th')[1]).toContainText('Last name:');
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700114 expect($(element).find('thead th')[2]).toContainText('Actions:');
115 });
116
117 it('should delete a model', () => {
Matteo Scandolob7dac502016-04-28 13:14:08 -0700118 // saving mockData (they are going to be deleted)
119 let mock = angular.copy(mockData);
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700120 $(element).find('a[title="delete"]')[0].click();
Matteo Scandolob7dac502016-04-28 13:14:08 -0700121 expect(spy.delete).toHaveBeenCalledWith({id: mock[0].id});
122 expect($(element).find('.alert')).toContainText(`MockResource with id ${mock[0].id} successfully deleted`);
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700123 });
124
125 it('should show the form', () => {
126 expect($(element).find('.panel')[0]).toHaveClass('ng-hide');
127 $(element).find('a[title="details"]')[0].click();
128 expect($(element).find('.panel')).not.toHaveClass('ng-hide');
129 });
130
Matteo Scandolof6445352016-04-26 12:15:23 -0700131 it('should hide the form', () => {
132 isolatedScope.detailedItem = {
133 some: 'model'
134 };
135 scope.$apply();
136 expect($(element).find('.panel')).not.toHaveClass('ng-hide');
Matteo Scandolof6445352016-04-26 12:15:23 -0700137 $(element).find('.panel .col-xs-1 a')[0].click();
138 expect($(element).find('.panel')[0]).toHaveClass('ng-hide');
139 });
140
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700141 it('should save an item', inject(($q) => {
142
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700143 let model = {
144 id: 1,
145 first_name: 'Jon',
146 last_name: 'Snow',
147 hidden_field: 'hidden',
Matteo Scandolo2e4c3442016-04-29 10:20:01 -0700148 $save: '',
149 $update: ''
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700150 };
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700151
152 spyOn(model, '$save').and.callFake(function() {
153 var deferred = $q.defer();
154 deferred.resolve();
155 return deferred.promise;
156 });
157
Matteo Scandolo2e4c3442016-04-29 10:20:01 -0700158 spyOn(model, '$update').and.callFake(function() {
159 var deferred = $q.defer();
160 deferred.resolve();
161 return deferred.promise;
162 });
163
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700164 isolatedScope.detailedItem = model;
165 scope.$apply();
166 $(element).find('xos-form .btn.btn-success').click();
Matteo Scandolo2e4c3442016-04-29 10:20:01 -0700167 expect(model.$update).toHaveBeenCalled();
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700168 }));
169
170 it('should have an add button', () => {
171 let addBtn = $(element).find('.row .btn.btn-success');
172 expect(addBtn.parent().parent()).not.toHaveClass('ng-hide');
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700173 });
174
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700175 describe('when the add button is clicked', () => {
176 beforeEach(() => {
177 let btn = $(element).find('.row .btn.btn-success')
178 btn[0].click();
179 });
180
181 xit('should create a new model', () => {
182 expect(isolatedScope.detailedItem).toBeDefined();
183 expect(isolatedScope.detailedItem).toBeInstanceOf('Resource');
184 });
185 });
Matteo Scandolof6445352016-04-26 12:15:23 -0700186
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700187 describe('when fetching an empty collection', () => {
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700188 beforeEach(inject(function ($compile, $rootScope, $q, EmptyResource) {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700189 scope = $rootScope.$new();
190
191 scope.config = {
192 resource: 'EmptyResource'
193 };
194
195 emptySpy = EmptyResource;
196
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700197 spyOn(EmptyResource, 'query').and.callFake(function() {
198 var deferred = $q.defer();
199 deferred.resolve([]);
200 return {$promise: deferred.promise};
201 });
202
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700203 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
204 $compile(element)(scope);
205 scope.$digest();
206 isolatedScope = element.isolateScope().vm;
207 }));
208
209 it('should display an alert', () => {
210 expect(emptySpy.query).toHaveBeenCalled();
211 expect($(element).find('.alert').parent().parent()).not.toHaveClass('ng-hide');
212 expect($(element).find('.alert')).toContainText('No data to show');
213 });
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700214
215 it('should not have an add button', () => {
216 let addBtn = $(element).find('.row .btn.btn-success');
217 expect(addBtn.parent().parent()).toHaveClass('ng-hide');
218 });
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700219 });
220
221
Matteo Scandoloaaa733d2016-04-26 08:42:51 -0700222 });
223 });
224})();