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