Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-resource'; |
| 4 | import {IModelStoreService, ModelStore} from './model.store'; |
| 5 | import {Subject} from 'rxjs'; |
| 6 | import {IWSEvent} from '../websocket/global'; |
| 7 | import {StoreHelpers} from '../helpers/store.helpers'; |
| 8 | import {ModelRest} from '../rest/model.rest'; |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 9 | import {ConfigHelpers} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 10 | import {AuthService} from '../rest/auth.rest'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 11 | |
| 12 | let service: IModelStoreService; |
| 13 | let httpBackend: ng.IHttpBackendService; |
| 14 | let $scope; |
| 15 | let WebSocket; |
| 16 | |
| 17 | class MockWs { |
| 18 | private _list; |
| 19 | constructor() { |
| 20 | this._list = new Subject<IWSEvent>(); |
| 21 | } |
| 22 | list() { |
| 23 | return this._list.asObservable(); |
| 24 | } |
| 25 | |
| 26 | next(event: IWSEvent) { |
| 27 | this._list.next(event); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | const queryData = [ |
| 32 | {id: 1, name: 'foo'}, |
| 33 | {id: 1, name: 'bar'} |
| 34 | ]; |
| 35 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 36 | const MockAppCfg = { |
| 37 | apiEndpoint: 'http://xos-test:3000/api', |
| 38 | websocketClient: 'http://xos-test:3000' |
| 39 | }; |
| 40 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 41 | describe('The ModelStore service', () => { |
| 42 | |
| 43 | beforeEach(() => { |
| 44 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 45 | .module('ModelStore', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 46 | .service('WebSocket', MockWs) |
| 47 | .service('StoreHelpers', StoreHelpers) // TODO mock |
| 48 | .service('ModelRest', ModelRest) // TODO mock |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 49 | .service('ModelStore', ModelStore) |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 50 | .service('ConfigHelpers', ConfigHelpers) // TODO mock |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 51 | .service('AuthService', AuthService) |
| 52 | .constant('AppConfig', MockAppCfg); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 53 | |
| 54 | angular.mock.module('ModelStore'); |
| 55 | }); |
| 56 | |
| 57 | beforeEach(angular.mock.inject(( |
| 58 | ModelStore: IModelStoreService, |
| 59 | $httpBackend: ng.IHttpBackendService, |
| 60 | _$rootScope_: ng.IRootScopeService, |
| 61 | _WebSocket_: any |
| 62 | ) => { |
| 63 | service = ModelStore; |
| 64 | httpBackend = $httpBackend; |
| 65 | $scope = _$rootScope_; |
| 66 | WebSocket = _WebSocket_; |
| 67 | |
| 68 | // ModelRest will call the backend |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 69 | httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 70 | .respond(queryData); |
| 71 | })); |
| 72 | |
| 73 | it('should return an Observable', () => { |
| 74 | expect(typeof service.query('test').subscribe).toBe('function'); |
| 75 | }); |
| 76 | |
| 77 | it('the first event should be the resource response', (done) => { |
| 78 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 79 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 80 | .subscribe(collection => { |
| 81 | event++; |
| 82 | if (event === 2) { |
| 83 | expect(collection[0].id).toEqual(queryData[0].id); |
| 84 | expect(collection[1].id).toEqual(queryData[1].id); |
| 85 | done(); |
| 86 | } |
| 87 | }); |
| 88 | $scope.$apply(); |
| 89 | httpBackend.flush(); |
| 90 | }); |
| 91 | |
| 92 | describe('when a web-socket event is received for that model', () => { |
| 93 | it('should update the collection', (done) => { |
| 94 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 95 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 96 | .subscribe( |
| 97 | collection => { |
| 98 | event++; |
| 99 | if (event === 3) { |
| 100 | expect(collection[0].id).toEqual(queryData[0].id); |
| 101 | expect(collection[1].id).toEqual(queryData[1].id); |
| 102 | expect(collection[2].id).toEqual(3); |
| 103 | expect(collection[2].name).toEqual('baz'); |
| 104 | done(); |
| 105 | } |
| 106 | }, |
| 107 | err => { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 108 | done(err); |
| 109 | } |
| 110 | ); |
| 111 | window.setTimeout(() => { |
| 112 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 113 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 114 | msg: { |
| 115 | changed_fields: ['id'], |
| 116 | object: {id: 3, name: 'baz'}, |
| 117 | pk: 3 |
| 118 | } |
| 119 | }); |
| 120 | }, 1); |
| 121 | $scope.$apply(); |
| 122 | httpBackend.flush(); |
| 123 | }); |
| 124 | }); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 125 | |
| 126 | describe('when multiple stores are requested', () => { |
| 127 | |
| 128 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 129 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 130 | .respond([ |
| 131 | {first: 'foo'} |
| 132 | ]); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 133 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 134 | .respond([ |
| 135 | {second: 'foo'} |
| 136 | ]); |
| 137 | }); |
| 138 | it('should create different Subject', (done) => { |
| 139 | let fevent = 0; |
| 140 | let sevent = 0; |
| 141 | service.query('first') |
| 142 | .subscribe(first => { |
| 143 | fevent++; |
| 144 | if (fevent >= 2) { |
| 145 | service.query('second') |
| 146 | .subscribe(second => { |
| 147 | sevent++; |
| 148 | if (sevent === 2) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 149 | expect(first).not.toEqual(second); |
| 150 | done(); |
| 151 | } |
| 152 | }); |
| 153 | } |
| 154 | }); |
| 155 | $scope.$apply(); |
| 156 | httpBackend.flush(); |
| 157 | }); |
| 158 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 159 | }); |