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 | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 29 | |
| 30 | let service: IStoreHelpersService; |
| 31 | let subject: BehaviorSubject<any>; |
| 32 | let resource: ng.resource.IResourceClass<any>; |
| 33 | let $resource: ng.resource.IResourceService; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 34 | let xosModelCache: IXosModeldefsCache; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 35 | |
| 36 | describe('The StoreHelpers service', () => { |
| 37 | |
| 38 | beforeEach(() => { |
| 39 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 40 | .module('test', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 41 | .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 42 | .service('ModelRest', ModelRest) // NOTE evaluate mock |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 43 | .service('StoreHelpers', StoreHelpers) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 44 | .service('AuthService', AuthService) |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 45 | .value('XosModeldefsCache', { |
| 46 | get: jasmine.createSpy('XosModeldefsCache.get'), |
| 47 | getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel') |
| 48 | }) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 49 | .value('AppConfig', {}); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 50 | |
| 51 | angular.mock.module('test'); |
| 52 | }); |
| 53 | |
| 54 | beforeEach(angular.mock.inject(( |
| 55 | StoreHelpers: IStoreHelpersService, |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 56 | XosModeldefsCache: IXosModeldefsCache, |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 57 | _$resource_: ng.resource.IResourceService |
| 58 | ) => { |
| 59 | $resource = _$resource_; |
| 60 | resource = $resource('/test'); |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 61 | xosModelCache = XosModeldefsCache; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 62 | service = StoreHelpers; |
| 63 | })); |
| 64 | |
| 65 | it('should have an update collection method', () => { |
| 66 | expect(service.updateCollection).toBeDefined(); |
| 67 | }); |
| 68 | |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 69 | describe('the updateCollection method', () => { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 70 | |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 71 | beforeEach(() => { |
| 72 | subject = new BehaviorSubject([ |
| 73 | new resource({id: 1, name: 'test'}) |
| 74 | ]); |
| 75 | |
| 76 | }); |
| 77 | |
| 78 | it('should get the correct url for a core model', () => { |
| 79 | const mockModelDef = { |
| 80 | name: 'Node', |
| 81 | app: 'core' |
| 82 | }; |
| 83 | |
| 84 | xosModelCache.get['and'].returnValue(mockModelDef); |
| 85 | |
| 86 | const event: IWSEvent = { |
| 87 | model: 'TestModel', |
| 88 | msg: { |
| 89 | object: { |
| 90 | id: 1, |
| 91 | name: 'test' |
| 92 | }, |
| 93 | changed_fields: ['deleted'] |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | service.updateCollection(event, subject); |
| 98 | expect(xosModelCache.get).toHaveBeenCalledWith('TestModel'); |
| 99 | expect(xosModelCache.getApiUrlFromModel).toHaveBeenCalledWith(mockModelDef); |
| 100 | }); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 101 | }); |
| 102 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 103 | describe('when updating a collection', () => { |
| 104 | |
| 105 | beforeEach(() => { |
| 106 | subject = new BehaviorSubject([ |
| 107 | new resource({id: 1, name: 'test'}) |
| 108 | ]); |
| 109 | }); |
| 110 | |
| 111 | it('should remove a model if it has been deleted', () => { |
| 112 | const event: IWSEvent = { |
| 113 | model: 'Test', |
| 114 | msg: { |
| 115 | object: { |
| 116 | id: 1, |
| 117 | name: 'test' |
| 118 | }, |
| 119 | changed_fields: ['deleted'] |
| 120 | } |
| 121 | }; |
| 122 | service.updateCollection(event, subject); |
| 123 | expect(subject.value.length).toBe(0); |
| 124 | }); |
| 125 | |
| 126 | it('should update a model if it has been updated', () => { |
| 127 | const event: IWSEvent = { |
| 128 | model: 'Test', |
| 129 | msg: { |
| 130 | object: { |
| 131 | id: 1, |
| 132 | name: 'test-updated' |
| 133 | }, |
| 134 | changed_fields: ['name'] |
| 135 | } |
| 136 | }; |
| 137 | service.updateCollection(event, subject); |
| 138 | expect(subject.value.length).toBe(1); |
| 139 | expect(subject.value[0].name).toBe('test-updated'); |
| 140 | }); |
| 141 | |
| 142 | it('should add a model if it has been created', () => { |
| 143 | const event: IWSEvent = { |
| 144 | model: 'Test', |
| 145 | msg: { |
| 146 | object: { |
| 147 | id: 2, |
| 148 | name: 'another-test' |
| 149 | }, |
| 150 | changed_fields: ['created'] |
| 151 | } |
| 152 | }; |
| 153 | service.updateCollection(event, subject); |
| 154 | expect(subject.value.length).toBe(2); |
| 155 | expect(subject.value[0].name).toBe('test'); |
| 156 | expect(subject.value[1].name).toBe('another-test'); |
| 157 | }); |
| 158 | |
| 159 | describe('when adding a model', () => { |
| 160 | |
| 161 | beforeEach(() => { |
| 162 | const event: IWSEvent = { |
| 163 | model: 'Test', |
| 164 | msg: { |
| 165 | object: { |
| 166 | id: 2, |
| 167 | name: 'another-test' |
| 168 | }, |
| 169 | changed_fields: ['created'] |
| 170 | } |
| 171 | }; |
| 172 | service.updateCollection(event, subject); |
| 173 | }); |
| 174 | |
| 175 | it('should create a resource', () => { |
| 176 | expect(subject.value[1].$save).toBeDefined(); |
| 177 | expect(subject.value[1].$delete).toBeDefined(); |
| 178 | }); |
| 179 | |
| 180 | xit('should automatically create the appropriate resource', () => { |
| 181 | // TODO test that the url of the resource is the correct one, |
| 182 | // use httpbackend and mock a call?? any faster way?? |
| 183 | }); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | }); |