Matteo Scandolo | aaa733d | 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 | ba1d35c | 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 | aaa733d | 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 | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 22 | var spy, emptySpy, scope, isolatedScope, element; |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 23 | |
| 24 | beforeEach(module('xos.helpers')); |
| 25 | |
| 26 | // mock the service |
| 27 | beforeEach(function(){ |
| 28 | module(function($provide){ |
| 29 | $provide.service('MockResource', function(){ |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 30 | this.query = jasmine.createSpy('query').and.callFake(() => { |
| 31 | return {$promise: {then: (cb) => cb(mockData)}}; |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | $provide.service('EmptyResource', function(){ |
| 36 | this.query = jasmine.createSpy('emptyQuery').and.callFake(() => { |
| 37 | return {$promise: {then: (cb) => cb([])}}; |
| 38 | }); |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 39 | }); |
| 40 | }); |
| 41 | }) |
| 42 | |
| 43 | beforeEach(inject(function ($compile, $rootScope, MockResource) { |
| 44 | scope = $rootScope.$new(); |
| 45 | |
| 46 | scope.config = { |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 47 | resource: 'MockResource', |
| 48 | hiddenFields: ['hidden_field'] |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | spy = MockResource; |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 52 | |
| 53 | element = angular.element('<xos-smart-table config="config"></xos-smart-table>'); |
| 54 | $compile(element)(scope); |
| 55 | scope.$digest(); |
| 56 | isolatedScope = element.isolateScope().vm; |
| 57 | })); |
| 58 | |
| 59 | it('should query elements', () => { |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 60 | expect(spy.query).toHaveBeenCalled(); |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 61 | expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide'); |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 62 | }); |
| 63 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 64 | it('should hide hidden fields', () => { |
| 65 | expect($(element).find('thead th').length).toEqual(2); |
| 66 | expect($(element).find('thead th')[0]).toContainText('First name:'); |
| 67 | expect($(element).find('thead th')[1]).toContainText('Last name:'); |
| 68 | }); |
| 69 | |
| 70 | describe('when fetching an empty collection', () => { |
| 71 | beforeEach(inject(function ($compile, $rootScope, EmptyResource) { |
| 72 | scope = $rootScope.$new(); |
| 73 | |
| 74 | scope.config = { |
| 75 | resource: 'EmptyResource' |
| 76 | }; |
| 77 | |
| 78 | emptySpy = EmptyResource; |
| 79 | |
| 80 | element = angular.element('<xos-smart-table config="config"></xos-smart-table>'); |
| 81 | $compile(element)(scope); |
| 82 | scope.$digest(); |
| 83 | isolatedScope = element.isolateScope().vm; |
| 84 | })); |
| 85 | |
| 86 | it('should display an alert', () => { |
| 87 | expect(emptySpy.query).toHaveBeenCalled(); |
| 88 | expect($(element).find('.alert').parent().parent()).not.toHaveClass('ng-hide'); |
| 89 | expect($(element).find('.alert')).toContainText('No data to show'); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | |
Matteo Scandolo | aaa733d | 2016-04-26 08:42:51 -0700 | [diff] [blame] | 94 | }); |
| 95 | }); |
| 96 | })(); |