blob: dd19c2710fe2ec8cb778f00ba20de7d04573165c [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 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
36describe('The ModelStore service', () => {
37
38 beforeEach(() => {
39 angular
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080040 .module('ModelStore', ['ngResource', 'toastr'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080041 .service('WebSocket', MockWs)
42 .service('StoreHelpers', StoreHelpers) // TODO mock
43 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolobac22452017-01-03 16:35:32 -080044 .service('ModelStore', ModelStore)
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080045 .service('ConfigHelpers', ConfigHelpers); // TODO mock
Matteo Scandoloa4a47112016-12-16 10:06:13 -080046
47 angular.mock.module('ModelStore');
48 });
49
50 beforeEach(angular.mock.inject((
51 ModelStore: IModelStoreService,
52 $httpBackend: ng.IHttpBackendService,
53 _$rootScope_: ng.IRootScopeService,
54 _WebSocket_: any
55 ) => {
56 service = ModelStore;
57 httpBackend = $httpBackend;
58 $scope = _$rootScope_;
59 WebSocket = _WebSocket_;
60
61 // ModelRest will call the backend
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080062 httpBackend.whenGET(`${AppConfig.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080063 .respond(queryData);
64 }));
65
66 it('should return an Observable', () => {
67 expect(typeof service.query('test').subscribe).toBe('function');
68 });
69
70 it('the first event should be the resource response', (done) => {
71 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080072 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080073 .subscribe(collection => {
74 event++;
75 if (event === 2) {
76 expect(collection[0].id).toEqual(queryData[0].id);
77 expect(collection[1].id).toEqual(queryData[1].id);
78 done();
79 }
80 });
81 $scope.$apply();
82 httpBackend.flush();
83 });
84
85 describe('when a web-socket event is received for that model', () => {
86 it('should update the collection', (done) => {
87 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -080088 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080089 .subscribe(
90 collection => {
91 event++;
92 if (event === 3) {
93 expect(collection[0].id).toEqual(queryData[0].id);
94 expect(collection[1].id).toEqual(queryData[1].id);
95 expect(collection[2].id).toEqual(3);
96 expect(collection[2].name).toEqual('baz');
97 done();
98 }
99 },
100 err => {
101 console.log(err);
102 done(err);
103 }
104 );
105 window.setTimeout(() => {
106 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800107 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800108 msg: {
109 changed_fields: ['id'],
110 object: {id: 3, name: 'baz'},
111 pk: 3
112 }
113 });
114 }, 1);
115 $scope.$apply();
116 httpBackend.flush();
117 });
118 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800119
120 describe('when multiple stores are requested', () => {
121
122 beforeEach(() => {
123 httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/firsts`)
124 .respond([
125 {first: 'foo'}
126 ]);
127 httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/seconds`)
128 .respond([
129 {second: 'foo'}
130 ]);
131 });
132 it('should create different Subject', (done) => {
133 let fevent = 0;
134 let sevent = 0;
135 service.query('first')
136 .subscribe(first => {
137 fevent++;
138 if (fevent >= 2) {
139 service.query('second')
140 .subscribe(second => {
141 sevent++;
142 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800143 expect(first).not.toEqual(second);
144 done();
145 }
146 });
147 }
148 });
149 $scope.$apply();
150 httpBackend.flush();
151 });
152 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800153});