blob: e8cc03023ec994f104d125962e524c077883fd1f [file] [log] [blame]
Matteo Scandolod5efedf2016-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 Scandolo3a55ad62016-04-26 10:10:54 -070010 const mockData = [
11 {
12 id: 1,
13 first_name: 'Jon',
14 last_name: 'Snow',
15 hidden_field: 'hidden'
16 }
17 ];
18
Matteo Scandolod5efedf2016-04-26 08:42:51 -070019 describe('The xos.helper module', function(){
20 describe('The xos-smart-table component', () => {
21
Matteo Scandolo3a55ad62016-04-26 10:10:54 -070022 var spy, emptySpy, scope, isolatedScope, element;
Matteo Scandolod5efedf2016-04-26 08:42:51 -070023
24 beforeEach(module('xos.helpers'));
25
26 // mock the service
27 beforeEach(function(){
28 module(function($provide){
29 $provide.service('MockResource', function(){
Matteo Scandolo3a55ad62016-04-26 10:10:54 -070030 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 Scandolod5efedf2016-04-26 08:42:51 -070039 });
40 });
41 })
42
43 beforeEach(inject(function ($compile, $rootScope, MockResource) {
44 scope = $rootScope.$new();
45
46 scope.config = {
Matteo Scandolo3a55ad62016-04-26 10:10:54 -070047 resource: 'MockResource',
48 hiddenFields: ['hidden_field']
Matteo Scandolod5efedf2016-04-26 08:42:51 -070049 };
50
51 spy = MockResource;
Matteo Scandolod5efedf2016-04-26 08:42:51 -070052
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 Scandolod5efedf2016-04-26 08:42:51 -070060 expect(spy.query).toHaveBeenCalled();
Matteo Scandolo3a55ad62016-04-26 10:10:54 -070061 expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide');
Matteo Scandolod5efedf2016-04-26 08:42:51 -070062 });
63
Matteo Scandolo3a55ad62016-04-26 10:10:54 -070064 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 Scandolod5efedf2016-04-26 08:42:51 -070094 });
95 });
96})();