blob: 8c0f2f949c989bc2bf93d7d7766983e7a52e6ecd [file] [log] [blame]
Matteo Scandolo5d962a32017-08-01 18:16:14 -07001import {
2 IXosCrudRelationService, XosCrudRelationService, IXosCrudRelationFormTabData,
3 IXosCrudRelationTableTabData
4} from './crud.relations.service';
5import {BehaviorSubject} from 'rxjs';
6import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
7
8const XosModelStoreMock = {
9 get: null,
10 query: null
11};
12
13const XosModelDiscovererMock = {
14 get: null
15};
16
17let service, scope;
18
19describe('The XosCrudRelation service', () => {
20 beforeEach(() => {
21 angular
22 .module('test', ['ui.router', 'toastr'])
23 .service('XosCrudRelation', XosCrudRelationService)
24 .value('XosModelStore', XosModelStoreMock)
25 .value('XosModelDiscoverer', XosModelDiscovererMock)
26 .service('ConfigHelpers', ConfigHelpers);
27
28 angular.mock.module('test');
29 });
30
31 beforeEach(angular.mock.inject((XosCrudRelation: IXosCrudRelationService, $rootScope: ng.IScope) => {
32 service = XosCrudRelation;
33 scope = $rootScope;
34 }));
35
36 it('should have the correct methods', () => {
37 expect(service.getModel).toBeDefined();
38 expect(service.getModels).toBeDefined();
39 expect(service.existsRelatedItem).toBeDefined();
40 });
41
42 describe('the existsRelatedItem method', () => {
43 it('should return true if the we have a reference to the related model', () => {
44 const relation = {
45 model: 'Test',
46 type: 'manytoone',
47 on_field: 'test'
48 };
49 const item = {test_id: 5};
50
51 const res = service.existsRelatedItem(relation, item);
52 expect(res).toBeTruthy();
53 });
54 it('should return false if the we don\'t have a reference to the related model', () => {
55 const relation = {
56 model: 'Test',
57 type: 'manytoone',
58 on_field: 'test'
59 };
60 const item = {foo: 5};
61
62 const res = service.existsRelatedItem(relation, item);
63 expect(res).toBeFalsy();
64 });
65 });
66
67 describe('the getHumanReadableOnField method', () => {
68 it('should return a human readable version of the on_field param', () => {
69 const relation = {
70 model: 'Test',
71 type: 'onetomany',
72 on_field: 'relate_to_test'
73 };
74
75 const res = service.getHumanReadableOnField(relation, 'Instance');
76 expect(res).toEqual('[Relate to test]');
77 });
78
79 it('should return am empty string if the on_field param equal the model param', () => {
80 const relation = {
81 model: 'Test',
82 type: 'onetomany',
83 on_field: 'test'
84 };
85
86 const res = service.getHumanReadableOnField(relation, 'Instance');
87 expect(res).toEqual('');
88 });
89
90 it('should return am empty string if the type on_field equal the base model', () => {
91 const relation = {
92 model: 'Test',
93 type: 'manytoone',
94 on_field: 'instance'
95 };
96
97 const res = service.getHumanReadableOnField(relation, 'Instance');
98 expect(res).toEqual('');
99 });
100 });
101
102 describe('the getModel method', () => {
103 it('should return the tab config for a single object', (done) => {
104 const relation = {
105 model: 'Test',
106 type: 'manytoone',
107 on_field: 'test'
108 };
109
110 const resModel = {foo: 'bar'};
111 const resFormCfg = {form: 'config'};
112
113 spyOn(XosModelStoreMock, 'get').and.callFake(() => {
114 const subject = new BehaviorSubject({});
115 subject.next(resModel);
116 return subject.asObservable();
117 });
118 spyOn(XosModelDiscovererMock, 'get').and.returnValue({formCfg: resFormCfg});
119
120 service.getModel(relation, '5')
121 .then((res: IXosCrudRelationFormTabData) => {
122 expect(res.model).toEqual(resModel);
123 expect(res.class).toEqual('full');
124 expect(res.formConfig).toEqual(resFormCfg);
125 done();
126 });
127 scope.$apply();
128 });
129 });
130
131 describe('the getModels method', () => {
132 it('should return one related model', (done) => {
133 const relation = {
134 model: 'Test',
135 type: 'onetomany',
136 on_field: 'test'
137 };
138
139 const resModels = [
140 {test_id: 5},
141 {test_id: 25}
142 ];
143 const resTableCfg = {table: 'config'};
144
145 spyOn(XosModelStoreMock, 'query').and.callFake(() => {
146 const subject = new BehaviorSubject(resModels);
147 return subject.asObservable();
148 });
149 spyOn(XosModelDiscovererMock, 'get').and.returnValue({tableCfg: resTableCfg});
150
151 service.getModels(relation, 5)
152 .then((res: IXosCrudRelationTableTabData) => {
153 expect(res.model.length).toEqual(1);
154 expect(res.class).toEqual('full');
155 expect(res.tableConfig).toEqual({
156 table: 'config',
157 filter: null
158 });
159 done();
160 });
161 scope.$apply();
162 });
163
164 it('should not return related models', (done) => {
165 const relation = {
166 model: 'Test',
167 type: 'onetomany',
168 on_field: 'test'
169 };
170
171 const resModels = [
172 {test_id: 15},
173 {test_id: 25}
174 ];
175 const resTableCfg = {table: 'config'};
176
177 spyOn(XosModelStoreMock, 'query').and.callFake(() => {
178 const subject = new BehaviorSubject(resModels);
179 return subject.asObservable();
180 });
181 spyOn(XosModelDiscovererMock, 'get').and.returnValue({tableCfg: resTableCfg});
182
183 service.getModels(relation, 5)
184 .then((res: IXosCrudRelationTableTabData) => {
185 expect(res.model.length).toEqual(0);
186 expect(res.class).toEqual('empty');
187 expect(res.tableConfig).toEqual({
188 table: 'config',
189 filter: null
190 });
191 done();
192 });
193 scope.$apply();
194 });
195 });
196});