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