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'}, |
| 34 | {id: 1, name: 'bar'} |
| 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 | |
| 75 | it('should return an Observable', () => { |
| 76 | expect(typeof service.query('test').subscribe).toBe('function'); |
| 77 | }); |
| 78 | |
| 79 | it('the first event should be the resource response', (done) => { |
| 80 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 81 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 82 | .subscribe(collection => { |
| 83 | event++; |
| 84 | if (event === 2) { |
| 85 | expect(collection[0].id).toEqual(queryData[0].id); |
| 86 | expect(collection[1].id).toEqual(queryData[1].id); |
| 87 | done(); |
| 88 | } |
| 89 | }); |
| 90 | $scope.$apply(); |
| 91 | httpBackend.flush(); |
| 92 | }); |
| 93 | |
| 94 | describe('when a web-socket event is received for that model', () => { |
| 95 | it('should update the collection', (done) => { |
| 96 | let event = 0; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 97 | service.query('sample') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 98 | .subscribe( |
| 99 | collection => { |
| 100 | event++; |
| 101 | if (event === 3) { |
| 102 | expect(collection[0].id).toEqual(queryData[0].id); |
| 103 | expect(collection[1].id).toEqual(queryData[1].id); |
| 104 | expect(collection[2].id).toEqual(3); |
| 105 | expect(collection[2].name).toEqual('baz'); |
| 106 | done(); |
| 107 | } |
| 108 | }, |
| 109 | err => { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 110 | done(err); |
| 111 | } |
| 112 | ); |
| 113 | window.setTimeout(() => { |
| 114 | WebSocket.next({ |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 115 | model: 'sample', |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 116 | msg: { |
| 117 | changed_fields: ['id'], |
| 118 | object: {id: 3, name: 'baz'}, |
| 119 | pk: 3 |
| 120 | } |
| 121 | }); |
| 122 | }, 1); |
| 123 | $scope.$apply(); |
| 124 | httpBackend.flush(); |
| 125 | }); |
| 126 | }); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 127 | |
| 128 | describe('when multiple stores are requested', () => { |
| 129 | |
| 130 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 131 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 132 | .respond([ |
| 133 | {first: 'foo'} |
| 134 | ]); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 135 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`) |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 136 | .respond([ |
| 137 | {second: 'foo'} |
| 138 | ]); |
| 139 | }); |
| 140 | it('should create different Subject', (done) => { |
| 141 | let fevent = 0; |
| 142 | let sevent = 0; |
| 143 | service.query('first') |
| 144 | .subscribe(first => { |
| 145 | fevent++; |
| 146 | if (fevent >= 2) { |
| 147 | service.query('second') |
| 148 | .subscribe(second => { |
| 149 | sevent++; |
| 150 | if (sevent === 2) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 151 | expect(first).not.toEqual(second); |
| 152 | done(); |
| 153 | } |
| 154 | }); |
| 155 | } |
| 156 | }); |
| 157 | $scope.$apply(); |
| 158 | httpBackend.flush(); |
| 159 | }); |
| 160 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 161 | }); |