blob: 97ce89034b39dec52f330f52832d71dd73340753 [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';
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 Scandoloa4a47112016-12-16 10:06:13 -080011
12let service: IModelStoreService;
13let httpBackend: ng.IHttpBackendService;
14let $scope;
15let WebSocket;
16
17class 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
31const queryData = [
32 {id: 1, name: 'foo'},
33 {id: 1, name: 'bar'}
34];
35
Matteo Scandolo828d1e82017-01-17 14:49:38 -080036const MockAppCfg = {
37 apiEndpoint: 'http://xos-test:3000/api',
38 websocketClient: 'http://xos-test:3000'
39};
40
Matteo Scandoloa4a47112016-12-16 10:06:13 -080041describe('The ModelStore service', () => {
42
43 beforeEach(() => {
44 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080045 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080046 .service('WebSocket', MockWs)
47 .service('StoreHelpers', StoreHelpers) // TODO mock
48 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolobac22452017-01-03 16:35:32 -080049 .service('ModelStore', ModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080050 .service('ConfigHelpers', ConfigHelpers) // TODO mock
Matteo Scandolo828d1e82017-01-17 14:49:38 -080051 .service('AuthService', AuthService)
52 .constant('AppConfig', MockAppCfg);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080053
54 angular.mock.module('ModelStore');
55 });
56
57 beforeEach(angular.mock.inject((
58 ModelStore: IModelStoreService,
59 $httpBackend: ng.IHttpBackendService,
60 _$rootScope_: ng.IRootScopeService,
61 _WebSocket_: any
62 ) => {
63 service = ModelStore;
64 httpBackend = $httpBackend;
65 $scope = _$rootScope_;
66 WebSocket = _WebSocket_;
67
68 // ModelRest will call the backend
Matteo Scandolo828d1e82017-01-17 14:49:38 -080069 httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080070 .respond(queryData);
71 }));
72
73 it('should return an Observable', () => {
74 expect(typeof service.query('test').subscribe).toBe('function');
75 });
76
77 it('the first event should be the resource response', (done) => {
78 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080079 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080080 .subscribe(collection => {
81 event++;
82 if (event === 2) {
83 expect(collection[0].id).toEqual(queryData[0].id);
84 expect(collection[1].id).toEqual(queryData[1].id);
85 done();
86 }
87 });
88 $scope.$apply();
89 httpBackend.flush();
90 });
91
92 describe('when a web-socket event is received for that model', () => {
93 it('should update the collection', (done) => {
94 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080095 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080096 .subscribe(
97 collection => {
98 event++;
99 if (event === 3) {
100 expect(collection[0].id).toEqual(queryData[0].id);
101 expect(collection[1].id).toEqual(queryData[1].id);
102 expect(collection[2].id).toEqual(3);
103 expect(collection[2].name).toEqual('baz');
104 done();
105 }
106 },
107 err => {
108 console.log(err);
109 done(err);
110 }
111 );
112 window.setTimeout(() => {
113 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800114 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800115 msg: {
116 changed_fields: ['id'],
117 object: {id: 3, name: 'baz'},
118 pk: 3
119 }
120 });
121 }, 1);
122 $scope.$apply();
123 httpBackend.flush();
124 });
125 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800126
127 describe('when multiple stores are requested', () => {
128
129 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800130 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800131 .respond([
132 {first: 'foo'}
133 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800134 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800135 .respond([
136 {second: 'foo'}
137 ]);
138 });
139 it('should create different Subject', (done) => {
140 let fevent = 0;
141 let sevent = 0;
142 service.query('first')
143 .subscribe(first => {
144 fevent++;
145 if (fevent >= 2) {
146 service.query('second')
147 .subscribe(second => {
148 sevent++;
149 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800150 expect(first).not.toEqual(second);
151 done();
152 }
153 });
154 }
155 });
156 $scope.$apply();
157 httpBackend.flush();
158 });
159 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800160});