blob: 768c0395ed70f1b875a479496ab569e2ea185c3a [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo5d962a32017-08-01 18:16:14 -070019import {
20 IXosCrudRelationService, XosCrudRelationService, IXosCrudRelationFormTabData,
21 IXosCrudRelationTableTabData
22} from './crud.relations.service';
23import {BehaviorSubject} from 'rxjs';
24import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0e171f32017-09-26 17:21:41 -070025import {XosFormHelpers} from '../../core/form/form-helpers';
Matteo Scandolo5d962a32017-08-01 18:16:14 -070026
27const XosModelStoreMock = {
28 get: null,
29 query: null
30};
31
Matteo Scandolo0e171f32017-09-26 17:21:41 -070032const XosModeldefsCacheMock = {
Matteo Scandolo5d962a32017-08-01 18:16:14 -070033 get: null
34};
35
36let service, scope;
37
38describe('The XosCrudRelation service', () => {
39 beforeEach(() => {
40 angular
41 .module('test', ['ui.router', 'toastr'])
42 .service('XosCrudRelation', XosCrudRelationService)
43 .value('XosModelStore', XosModelStoreMock)
Matteo Scandolo0e171f32017-09-26 17:21:41 -070044 .value('XosModeldefsCache', XosModeldefsCacheMock)
45 .service('ConfigHelpers', ConfigHelpers)
46 .service('XosFormHelpers', XosFormHelpers);
Matteo Scandolo5d962a32017-08-01 18:16:14 -070047
48 angular.mock.module('test');
49 });
50
51 beforeEach(angular.mock.inject((XosCrudRelation: IXosCrudRelationService, $rootScope: ng.IScope) => {
52 service = XosCrudRelation;
53 scope = $rootScope;
54 }));
55
56 it('should have the correct methods', () => {
57 expect(service.getModel).toBeDefined();
58 expect(service.getModels).toBeDefined();
59 expect(service.existsRelatedItem).toBeDefined();
60 });
61
62 describe('the existsRelatedItem method', () => {
63 it('should return true if the we have a reference to the related model', () => {
64 const relation = {
65 model: 'Test',
66 type: 'manytoone',
67 on_field: 'test'
68 };
69 const item = {test_id: 5};
70
71 const res = service.existsRelatedItem(relation, item);
72 expect(res).toBeTruthy();
73 });
74 it('should return false if the we don\'t have a reference to the related model', () => {
75 const relation = {
76 model: 'Test',
77 type: 'manytoone',
78 on_field: 'test'
79 };
80 const item = {foo: 5};
81
82 const res = service.existsRelatedItem(relation, item);
83 expect(res).toBeFalsy();
84 });
85 });
86
87 describe('the getHumanReadableOnField method', () => {
88 it('should return a human readable version of the on_field param', () => {
89 const relation = {
90 model: 'Test',
91 type: 'onetomany',
92 on_field: 'relate_to_test'
93 };
94
95 const res = service.getHumanReadableOnField(relation, 'Instance');
96 expect(res).toEqual('[Relate to test]');
97 });
98
99 it('should return am empty string if the on_field param equal the model param', () => {
100 const relation = {
101 model: 'Test',
102 type: 'onetomany',
103 on_field: 'test'
104 };
105
106 const res = service.getHumanReadableOnField(relation, 'Instance');
107 expect(res).toEqual('');
108 });
109
110 it('should return am empty string if the type on_field equal the base model', () => {
111 const relation = {
112 model: 'Test',
113 type: 'manytoone',
114 on_field: 'instance'
115 };
116
117 const res = service.getHumanReadableOnField(relation, 'Instance');
118 expect(res).toEqual('');
119 });
120 });
121
122 describe('the getModel method', () => {
123 it('should return the tab config for a single object', (done) => {
124 const relation = {
125 model: 'Test',
126 type: 'manytoone',
127 on_field: 'test'
128 };
129
130 const resModel = {foo: 'bar'};
131 const resFormCfg = {form: 'config'};
132
133 spyOn(XosModelStoreMock, 'get').and.callFake(() => {
134 const subject = new BehaviorSubject({});
135 subject.next(resModel);
136 return subject.asObservable();
137 });
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700138 spyOn(XosModeldefsCacheMock, 'get').and.returnValue({formCfg: resFormCfg});
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700139
140 service.getModel(relation, '5')
141 .then((res: IXosCrudRelationFormTabData) => {
142 expect(res.model).toEqual(resModel);
143 expect(res.class).toEqual('full');
144 expect(res.formConfig).toEqual(resFormCfg);
145 done();
146 });
147 scope.$apply();
148 });
149 });
150
151 describe('the getModels method', () => {
152 it('should return one related model', (done) => {
153 const relation = {
154 model: 'Test',
155 type: 'onetomany',
156 on_field: 'test'
157 };
158
159 const resModels = [
160 {test_id: 5},
161 {test_id: 25}
162 ];
163 const resTableCfg = {table: 'config'};
164
165 spyOn(XosModelStoreMock, 'query').and.callFake(() => {
166 const subject = new BehaviorSubject(resModels);
167 return subject.asObservable();
168 });
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700169 spyOn(XosModeldefsCacheMock, 'get').and.returnValue({tableCfg: resTableCfg});
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700170
171 service.getModels(relation, 5)
172 .then((res: IXosCrudRelationTableTabData) => {
173 expect(res.model.length).toEqual(1);
174 expect(res.class).toEqual('full');
175 expect(res.tableConfig).toEqual({
176 table: 'config',
177 filter: null
178 });
179 done();
180 });
181 scope.$apply();
182 });
183
184 it('should not return related models', (done) => {
185 const relation = {
186 model: 'Test',
187 type: 'onetomany',
188 on_field: 'test'
189 };
190
191 const resModels = [
192 {test_id: 15},
193 {test_id: 25}
194 ];
195 const resTableCfg = {table: 'config'};
196
197 spyOn(XosModelStoreMock, 'query').and.callFake(() => {
198 const subject = new BehaviorSubject(resModels);
199 return subject.asObservable();
200 });
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700201 spyOn(XosModeldefsCacheMock, 'get').and.returnValue({tableCfg: resTableCfg});
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700202
203 service.getModels(relation, 5)
204 .then((res: IXosCrudRelationTableTabData) => {
205 expect(res.model.length).toEqual(0);
206 expect(res.class).toEqual('empty');
207 expect(res.tableConfig).toEqual({
208 table: 'config',
209 filter: null
210 });
211 done();
212 });
213 scope.$apply();
214 });
215 });
216});