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 | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
| 22 | import {StoreHelpers, IStoreHelpersService} from './store.helpers'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 23 | import {ModelRest} from '../rest/model.rest'; |
| 24 | import {BehaviorSubject} from 'rxjs'; |
| 25 | import {IWSEvent} from '../websocket/global'; |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 26 | import {ConfigHelpers} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 27 | import {AuthService} from '../rest/auth.rest'; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 28 | import {IXosModeldefsCache} from './modeldefs.service'; |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 29 | import {XosFormHelpers} from '../../core/form/form-helpers'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 30 | |
| 31 | let service: IStoreHelpersService; |
| 32 | let subject: BehaviorSubject<any>; |
| 33 | let resource: ng.resource.IResourceClass<any>; |
| 34 | let $resource: ng.resource.IResourceService; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 35 | let xosModelCache: IXosModeldefsCache; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 36 | let $log: ng.ILogService; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 37 | |
| 38 | describe('The StoreHelpers service', () => { |
| 39 | |
| 40 | beforeEach(() => { |
| 41 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 42 | .module('test', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 43 | .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 44 | .service('ModelRest', ModelRest) // NOTE evaluate mock |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 45 | .service('StoreHelpers', StoreHelpers) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 46 | .service('AuthService', AuthService) |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 47 | .service('XosFormHelpers', XosFormHelpers) |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 48 | .value('XosModeldefsCache', { |
| 49 | get: jasmine.createSpy('XosModeldefsCache.get'), |
| 50 | getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel') |
| 51 | }) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 52 | .value('AppConfig', {}); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 53 | |
| 54 | angular.mock.module('test'); |
| 55 | }); |
| 56 | |
| 57 | beforeEach(angular.mock.inject(( |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 59 | StoreHelpers: IStoreHelpersService, |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 60 | XosModeldefsCache: IXosModeldefsCache, |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 61 | _$resource_: ng.resource.IResourceService, |
| 62 | _$log_: ng.ILogService |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 63 | ) => { |
| 64 | $resource = _$resource_; |
| 65 | resource = $resource('/test'); |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 66 | xosModelCache = XosModeldefsCache; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 67 | service = StoreHelpers; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 68 | $log = _$log_; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 69 | })); |
| 70 | |
| 71 | it('should have an update collection method', () => { |
| 72 | expect(service.updateCollection).toBeDefined(); |
| 73 | }); |
| 74 | |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 75 | describe('the updateCollection method', () => { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 77 | beforeEach(() => { |
| 78 | subject = new BehaviorSubject([ |
| 79 | new resource({id: 1, name: 'test'}) |
| 80 | ]); |
| 81 | |
| 82 | }); |
| 83 | |
| 84 | it('should get the correct url for a core model', () => { |
| 85 | const mockModelDef = { |
| 86 | name: 'Node', |
| 87 | app: 'core' |
| 88 | }; |
| 89 | |
| 90 | xosModelCache.get['and'].returnValue(mockModelDef); |
| 91 | |
| 92 | const event: IWSEvent = { |
| 93 | model: 'TestModel', |
| 94 | msg: { |
| 95 | object: { |
| 96 | id: 1, |
| 97 | name: 'test' |
| 98 | }, |
| 99 | changed_fields: ['deleted'] |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | service.updateCollection(event, subject); |
| 104 | expect(xosModelCache.get).toHaveBeenCalledWith('TestModel'); |
| 105 | expect(xosModelCache.getApiUrlFromModel).toHaveBeenCalledWith(mockModelDef); |
| 106 | }); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 107 | }); |
| 108 | |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 109 | describe('when removing an item from a collection', () => { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 110 | beforeEach(() => { |
| 111 | subject = new BehaviorSubject([ |
| 112 | new resource({id: 1, name: 'test'}) |
| 113 | ]); |
| 114 | }); |
| 115 | |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 116 | describe('the updateCollection method', () => { |
| 117 | beforeEach(() => { |
| 118 | spyOn($log, 'error'); |
| 119 | }); |
| 120 | |
| 121 | it('should log an error if called with a delete event', () => { |
| 122 | const event: IWSEvent = { |
| 123 | deleted: true, |
| 124 | model: 'Deleted', |
| 125 | msg: { |
| 126 | changed_fields: [] |
| 127 | } |
| 128 | }; |
| 129 | service.updateCollection(event, subject); |
| 130 | expect($log.error).toHaveBeenCalled(); |
| 131 | }); |
| 132 | }); |
| 133 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 134 | it('should remove a model if it has been deleted', () => { |
| 135 | const event: IWSEvent = { |
| 136 | model: 'Test', |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 137 | deleted: true, |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 138 | msg: { |
| 139 | object: { |
| 140 | id: 1, |
| 141 | name: 'test' |
| 142 | }, |
| 143 | changed_fields: ['deleted'] |
| 144 | } |
| 145 | }; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 146 | service.removeItemFromCollection(event, subject); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 147 | expect(subject.value.length).toBe(0); |
| 148 | }); |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 149 | }); |
| 150 | |
| 151 | describe('when updating a collection', () => { |
| 152 | |
| 153 | beforeEach(() => { |
| 154 | subject = new BehaviorSubject([ |
| 155 | new resource({id: 1, name: 'test'}) |
| 156 | ]); |
| 157 | }); |
| 158 | |
| 159 | describe('the removeItemFromCollection method', () => { |
| 160 | beforeEach(() => { |
| 161 | spyOn($log, 'error'); |
| 162 | }); |
| 163 | |
| 164 | it('should log an error if called with an update event', () => { |
| 165 | const event: IWSEvent = { |
| 166 | model: 'Deleted', |
| 167 | msg: { |
| 168 | changed_fields: [] |
| 169 | } |
| 170 | }; |
| 171 | service.removeItemFromCollection(event, subject); |
| 172 | expect($log.error).toHaveBeenCalled(); |
| 173 | }); |
| 174 | }); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 175 | |
| 176 | it('should update a model if it has been updated', () => { |
| 177 | const event: IWSEvent = { |
| 178 | model: 'Test', |
| 179 | msg: { |
| 180 | object: { |
| 181 | id: 1, |
| 182 | name: 'test-updated' |
| 183 | }, |
| 184 | changed_fields: ['name'] |
| 185 | } |
| 186 | }; |
| 187 | service.updateCollection(event, subject); |
| 188 | expect(subject.value.length).toBe(1); |
| 189 | expect(subject.value[0].name).toBe('test-updated'); |
| 190 | }); |
| 191 | |
| 192 | it('should add a model if it has been created', () => { |
| 193 | const event: IWSEvent = { |
| 194 | model: 'Test', |
| 195 | msg: { |
| 196 | object: { |
| 197 | id: 2, |
| 198 | name: 'another-test' |
| 199 | }, |
| 200 | changed_fields: ['created'] |
| 201 | } |
| 202 | }; |
| 203 | service.updateCollection(event, subject); |
| 204 | expect(subject.value.length).toBe(2); |
| 205 | expect(subject.value[0].name).toBe('test'); |
| 206 | expect(subject.value[1].name).toBe('another-test'); |
| 207 | }); |
| 208 | |
| 209 | describe('when adding a model', () => { |
| 210 | |
| 211 | beforeEach(() => { |
| 212 | const event: IWSEvent = { |
| 213 | model: 'Test', |
| 214 | msg: { |
| 215 | object: { |
| 216 | id: 2, |
| 217 | name: 'another-test' |
| 218 | }, |
| 219 | changed_fields: ['created'] |
| 220 | } |
| 221 | }; |
| 222 | service.updateCollection(event, subject); |
| 223 | }); |
| 224 | |
| 225 | it('should create a resource', () => { |
| 226 | expect(subject.value[1].$save).toBeDefined(); |
| 227 | expect(subject.value[1].$delete).toBeDefined(); |
| 228 | }); |
| 229 | |
| 230 | xit('should automatically create the appropriate resource', () => { |
| 231 | // TODO test that the url of the resource is the correct one, |
| 232 | // use httpbackend and mock a call?? any faster way?? |
| 233 | }); |
| 234 | }); |
| 235 | }); |
| 236 | |
| 237 | }); |