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