Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-ui-router'; |
| 4 | import {IXosServiceGraphStore, XosServiceGraphStore} from './graph.store'; |
| 5 | import {Subject} from 'rxjs'; |
| 6 | import {XosDebouncer} from '../../core/services/helpers/debounce.helper'; |
| 7 | import {IXosServiceGraph} from '../interfaces'; |
| 8 | import {XosServiceGraphExtender, IXosServiceGraphExtender} from './graph.extender'; |
| 9 | |
| 10 | let service: IXosServiceGraphStore, extender: IXosServiceGraphExtender; |
| 11 | |
| 12 | const subjects = { |
| 13 | service: new Subject<any>(), |
| 14 | tenant: new Subject<any>(), |
| 15 | subscriber: new Subject<any>(), |
| 16 | network: new Subject<any>(), |
| 17 | }; |
| 18 | |
| 19 | // COARSE data |
| 20 | const coarseServices = [ |
| 21 | { |
| 22 | id: 1, |
| 23 | name: 'Service A', |
| 24 | class_names: 'Service,PlCoreBase' |
| 25 | }, |
| 26 | { |
| 27 | id: 2, |
| 28 | name: 'Service B', |
| 29 | class_names: 'Service,PlCoreBase' |
| 30 | } |
| 31 | ]; |
| 32 | |
| 33 | const coarseTenants = [ |
| 34 | { |
| 35 | id: 1, |
| 36 | provider_service_id: 2, |
| 37 | subscriber_service_id: 1, |
| 38 | kind: 'coarse', |
| 39 | class_names: 'Tenant,PlCoreBase' |
| 40 | } |
| 41 | ]; |
| 42 | |
| 43 | const mockModelStore = { |
| 44 | query: (modelName: string) => { |
| 45 | return subjects[modelName.toLowerCase()].asObservable(); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | describe('The XosServiceGraphStore service', () => { |
| 50 | |
| 51 | beforeEach(() => { |
| 52 | angular.module('xosServiceGraphStore', []) |
| 53 | .service('XosServiceGraphStore', XosServiceGraphStore) |
| 54 | .value('XosModelStore', mockModelStore) |
| 55 | .service('XosServiceGraphExtender', XosServiceGraphExtender) |
| 56 | .service('XosDebouncer', XosDebouncer); |
| 57 | |
| 58 | angular.mock.module('xosServiceGraphStore'); |
| 59 | }); |
| 60 | |
| 61 | beforeEach(angular.mock.inject(( |
| 62 | XosServiceGraphStore: IXosServiceGraphStore, |
| 63 | XosServiceGraphExtender: IXosServiceGraphExtender |
| 64 | ) => { |
| 65 | service = XosServiceGraphStore; |
| 66 | extender = XosServiceGraphExtender; |
| 67 | })); |
| 68 | |
| 69 | describe('when subscribing for the COARSE service graph', () => { |
| 70 | beforeEach((done) => { |
| 71 | subjects.service.next(coarseServices); |
| 72 | subjects.tenant.next(coarseTenants); |
| 73 | setTimeout(done, 500); |
| 74 | }); |
| 75 | |
| 76 | it('should return an observer for the Coarse Service Graph', (done) => { |
| 77 | service.getCoarse() |
| 78 | .subscribe( |
| 79 | (res: IXosServiceGraph) => { |
| 80 | expect(res.nodes.length).toBe(2); |
| 81 | expect(res.nodes[0].d3Class).toBeUndefined(); |
| 82 | expect(res.links.length).toBe(1); |
| 83 | expect(res.links[0].d3Class).toBeUndefined(); |
| 84 | done(); |
| 85 | }, |
| 86 | (err) => { |
| 87 | done(err); |
| 88 | } |
| 89 | ); |
| 90 | }); |
| 91 | |
| 92 | describe('when a reducer is register', () => { |
| 93 | |
| 94 | beforeEach((done) => { |
| 95 | extender.register('coarse', 'test', (graph: IXosServiceGraph) => { |
| 96 | graph.nodes = graph.nodes.map(n => { |
| 97 | n.d3Class = `testNode`; |
| 98 | return n; |
| 99 | }); |
| 100 | |
| 101 | graph.links = graph.links.map(n => { |
| 102 | n.d3Class = `testLink`; |
| 103 | return n; |
| 104 | }); |
| 105 | |
| 106 | return graph; |
| 107 | }); |
| 108 | |
| 109 | // triggering another next cycle to apply the reducer |
| 110 | subjects.service.next(coarseServices); |
| 111 | subjects.tenant.next(coarseTenants); |
| 112 | setTimeout(done, 500); |
| 113 | }); |
| 114 | |
| 115 | it('should transform the result', (done) => { |
| 116 | service.getCoarse() |
| 117 | .subscribe( |
| 118 | (res: IXosServiceGraph) => { |
| 119 | expect(res.nodes.length).toBe(2); |
| 120 | expect(res.nodes[0].d3Class).toEqual('testNode'); |
| 121 | expect(res.links.length).toBe(1); |
| 122 | expect(res.links[0].d3Class).toEqual('testLink'); |
| 123 | done(); |
| 124 | }, |
| 125 | (err) => { |
| 126 | done(err); |
| 127 | } |
| 128 | ); |
| 129 | }); |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | describe('when subscribing for the Fine-grained service graph', () => { |
| 134 | xit('should have a test', () => { |
| 135 | expect(true).toBeTruthy(); |
| 136 | }); |
| 137 | }); |
| 138 | }); |