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 | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-resource'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 22 | import {IXosModelStoreService, XosModelStore} from './model.store'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 23 | import {Subject} from 'rxjs'; |
| 24 | import {IWSEvent} from '../websocket/global'; |
| 25 | import {StoreHelpers} from '../helpers/store.helpers'; |
| 26 | import {ModelRest} from '../rest/model.rest'; |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 27 | import {ConfigHelpers} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 28 | import {AuthService} from '../rest/auth.rest'; |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 29 | import {XosDebouncer} from '../../core/services/helpers/debounce.helper'; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 30 | import {IXosModeldefsCache} from '../helpers/modeldefs.service'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 31 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 32 | let service: IXosModelStoreService; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 33 | let httpBackend: ng.IHttpBackendService; |
| 34 | let $scope; |
| 35 | let WebSocket; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 36 | let XosModeldefsCache; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 37 | |
| 38 | class MockWs { |
| 39 | private _list; |
| 40 | constructor() { |
| 41 | this._list = new Subject<IWSEvent>(); |
| 42 | } |
| 43 | list() { |
| 44 | return this._list.asObservable(); |
| 45 | } |
| 46 | |
| 47 | next(event: IWSEvent) { |
| 48 | this._list.next(event); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | const queryData = [ |
| 53 | {id: 1, name: 'foo'}, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 54 | {id: 2, name: 'bar'} |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 55 | ]; |
| 56 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 57 | const MockAppCfg = { |
| 58 | apiEndpoint: 'http://xos-test:3000/api', |
| 59 | websocketClient: 'http://xos-test:3000' |
| 60 | }; |
| 61 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 62 | describe('The ModelStore service', () => { |
| 63 | |
| 64 | beforeEach(() => { |
| 65 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 66 | .module('ModelStore', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 67 | .service('WebSocket', MockWs) |
| 68 | .service('StoreHelpers', StoreHelpers) // TODO mock |
| 69 | .service('ModelRest', ModelRest) // TODO mock |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 70 | .service('XosModelStore', XosModelStore) |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 71 | .service('ConfigHelpers', ConfigHelpers) // TODO mock |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 72 | .service('AuthService', AuthService) |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 73 | .constant('AppConfig', MockAppCfg) |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 74 | .value('XosModeldefsCache', { |
| 75 | get: jasmine.createSpy('XosModeldefsCache.get').and.returnValue({}), |
| 76 | getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel') |
| 77 | }) |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 78 | .service('XosDebouncer', XosDebouncer); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 79 | |
| 80 | angular.mock.module('ModelStore'); |
| 81 | }); |
| 82 | |
| 83 | beforeEach(angular.mock.inject(( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 84 | XosModelStore: IXosModelStoreService, |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 85 | $httpBackend: ng.IHttpBackendService, |
| 86 | _$rootScope_: ng.IRootScopeService, |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 87 | _WebSocket_: any, |
| 88 | _XosModeldefsCache_: IXosModeldefsCache |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 89 | ) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 90 | service = XosModelStore; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 91 | httpBackend = $httpBackend; |
| 92 | $scope = _$rootScope_; |
| 93 | WebSocket = _WebSocket_; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 94 | XosModeldefsCache = _XosModeldefsCache_; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 95 | |
| 96 | // ModelRest will call the backend |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 97 | httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 98 | .respond(queryData); |
| 99 | })); |
| 100 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 101 | describe('the QUERY method', () => { |
| 102 | it('should return an Observable', () => { |
| 103 | expect(typeof service.query('test').subscribe).toBe('function'); |
| 104 | }); |
| 105 | |
| 106 | it('the first event should be the resource response', (done) => { |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 107 | XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 108 | let event = 0; |
| 109 | service.query('sample') |
| 110 | .subscribe(collection => { |
| 111 | event++; |
| 112 | if (event === 2) { |
| 113 | expect(collection[0].id).toEqual(queryData[0].id); |
| 114 | expect(collection[1].id).toEqual(queryData[1].id); |
| 115 | done(); |
| 116 | } |
| 117 | }); |
| 118 | $scope.$apply(); |
| 119 | httpBackend.flush(); |
| 120 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 121 | }); |
| 122 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 123 | describe('the GET method', () => { |
| 124 | it('should return an observable containing a single model', (done) => { |
| 125 | let event = 0; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 126 | XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 127 | service.get('sample', queryData[1].id) |
| 128 | .subscribe((model) => { |
| 129 | event++; |
| 130 | if (event === 2) { |
| 131 | expect(model.id).toEqual(queryData[1].id); |
| 132 | expect(model.name).toEqual(queryData[1].name); |
| 133 | done(); |
| 134 | } |
| 135 | }); |
| 136 | httpBackend.flush(); |
| 137 | $scope.$apply(); |
| 138 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 139 | }); |
| 140 | |
| 141 | describe('when a web-socket event is received for that model', () => { |
| 142 | it('should update the collection', (done) => { |
| 143 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 144 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 145 | .subscribe( |
| 146 | collection => { |
| 147 | event++; |
| 148 | if (event === 3) { |
| 149 | expect(collection[0].id).toEqual(queryData[0].id); |
| 150 | expect(collection[1].id).toEqual(queryData[1].id); |
| 151 | expect(collection[2].id).toEqual(3); |
| 152 | expect(collection[2].name).toEqual('baz'); |
| 153 | done(); |
| 154 | } |
| 155 | }, |
| 156 | err => { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 157 | done(err); |
| 158 | } |
| 159 | ); |
| 160 | window.setTimeout(() => { |
| 161 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 162 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 163 | msg: { |
| 164 | changed_fields: ['id'], |
| 165 | object: {id: 3, name: 'baz'}, |
| 166 | pk: 3 |
| 167 | } |
| 168 | }); |
| 169 | }, 1); |
| 170 | $scope.$apply(); |
| 171 | httpBackend.flush(); |
| 172 | }); |
| 173 | }); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 174 | |
| 175 | describe('when multiple stores are requested', () => { |
| 176 | |
| 177 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 178 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 179 | .respond([ |
| 180 | {first: 'foo'} |
| 181 | ]); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 182 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 183 | .respond([ |
| 184 | {second: 'foo'} |
| 185 | ]); |
| 186 | }); |
| 187 | it('should create different Subject', (done) => { |
| 188 | let fevent = 0; |
| 189 | let sevent = 0; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 190 | XosModeldefsCache.get.and.callFake(v => v); |
| 191 | XosModeldefsCache.getApiUrlFromModel.and.callFake(v => `/core/${v}s`); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 192 | service.query('first') |
| 193 | .subscribe(first => { |
| 194 | fevent++; |
| 195 | if (fevent >= 2) { |
| 196 | service.query('second') |
| 197 | .subscribe(second => { |
| 198 | sevent++; |
| 199 | if (sevent === 2) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 200 | expect(first).not.toEqual(second); |
| 201 | done(); |
| 202 | } |
| 203 | }); |
| 204 | } |
| 205 | }); |
| 206 | $scope.$apply(); |
| 207 | httpBackend.flush(); |
| 208 | }); |
| 209 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 210 | }); |