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 | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 28 | |
| 29 | let service: IStoreHelpersService; |
| 30 | let subject: BehaviorSubject<any>; |
| 31 | let resource: ng.resource.IResourceClass<any>; |
| 32 | let $resource: ng.resource.IResourceService; |
| 33 | |
| 34 | describe('The StoreHelpers service', () => { |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 38 | .module('test', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 39 | .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 40 | .service('ModelRest', ModelRest) // NOTE evaluate mock |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 41 | .service('StoreHelpers', StoreHelpers) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 42 | .service('AuthService', AuthService) |
| 43 | .value('AppConfig', {}); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 44 | |
| 45 | angular.mock.module('test'); |
| 46 | }); |
| 47 | |
| 48 | beforeEach(angular.mock.inject(( |
| 49 | StoreHelpers: IStoreHelpersService, |
| 50 | _$resource_: ng.resource.IResourceService |
| 51 | ) => { |
| 52 | $resource = _$resource_; |
| 53 | resource = $resource('/test'); |
| 54 | service = StoreHelpers; |
| 55 | })); |
| 56 | |
| 57 | it('should have an update collection method', () => { |
| 58 | expect(service.updateCollection).toBeDefined(); |
| 59 | }); |
| 60 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 61 | |
| 62 | it('should convert a core model name in an URL', () => { |
| 63 | expect(service.urlFromCoreModel('Slice')).toBe('/core/slices'); |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 64 | expect(service.urlFromCoreModel('Xos')).toBe('/core/xoses'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 65 | expect(service.urlFromCoreModel('SiteRole')).toBe('/core/siteroles'); |
| 66 | expect(service.urlFromCoreModel('SliceRole')).toBe('/core/sliceroles'); |
| 67 | expect(service.urlFromCoreModel('SlicePrivilege')).toBe('/core/sliceprivileges'); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 68 | }); |
| 69 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 70 | describe('when updating a collection', () => { |
| 71 | |
| 72 | beforeEach(() => { |
| 73 | subject = new BehaviorSubject([ |
| 74 | new resource({id: 1, name: 'test'}) |
| 75 | ]); |
| 76 | }); |
| 77 | |
| 78 | it('should remove a model if it has been deleted', () => { |
| 79 | const event: IWSEvent = { |
| 80 | model: 'Test', |
| 81 | msg: { |
| 82 | object: { |
| 83 | id: 1, |
| 84 | name: 'test' |
| 85 | }, |
| 86 | changed_fields: ['deleted'] |
| 87 | } |
| 88 | }; |
| 89 | service.updateCollection(event, subject); |
| 90 | expect(subject.value.length).toBe(0); |
| 91 | }); |
| 92 | |
| 93 | it('should update a model if it has been updated', () => { |
| 94 | const event: IWSEvent = { |
| 95 | model: 'Test', |
| 96 | msg: { |
| 97 | object: { |
| 98 | id: 1, |
| 99 | name: 'test-updated' |
| 100 | }, |
| 101 | changed_fields: ['name'] |
| 102 | } |
| 103 | }; |
| 104 | service.updateCollection(event, subject); |
| 105 | expect(subject.value.length).toBe(1); |
| 106 | expect(subject.value[0].name).toBe('test-updated'); |
| 107 | }); |
| 108 | |
| 109 | it('should add a model if it has been created', () => { |
| 110 | const event: IWSEvent = { |
| 111 | model: 'Test', |
| 112 | msg: { |
| 113 | object: { |
| 114 | id: 2, |
| 115 | name: 'another-test' |
| 116 | }, |
| 117 | changed_fields: ['created'] |
| 118 | } |
| 119 | }; |
| 120 | service.updateCollection(event, subject); |
| 121 | expect(subject.value.length).toBe(2); |
| 122 | expect(subject.value[0].name).toBe('test'); |
| 123 | expect(subject.value[1].name).toBe('another-test'); |
| 124 | }); |
| 125 | |
| 126 | describe('when adding a model', () => { |
| 127 | |
| 128 | beforeEach(() => { |
| 129 | const event: IWSEvent = { |
| 130 | model: 'Test', |
| 131 | msg: { |
| 132 | object: { |
| 133 | id: 2, |
| 134 | name: 'another-test' |
| 135 | }, |
| 136 | changed_fields: ['created'] |
| 137 | } |
| 138 | }; |
| 139 | service.updateCollection(event, subject); |
| 140 | }); |
| 141 | |
| 142 | it('should create a resource', () => { |
| 143 | expect(subject.value[1].$save).toBeDefined(); |
| 144 | expect(subject.value[1].$delete).toBeDefined(); |
| 145 | }); |
| 146 | |
| 147 | xit('should automatically create the appropriate resource', () => { |
| 148 | // TODO test that the url of the resource is the correct one, |
| 149 | // use httpbackend and mock a call?? any faster way?? |
| 150 | }); |
| 151 | }); |
| 152 | }); |
| 153 | |
| 154 | }); |