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