Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 3/24/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
| 10 | describe('The xos.helper module', function(){ |
| 11 | describe('The xos-table component', () => { |
| 12 | |
| 13 | beforeEach(module('xos.helpers')); |
| 14 | |
| 15 | it('should throw an error if no config is specified', inject(($compile, $rootScope) => { |
| 16 | function errorFunctionWrapper(){ |
| 17 | $compile(angular.element('<xos-table></xos-table>'))($rootScope); |
| 18 | $rootScope.$digest(); |
| 19 | } |
| 20 | expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a configuration via the "config" attribute')); |
| 21 | })); |
| 22 | |
| 23 | it('should throw an error if no config columns are specified', inject(($compile, $rootScope) => { |
| 24 | function errorFunctionWrapper(){ |
| 25 | // setup the parent scope |
| 26 | let scope = $rootScope.$new(); |
| 27 | scope.config = 'green'; |
| 28 | $compile(angular.element('<xos-table config="config"></xos-table>'))(scope); |
| 29 | $rootScope.$digest(); |
| 30 | } |
| 31 | expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a columns list in the configuration')); |
| 32 | })); |
| 33 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 34 | describe('when basicly configured', function() { |
| 35 | var scope, element, isolatedScope; |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 36 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 37 | beforeEach(inject(function ($compile, $rootScope) { |
| 38 | scope = $rootScope.$new(); |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 39 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 40 | scope.config = { |
| 41 | columns: [ |
| 42 | { |
| 43 | label: 'Label 1', |
| 44 | prop: 'label-1' |
| 45 | }, |
| 46 | { |
| 47 | label: 'Label 2', |
| 48 | prop: 'label-2' |
| 49 | } |
| 50 | ] |
| 51 | }; |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 52 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 53 | scope.data = [ |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 54 | { |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 55 | 'label-1': 'Sample 1.1', |
| 56 | 'label-2': 'Sample 1.2' |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 57 | }, |
| 58 | { |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 59 | 'label-1': 'Sample 2.1', |
| 60 | 'label-2': 'Sample 2.2' |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 61 | } |
| 62 | ] |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 63 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 64 | element = angular.element('<xos-table config="config" data="data"></xos-table>'); |
| 65 | $compile(element)(scope); |
| 66 | scope.$digest(); |
| 67 | isolatedScope = element.isolateScope().vm; |
| 68 | })); |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 69 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 70 | it('should contain 2 columns', function() { |
| 71 | var th = element[0].getElementsByTagName('th'); |
| 72 | expect(th.length).toEqual(2); |
| 73 | expect(isolatedScope.columns.length).toEqual(2); |
| 74 | }); |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 75 | |
Matteo Scandolo | 6a6586e | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 76 | it('should contain 3 rows', function() { |
| 77 | var tr = element[0].getElementsByTagName('tr'); |
| 78 | expect(tr.length).toEqual(3); |
| 79 | }); |
| 80 | |
| 81 | describe('when actions are passed', () => { |
| 82 | |
| 83 | let cb = jasmine.createSpy('callback') |
| 84 | |
| 85 | beforeEach(() => { |
| 86 | isolatedScope.config.actions = [ |
| 87 | { |
| 88 | label: 'delete', |
| 89 | icon: 'remove', |
| 90 | cb: cb, |
| 91 | color: 'red' |
| 92 | } |
| 93 | ]; |
| 94 | scope.$digest(); |
| 95 | }); |
| 96 | |
| 97 | it('should have 3 columns', () => { |
| 98 | var th = element[0].getElementsByTagName('th'); |
| 99 | expect(th.length).toEqual(3); |
| 100 | expect(isolatedScope.columns.length).toEqual(2); |
| 101 | }); |
| 102 | |
| 103 | it('when clicking on action should invoke callback', () => { |
| 104 | var link = element[0].getElementsByTagName('a')[0]; |
| 105 | link.click(); |
| 106 | expect(cb).toHaveBeenCalledWith(scope.data[0]); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe('when filter is fulltext', () => { |
| 111 | beforeEach(() => { |
| 112 | isolatedScope.config.filter = 'fulltext'; |
| 113 | scope.$digest(); |
| 114 | }); |
| 115 | |
| 116 | it('should render a text field', () => { |
| 117 | var textField = element[0].getElementsByTagName('input'); |
| 118 | expect(textField.length).toEqual(1); |
| 119 | }); |
| 120 | |
| 121 | describe('and a value is enterd', () => { |
| 122 | beforeEach(() => { |
| 123 | isolatedScope.query = '2.2'; |
| 124 | scope.$digest(); |
| 125 | }); |
| 126 | |
| 127 | it('should contain 2 rows', function() { |
| 128 | var tr = element[0].getElementsByTagName('tr'); |
| 129 | expect(tr.length).toEqual(2); |
| 130 | }); |
| 131 | }); |
| 132 | }); |
| 133 | |
| 134 | describe('when filter is field', () => { |
| 135 | beforeEach(() => { |
| 136 | isolatedScope.config.filter = 'field'; |
| 137 | scope.$digest(); |
| 138 | }); |
| 139 | |
| 140 | it('should render a text field for each column', () => { |
| 141 | var textField = element[0].getElementsByTagName('input'); |
| 142 | expect(textField.length).toEqual(2); |
| 143 | }); |
| 144 | |
| 145 | describe('and a value is enterd', () => { |
| 146 | beforeEach(() => { |
| 147 | isolatedScope.query = {'label-1': '2.1'}; |
| 148 | scope.$digest(); |
| 149 | }); |
| 150 | |
| 151 | it('should contain 3 rows', function() { |
| 152 | var tr = element[0].getElementsByTagName('tr'); |
| 153 | expect(tr.length).toEqual(3); |
| 154 | }); |
| 155 | }); |
| 156 | }); |
Matteo Scandolo | f32a2e9 | 2016-04-14 17:19:08 -0700 | [diff] [blame] | 157 | |
| 158 | describe('when order is true', () => { |
| 159 | beforeEach(() => { |
| 160 | isolatedScope.config.order = true; |
| 161 | scope.$digest(); |
| 162 | }); |
| 163 | |
| 164 | it('should render a arrows beside', () => { |
| 165 | var arrows = element[0].getElementsByTagName('i'); |
| 166 | expect(arrows.length).toEqual(4); |
| 167 | }); |
| 168 | |
| 169 | describe('and an order is set', () => { |
| 170 | beforeEach(() => { |
| 171 | isolatedScope.orderBy = 'label-1'; |
| 172 | isolatedScope.reverse = true; |
| 173 | scope.$digest(); |
| 174 | }); |
| 175 | |
| 176 | it('should orderBy', function() { |
| 177 | var tr = element[0].getElementsByTagName('tr'); |
| 178 | expect(angular.element(tr[1]).text()).toContain('Sample 2.2'); |
| 179 | expect(angular.element(tr[2]).text()).toContain('Sample 1.1'); |
| 180 | }); |
| 181 | }); |
| 182 | }); |
Matteo Scandolo | a7e64fd | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 183 | }); |
| 184 | }); |
| 185 | }); |
| 186 | })(); |
| 187 | |