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