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