blob: 846ef84debb29166a5e3b033dd29c24d405b760f [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'},
Matteo Scandolo5d962a32017-08-01 18:16:14 -070034 {id: 2, name: 'bar'}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080035];
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
Matteo Scandolo5d962a32017-08-01 18:16:14 -070075 describe('the QUERY method', () => {
76 it('should return an Observable', () => {
77 expect(typeof service.query('test').subscribe).toBe('function');
78 });
79
80 it('the first event should be the resource response', (done) => {
81 let event = 0;
82 service.query('sample')
83 .subscribe(collection => {
84 event++;
85 if (event === 2) {
86 expect(collection[0].id).toEqual(queryData[0].id);
87 expect(collection[1].id).toEqual(queryData[1].id);
88 done();
89 }
90 });
91 $scope.$apply();
92 httpBackend.flush();
93 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -080094 });
95
Matteo Scandolo5d962a32017-08-01 18:16:14 -070096 describe('the GET method', () => {
97 it('should return an observable containing a single model', (done) => {
98 let event = 0;
99 service.get('sample', queryData[1].id)
100 .subscribe((model) => {
101 event++;
102 if (event === 2) {
103 expect(model.id).toEqual(queryData[1].id);
104 expect(model.name).toEqual(queryData[1].name);
105 done();
106 }
107 });
108 httpBackend.flush();
109 $scope.$apply();
110 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800111 });
112
113 describe('when a web-socket event is received for that model', () => {
114 it('should update the collection', (done) => {
115 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800116 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800117 .subscribe(
118 collection => {
119 event++;
120 if (event === 3) {
121 expect(collection[0].id).toEqual(queryData[0].id);
122 expect(collection[1].id).toEqual(queryData[1].id);
123 expect(collection[2].id).toEqual(3);
124 expect(collection[2].name).toEqual('baz');
125 done();
126 }
127 },
128 err => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800129 done(err);
130 }
131 );
132 window.setTimeout(() => {
133 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800134 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800135 msg: {
136 changed_fields: ['id'],
137 object: {id: 3, name: 'baz'},
138 pk: 3
139 }
140 });
141 }, 1);
142 $scope.$apply();
143 httpBackend.flush();
144 });
145 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800146
147 describe('when multiple stores are requested', () => {
148
149 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800150 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800151 .respond([
152 {first: 'foo'}
153 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800154 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800155 .respond([
156 {second: 'foo'}
157 ]);
158 });
159 it('should create different Subject', (done) => {
160 let fevent = 0;
161 let sevent = 0;
162 service.query('first')
163 .subscribe(first => {
164 fevent++;
165 if (fevent >= 2) {
166 service.query('second')
167 .subscribe(second => {
168 sevent++;
169 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800170 expect(first).not.toEqual(second);
171 done();
172 }
173 });
174 }
175 });
176 $scope.$apply();
177 httpBackend.flush();
178 });
179 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800180});