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