blob: 797685bc07b5cd850aa682e0feaba9e167b34c7a [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
4import {IModelStoreService, ModelStore} from './model.store';
5import {Subject} from 'rxjs';
6import {IWSEvent} from '../websocket/global';
7import {StoreHelpers} from '../helpers/store.helpers';
8import {ModelRest} from '../rest/model.rest';
9import {AppConfig} from '../../config/app.config';
10
11let service: IModelStoreService;
12let httpBackend: ng.IHttpBackendService;
13let $scope;
14let WebSocket;
15
16class 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
30const queryData = [
31 {id: 1, name: 'foo'},
32 {id: 1, name: 'bar'}
33];
34
35describe('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 Scandolod58d5042016-12-16 16:59:21 -080060 httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080061 .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 Scandolod58d5042016-12-16 16:59:21 -080070 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080071 .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 Scandolod58d5042016-12-16 16:59:21 -080086 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080087 .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 Scandolod58d5042016-12-16 16:59:21 -0800105 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800106 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});