blob: b114de4bcf4c40e7c09899cee5f205cf00c6c23b [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08004import {IXosModelStoreService, XosModelStore} from './model.store';
Matteo Scandoloa4a47112016-12-16 10:06:13 -08005import {Subject} from 'rxjs';
6import {IWSEvent} from '../websocket/global';
7import {StoreHelpers} from '../helpers/store.helpers';
8import {ModelRest} from '../rest/model.rest';
Matteo Scandolo1c5905f2017-01-04 17:41:15 -08009import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080010import {AuthService} from '../rest/auth.rest';
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080011import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080012
Matteo Scandolo47860fe2017-02-02 12:05:55 -080013let service: IXosModelStoreService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080014let httpBackend: ng.IHttpBackendService;
15let $scope;
16let WebSocket;
17
18class 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
32const queryData = [
33 {id: 1, name: 'foo'},
34 {id: 1, name: 'bar'}
35];
36
Matteo Scandolo828d1e82017-01-17 14:49:38 -080037const MockAppCfg = {
38 apiEndpoint: 'http://xos-test:3000/api',
39 websocketClient: 'http://xos-test:3000'
40};
41
Matteo Scandoloa4a47112016-12-16 10:06:13 -080042describe('The ModelStore service', () => {
43
44 beforeEach(() => {
45 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080046 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080047 .service('WebSocket', MockWs)
48 .service('StoreHelpers', StoreHelpers) // TODO mock
49 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 .service('XosModelStore', XosModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080051 .service('ConfigHelpers', ConfigHelpers) // TODO mock
Matteo Scandolo828d1e82017-01-17 14:49:38 -080052 .service('AuthService', AuthService)
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080053 .constant('AppConfig', MockAppCfg)
54 .service('XosDebouncer', XosDebouncer);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080055
56 angular.mock.module('ModelStore');
57 });
58
59 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080060 XosModelStore: IXosModelStoreService,
Matteo Scandoloa4a47112016-12-16 10:06:13 -080061 $httpBackend: ng.IHttpBackendService,
62 _$rootScope_: ng.IRootScopeService,
63 _WebSocket_: any
64 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080065 service = XosModelStore;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080066 httpBackend = $httpBackend;
67 $scope = _$rootScope_;
68 WebSocket = _WebSocket_;
69
70 // ModelRest will call the backend
Matteo Scandolo828d1e82017-01-17 14:49:38 -080071 httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080072 .respond(queryData);
73 }));
74
75 it('should return an Observable', () => {
76 expect(typeof service.query('test').subscribe).toBe('function');
77 });
78
79 it('the first event should be the resource response', (done) => {
80 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080081 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080082 .subscribe(collection => {
83 event++;
84 if (event === 2) {
85 expect(collection[0].id).toEqual(queryData[0].id);
86 expect(collection[1].id).toEqual(queryData[1].id);
87 done();
88 }
89 });
90 $scope.$apply();
91 httpBackend.flush();
92 });
93
94 describe('when a web-socket event is received for that model', () => {
95 it('should update the collection', (done) => {
96 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080097 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080098 .subscribe(
99 collection => {
100 event++;
101 if (event === 3) {
102 expect(collection[0].id).toEqual(queryData[0].id);
103 expect(collection[1].id).toEqual(queryData[1].id);
104 expect(collection[2].id).toEqual(3);
105 expect(collection[2].name).toEqual('baz');
106 done();
107 }
108 },
109 err => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800110 done(err);
111 }
112 );
113 window.setTimeout(() => {
114 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800115 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800116 msg: {
117 changed_fields: ['id'],
118 object: {id: 3, name: 'baz'},
119 pk: 3
120 }
121 });
122 }, 1);
123 $scope.$apply();
124 httpBackend.flush();
125 });
126 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800127
128 describe('when multiple stores are requested', () => {
129
130 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800131 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800132 .respond([
133 {first: 'foo'}
134 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800135 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800136 .respond([
137 {second: 'foo'}
138 ]);
139 });
140 it('should create different Subject', (done) => {
141 let fevent = 0;
142 let sevent = 0;
143 service.query('first')
144 .subscribe(first => {
145 fevent++;
146 if (fevent >= 2) {
147 service.query('second')
148 .subscribe(second => {
149 sevent++;
150 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800151 expect(first).not.toEqual(second);
152 done();
153 }
154 });
155 }
156 });
157 $scope.$apply();
158 httpBackend.flush();
159 });
160 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800161});