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