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