Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 3/24/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
Matteo Scandolo | ed92a29 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 10 | let mockData; |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 11 | |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 12 | describe('The xos.helper module', function(){ |
| 13 | describe('The xos-smart-table component', () => { |
| 14 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 15 | var spy, emptySpy, scope, isolatedScope, element; |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 16 | |
| 17 | beforeEach(module('xos.helpers')); |
| 18 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 19 | beforeEach(function() { |
Matteo Scandolo | ed92a29 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 20 | |
| 21 | // set mockData |
| 22 | mockData = [ |
| 23 | { |
| 24 | id: 1, |
| 25 | first_name: 'Jon', |
| 26 | last_name: 'Snow', |
| 27 | hidden_field: 'hidden' |
| 28 | } |
Matteo Scandolo | 445cef2 | 2016-06-13 18:12:48 -0700 | [diff] [blame] | 29 | ]; |
Matteo Scandolo | ed92a29 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 30 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 31 | 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 Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 58 | // mock the service |
| 59 | beforeEach(function(){ |
| 60 | module(function($provide){ |
| 61 | $provide.service('MockResource', function(){ |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 62 | return { |
| 63 | query: '', |
| 64 | delete: '' |
| 65 | } |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 66 | }); |
| 67 | |
| 68 | $provide.service('EmptyResource', function(){ |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 69 | return { |
| 70 | query: '' |
| 71 | } |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 72 | }); |
| 73 | }); |
| 74 | }) |
| 75 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 76 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 77 | scope = $rootScope.$new(); |
| 78 | |
| 79 | scope.config = { |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 80 | resource: 'MockResource', |
| 81 | hiddenFields: ['hidden_field'] |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | spy = MockResource; |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 85 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 86 | 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 Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 98 | 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 Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 105 | expect(spy.query).toHaveBeenCalled(); |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 106 | expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide'); |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 107 | }); |
| 108 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 109 | it('should hide hidden fields', () => { |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 110 | // the 4th field is the mocked save method |
| 111 | expect($(element).find('thead th').length).toEqual(3); |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 112 | expect($(element).find('thead th')[0]).toContainText('First name:'); |
| 113 | expect($(element).find('thead th')[1]).toContainText('Last name:'); |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 114 | expect($(element).find('thead th')[2]).toContainText('Actions:'); |
| 115 | }); |
| 116 | |
| 117 | it('should delete a model', () => { |
Matteo Scandolo | ed92a29 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 118 | // saving mockData (they are going to be deleted) |
| 119 | let mock = angular.copy(mockData); |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 120 | $(element).find('a[title="delete"]')[0].click(); |
Matteo Scandolo | ed92a29 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 121 | expect(spy.delete).toHaveBeenCalledWith({id: mock[0].id}); |
| 122 | expect($(element).find('.alert')).toContainText(`MockResource with id ${mock[0].id} successfully deleted`); |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 123 | }); |
| 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 Scandolo | 2db2335 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 131 | 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 Scandolo | 2db2335 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 137 | $(element).find('.panel .col-xs-1 a')[0].click(); |
| 138 | expect($(element).find('.panel')[0]).toHaveClass('ng-hide'); |
| 139 | }); |
| 140 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 141 | it('should save an item', inject(($q) => { |
| 142 | |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 143 | let model = { |
| 144 | id: 1, |
| 145 | first_name: 'Jon', |
| 146 | last_name: 'Snow', |
| 147 | hidden_field: 'hidden', |
Matteo Scandolo | 6ca1911 | 2016-04-29 10:20:01 -0700 | [diff] [blame] | 148 | $save: '', |
| 149 | $update: '' |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 150 | }; |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 151 | |
| 152 | spyOn(model, '$save').and.callFake(function() { |
| 153 | var deferred = $q.defer(); |
| 154 | deferred.resolve(); |
| 155 | return deferred.promise; |
| 156 | }); |
| 157 | |
Matteo Scandolo | 6ca1911 | 2016-04-29 10:20:01 -0700 | [diff] [blame] | 158 | spyOn(model, '$update').and.callFake(function() { |
| 159 | var deferred = $q.defer(); |
| 160 | deferred.resolve(); |
| 161 | return deferred.promise; |
| 162 | }); |
| 163 | |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 164 | isolatedScope.detailedItem = model; |
| 165 | scope.$apply(); |
| 166 | $(element).find('xos-form .btn.btn-success').click(); |
Matteo Scandolo | 6ca1911 | 2016-04-29 10:20:01 -0700 | [diff] [blame] | 167 | expect(model.$update).toHaveBeenCalled(); |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 168 | })); |
| 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 Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 173 | }); |
| 174 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 175 | 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 Scandolo | 2db2335 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 186 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 187 | describe('when fetching an empty collection', () => { |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 188 | beforeEach(inject(function ($compile, $rootScope, $q, EmptyResource) { |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 189 | scope = $rootScope.$new(); |
| 190 | |
| 191 | scope.config = { |
| 192 | resource: 'EmptyResource' |
| 193 | }; |
| 194 | |
| 195 | emptySpy = EmptyResource; |
| 196 | |
Matteo Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 197 | spyOn(EmptyResource, 'query').and.callFake(function() { |
| 198 | var deferred = $q.defer(); |
| 199 | deferred.resolve([]); |
| 200 | return {$promise: deferred.promise}; |
| 201 | }); |
| 202 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 203 | 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 Scandolo | 0e053c2 | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 214 | |
| 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 Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 219 | }); |
| 220 | |
| 221 | |
Matteo Scandolo | d5efedf | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 222 | }); |
| 223 | }); |
| 224 | })(); |