blob: 5bdc4987739a2ee41f57d18139b3fec924d73b3a [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';
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080010import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080011import {AuthService} from '../rest/auth.rest';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080012
13let service: IModelStoreService;
14let 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
37describe('The ModelStore service', () => {
38
39 beforeEach(() => {
40 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080041 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080042 .service('WebSocket', MockWs)
43 .service('StoreHelpers', StoreHelpers) // TODO mock
44 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolobac22452017-01-03 16:35:32 -080045 .service('ModelStore', ModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080046 .service('ConfigHelpers', ConfigHelpers) // TODO mock
47 .service('AuthService', AuthService);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080048
49 angular.mock.module('ModelStore');
50 });
51
52 beforeEach(angular.mock.inject((
53 ModelStore: IModelStoreService,
54 $httpBackend: ng.IHttpBackendService,
55 _$rootScope_: ng.IRootScopeService,
56 _WebSocket_: any
57 ) => {
58 service = ModelStore;
59 httpBackend = $httpBackend;
60 $scope = _$rootScope_;
61 WebSocket = _WebSocket_;
62
63 // ModelRest will call the backend
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080064 httpBackend.whenGET(`${AppConfig.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080065 .respond(queryData);
66 }));
67
68 it('should return an Observable', () => {
69 expect(typeof service.query('test').subscribe).toBe('function');
70 });
71
72 it('the first event should be the resource response', (done) => {
73 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080074 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080075 .subscribe(collection => {
76 event++;
77 if (event === 2) {
78 expect(collection[0].id).toEqual(queryData[0].id);
79 expect(collection[1].id).toEqual(queryData[1].id);
80 done();
81 }
82 });
83 $scope.$apply();
84 httpBackend.flush();
85 });
86
87 describe('when a web-socket event is received for that model', () => {
88 it('should update the collection', (done) => {
89 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080090 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080091 .subscribe(
92 collection => {
93 event++;
94 if (event === 3) {
95 expect(collection[0].id).toEqual(queryData[0].id);
96 expect(collection[1].id).toEqual(queryData[1].id);
97 expect(collection[2].id).toEqual(3);
98 expect(collection[2].name).toEqual('baz');
99 done();
100 }
101 },
102 err => {
103 console.log(err);
104 done(err);
105 }
106 );
107 window.setTimeout(() => {
108 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800109 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800110 msg: {
111 changed_fields: ['id'],
112 object: {id: 3, name: 'baz'},
113 pk: 3
114 }
115 });
116 }, 1);
117 $scope.$apply();
118 httpBackend.flush();
119 });
120 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800121
122 describe('when multiple stores are requested', () => {
123
124 beforeEach(() => {
125 httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/firsts`)
126 .respond([
127 {first: 'foo'}
128 ]);
129 httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/seconds`)
130 .respond([
131 {second: 'foo'}
132 ]);
133 });
134 it('should create different Subject', (done) => {
135 let fevent = 0;
136 let sevent = 0;
137 service.query('first')
138 .subscribe(first => {
139 fevent++;
140 if (fevent >= 2) {
141 service.query('second')
142 .subscribe(second => {
143 sevent++;
144 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800145 expect(first).not.toEqual(second);
146 done();
147 }
148 });
149 }
150 });
151 $scope.$apply();
152 httpBackend.flush();
153 });
154 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800155});