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 => { |
| 108 | console.log(err); |
| 109 | done(err); |
| 110 | } |
| 111 | ); |
| 112 | window.setTimeout(() => { |
| 113 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 114 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 115 | msg: { |
| 116 | changed_fields: ['id'], |
| 117 | object: {id: 3, name: 'baz'}, |
| 118 | pk: 3 |
| 119 | } |
| 120 | }); |
| 121 | }, 1); |
| 122 | $scope.$apply(); |
| 123 | httpBackend.flush(); |
| 124 | }); |
| 125 | }); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 126 | |
| 127 | describe('when multiple stores are requested', () => { |
| 128 | |
| 129 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 130 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 131 | .respond([ |
| 132 | {first: 'foo'} |
| 133 | ]); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 134 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 135 | .respond([ |
| 136 | {second: 'foo'} |
| 137 | ]); |
| 138 | }); |
| 139 | it('should create different Subject', (done) => { |
| 140 | let fevent = 0; |
| 141 | let sevent = 0; |
| 142 | service.query('first') |
| 143 | .subscribe(first => { |
| 144 | fevent++; |
| 145 | if (fevent >= 2) { |
| 146 | service.query('second') |
| 147 | .subscribe(second => { |
| 148 | sevent++; |
| 149 | if (sevent === 2) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 150 | expect(first).not.toEqual(second); |
| 151 | done(); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | }); |
| 156 | $scope.$apply(); |
| 157 | httpBackend.flush(); |
| 158 | }); |
| 159 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 160 | }); |