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'; |
| 10 | |
| 11 | let service: IModelStoreService; |
| 12 | let httpBackend: ng.IHttpBackendService; |
| 13 | let $scope; |
| 14 | let WebSocket; |
| 15 | |
| 16 | class MockWs { |
| 17 | private _list; |
| 18 | constructor() { |
| 19 | this._list = new Subject<IWSEvent>(); |
| 20 | } |
| 21 | list() { |
| 22 | return this._list.asObservable(); |
| 23 | } |
| 24 | |
| 25 | next(event: IWSEvent) { |
| 26 | this._list.next(event); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const queryData = [ |
| 31 | {id: 1, name: 'foo'}, |
| 32 | {id: 1, name: 'bar'} |
| 33 | ]; |
| 34 | |
| 35 | describe('The ModelStore service', () => { |
| 36 | |
| 37 | beforeEach(() => { |
| 38 | angular |
| 39 | .module('ModelStore', ['ngResource']) |
| 40 | .service('WebSocket', MockWs) |
| 41 | .service('StoreHelpers', StoreHelpers) // TODO mock |
| 42 | .service('ModelRest', ModelRest) // TODO mock |
| 43 | .service('ModelStore', ModelStore); |
| 44 | |
| 45 | angular.mock.module('ModelStore'); |
| 46 | }); |
| 47 | |
| 48 | beforeEach(angular.mock.inject(( |
| 49 | ModelStore: IModelStoreService, |
| 50 | $httpBackend: ng.IHttpBackendService, |
| 51 | _$rootScope_: ng.IRootScopeService, |
| 52 | _WebSocket_: any |
| 53 | ) => { |
| 54 | service = ModelStore; |
| 55 | httpBackend = $httpBackend; |
| 56 | $scope = _$rootScope_; |
| 57 | WebSocket = _WebSocket_; |
| 58 | |
| 59 | // ModelRest will call the backend |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 60 | httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/samples`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 61 | .respond(queryData); |
| 62 | })); |
| 63 | |
| 64 | it('should return an Observable', () => { |
| 65 | expect(typeof service.query('test').subscribe).toBe('function'); |
| 66 | }); |
| 67 | |
| 68 | it('the first event should be the resource response', (done) => { |
| 69 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 70 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 71 | .subscribe(collection => { |
| 72 | event++; |
| 73 | if (event === 2) { |
| 74 | expect(collection[0].id).toEqual(queryData[0].id); |
| 75 | expect(collection[1].id).toEqual(queryData[1].id); |
| 76 | done(); |
| 77 | } |
| 78 | }); |
| 79 | $scope.$apply(); |
| 80 | httpBackend.flush(); |
| 81 | }); |
| 82 | |
| 83 | describe('when a web-socket event is received for that model', () => { |
| 84 | it('should update the collection', (done) => { |
| 85 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 86 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 87 | .subscribe( |
| 88 | collection => { |
| 89 | event++; |
| 90 | if (event === 3) { |
| 91 | expect(collection[0].id).toEqual(queryData[0].id); |
| 92 | expect(collection[1].id).toEqual(queryData[1].id); |
| 93 | expect(collection[2].id).toEqual(3); |
| 94 | expect(collection[2].name).toEqual('baz'); |
| 95 | done(); |
| 96 | } |
| 97 | }, |
| 98 | err => { |
| 99 | console.log(err); |
| 100 | done(err); |
| 101 | } |
| 102 | ); |
| 103 | window.setTimeout(() => { |
| 104 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 105 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 106 | msg: { |
| 107 | changed_fields: ['id'], |
| 108 | object: {id: 3, name: 'baz'}, |
| 109 | pk: 3 |
| 110 | } |
| 111 | }); |
| 112 | }, 1); |
| 113 | $scope.$apply(); |
| 114 | httpBackend.flush(); |
| 115 | }); |
| 116 | }); |
| 117 | }); |