Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-resource'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 4 | import {IXosModelStoreService, XosModelStore} from './model.store'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 5 | import {Subject} from 'rxjs'; |
| 6 | import {IWSEvent} from '../websocket/global'; |
| 7 | import {StoreHelpers} from '../helpers/store.helpers'; |
| 8 | import {ModelRest} from '../rest/model.rest'; |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 9 | import {ConfigHelpers} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 10 | import {AuthService} from '../rest/auth.rest'; |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 11 | import {XosDebouncer} from '../../core/services/helpers/debounce.helper'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 12 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 13 | let service: IXosModelStoreService; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 14 | let httpBackend: ng.IHttpBackendService; |
| 15 | let $scope; |
| 16 | let WebSocket; |
| 17 | |
| 18 | class 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 | |
| 32 | const queryData = [ |
| 33 | {id: 1, name: 'foo'}, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 34 | {id: 2, name: 'bar'} |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 35 | ]; |
| 36 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 37 | const MockAppCfg = { |
| 38 | apiEndpoint: 'http://xos-test:3000/api', |
| 39 | websocketClient: 'http://xos-test:3000' |
| 40 | }; |
| 41 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 42 | describe('The ModelStore service', () => { |
| 43 | |
| 44 | beforeEach(() => { |
| 45 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 46 | .module('ModelStore', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 47 | .service('WebSocket', MockWs) |
| 48 | .service('StoreHelpers', StoreHelpers) // TODO mock |
| 49 | .service('ModelRest', ModelRest) // TODO mock |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 50 | .service('XosModelStore', XosModelStore) |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 51 | .service('ConfigHelpers', ConfigHelpers) // TODO mock |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 52 | .service('AuthService', AuthService) |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 53 | .constant('AppConfig', MockAppCfg) |
| 54 | .service('XosDebouncer', XosDebouncer); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 55 | |
| 56 | angular.mock.module('ModelStore'); |
| 57 | }); |
| 58 | |
| 59 | beforeEach(angular.mock.inject(( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 60 | XosModelStore: IXosModelStoreService, |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 61 | $httpBackend: ng.IHttpBackendService, |
| 62 | _$rootScope_: ng.IRootScopeService, |
| 63 | _WebSocket_: any |
| 64 | ) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 65 | service = XosModelStore; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 66 | httpBackend = $httpBackend; |
| 67 | $scope = _$rootScope_; |
| 68 | WebSocket = _WebSocket_; |
| 69 | |
| 70 | // ModelRest will call the backend |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 71 | httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 72 | .respond(queryData); |
| 73 | })); |
| 74 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 75 | 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 94 | }); |
| 95 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 96 | 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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 111 | }); |
| 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 Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 116 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 117 | .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 Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 129 | done(err); |
| 130 | } |
| 131 | ); |
| 132 | window.setTimeout(() => { |
| 133 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 134 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 135 | 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 Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 146 | |
| 147 | describe('when multiple stores are requested', () => { |
| 148 | |
| 149 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 150 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 151 | .respond([ |
| 152 | {first: 'foo'} |
| 153 | ]); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 154 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 155 | .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 Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 170 | expect(first).not.toEqual(second); |
| 171 | done(); |
| 172 | } |
| 173 | }); |
| 174 | } |
| 175 | }); |
| 176 | $scope.$apply(); |
| 177 | httpBackend.flush(); |
| 178 | }); |
| 179 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 180 | }); |