blob: 7173658fe3b5aa40795ce5460897b93f8944379a [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
Matteo Scandolo47860fe2017-02-02 12:05:55 -08004import {IXosModelStoreService, ModelStore} 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 Scandoloa4a47112016-12-16 10:06:13 -080011
Matteo Scandolo47860fe2017-02-02 12:05:55 -080012let service: IXosModelStoreService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080013let 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((
Matteo Scandolo47860fe2017-02-02 12:05:55 -080058 ModelStore: IXosModelStoreService,
Matteo Scandoloa4a47112016-12-16 10:06:13 -080059 $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 => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800108 done(err);
109 }
110 );
111 window.setTimeout(() => {
112 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800113 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800114 msg: {
115 changed_fields: ['id'],
116 object: {id: 3, name: 'baz'},
117 pk: 3
118 }
119 });
120 }, 1);
121 $scope.$apply();
122 httpBackend.flush();
123 });
124 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800125
126 describe('when multiple stores are requested', () => {
127
128 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800129 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800130 .respond([
131 {first: 'foo'}
132 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800133 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800134 .respond([
135 {second: 'foo'}
136 ]);
137 });
138 it('should create different Subject', (done) => {
139 let fevent = 0;
140 let sevent = 0;
141 service.query('first')
142 .subscribe(first => {
143 fevent++;
144 if (fevent >= 2) {
145 service.query('second')
146 .subscribe(second => {
147 sevent++;
148 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800149 expect(first).not.toEqual(second);
150 done();
151 }
152 });
153 }
154 });
155 $scope.$apply();
156 httpBackend.flush();
157 });
158 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800159});