blob: bcc34e1fad5825965fe06ac769f2ec85cecb31de [file] [log] [blame]
Matteo Scandoloaaa733d2016-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 Scandoloba1d35c2016-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 Scandoloaaa733d2016-04-26 08:42:51 -070019 describe('The xos.helper module', function(){
20 describe('The xos-smart-table component', () => {
21
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070022 var spy, emptySpy, scope, isolatedScope, element;
Matteo Scandoloaaa733d2016-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 Scandoloba1d35c2016-04-26 10:10:54 -070030 this.query = jasmine.createSpy('query').and.callFake(() => {
31 return {$promise: {then: (cb) => cb(mockData)}};
32 });
Matteo Scandolodc249eb2016-04-26 11:44:36 -070033 this.delete = jasmine.createSpy('delete').and.callFake(() => {
34 return {$promise: {then: (cb) => cb({})}};
35 });
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070036 });
37
38 $provide.service('EmptyResource', function(){
39 this.query = jasmine.createSpy('emptyQuery').and.callFake(() => {
40 return {$promise: {then: (cb) => cb([])}};
41 });
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070042 });
43 });
44 })
45
46 beforeEach(inject(function ($compile, $rootScope, MockResource) {
47 scope = $rootScope.$new();
48
49 scope.config = {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070050 resource: 'MockResource',
51 hiddenFields: ['hidden_field']
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070052 };
53
54 spy = MockResource;
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070055
56 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
57 $compile(element)(scope);
58 scope.$digest();
59 isolatedScope = element.isolateScope().vm;
60 }));
61
62 it('should query elements', () => {
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070063 expect(spy.query).toHaveBeenCalled();
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070064 expect($(element).find('.alert').parent().parent()).toHaveClass('ng-hide');
Matteo Scandoloaaa733d2016-04-26 08:42:51 -070065 });
66
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070067 it('should hide hidden fields', () => {
Matteo Scandolodc249eb2016-04-26 11:44:36 -070068 // the 4th field is the mocked save method
69 expect($(element).find('thead th').length).toEqual(3);
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070070 expect($(element).find('thead th')[0]).toContainText('First name:');
71 expect($(element).find('thead th')[1]).toContainText('Last name:');
Matteo Scandolodc249eb2016-04-26 11:44:36 -070072 expect($(element).find('thead th')[2]).toContainText('Actions:');
73 });
74
75 it('should delete a model', () => {
76 $(element).find('a[title="delete"]')[0].click();
77 expect(spy.delete).toHaveBeenCalledWith({id: mockData[0].id});
78 expect($(element).find('.alert')).toContainText(`MockResource with id ${mockData[0].id} successfully deleted`);
79 });
80
81 it('should show the form', () => {
82 expect($(element).find('.panel')[0]).toHaveClass('ng-hide');
83 $(element).find('a[title="details"]')[0].click();
84 expect($(element).find('.panel')).not.toHaveClass('ng-hide');
85 });
86
87 it('should save an item', () => {
88 const saveMethod = jasmine.createSpy('$save').and.callFake(() => {
89 return {then: (cb) => cb(mockData[0])};
90 });
91 let model = {
92 id: 1,
93 first_name: 'Jon',
94 last_name: 'Snow',
95 hidden_field: 'hidden',
96 $save: saveMethod
97 };
98 isolatedScope.detailedItem = model;
99 scope.$apply();
100 $(element).find('xos-form .btn.btn-success').click();
101 expect(saveMethod).toHaveBeenCalled();
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700102 });
103
104 describe('when fetching an empty collection', () => {
105 beforeEach(inject(function ($compile, $rootScope, EmptyResource) {
106 scope = $rootScope.$new();
107
108 scope.config = {
109 resource: 'EmptyResource'
110 };
111
112 emptySpy = EmptyResource;
113
114 element = angular.element('<xos-smart-table config="config"></xos-smart-table>');
115 $compile(element)(scope);
116 scope.$digest();
117 isolatedScope = element.isolateScope().vm;
118 }));
119
120 it('should display an alert', () => {
121 expect(emptySpy.query).toHaveBeenCalled();
122 expect($(element).find('.alert').parent().parent()).not.toHaveClass('ng-hide');
123 expect($(element).find('.alert')).toContainText('No data to show');
124 });
125 });
126
127
Matteo Scandoloaaa733d2016-04-26 08:42:51 -0700128 });
129 });
130})();