blob: da389e1e6b59741df5bf8743529ff6dedd9bc3b0 [file] [log] [blame]
Matteo Scandolo7629cc42017-03-13 14:12:15 -07001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4import {IXosServiceGraphStore, XosServiceGraphStore} from './graph.store';
5import {Subject} from 'rxjs';
6import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
7import {IXosServiceGraph} from '../interfaces';
8import {XosServiceGraphExtender, IXosServiceGraphExtender} from './graph.extender';
9
10let service: IXosServiceGraphStore, extender: IXosServiceGraphExtender;
11
12const subjects = {
13 service: new Subject<any>(),
14 tenant: new Subject<any>(),
15 subscriber: new Subject<any>(),
Matteo Scandolo0e8a8422017-03-25 14:55:40 -070016 tenantroot: new Subject<any>(),
Matteo Scandolo7629cc42017-03-13 14:12:15 -070017 network: new Subject<any>(),
Matteo Scandolod4878532017-03-20 17:39:55 -070018 servicedependency: new Subject<any>()
Matteo Scandolo7629cc42017-03-13 14:12:15 -070019};
20
21// COARSE data
22const 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
35const 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
45const mockModelStore = {
46 query: (modelName: string) => {
47 return subjects[modelName.toLowerCase()].asObservable();
48 }
49};
50
51describe('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 Scandolod4878532017-03-20 17:39:55 -070074 subjects.servicedependency.next(coarseTenants);
Matteo Scandolo7629cc42017-03-13 14:12:15 -070075 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
Matteo Scandolobafd8d62017-03-29 23:23:00 -070094 xdescribe('when a reducer is registered', () => {
95 // NOTE the reducer appliance has been moved in the component
Matteo Scandolo7629cc42017-03-13 14:12:15 -070096 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});