blob: 9f4370c113780926e4df82e9007e77ddfdcf5f18 [file] [log] [blame]
Matteo Scandoloaa024ff2017-01-04 15:04:46 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4import {StoreHelpers, IStoreHelpersService} from './store.helpers';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -08005import {ModelRest} from '../rest/model.rest';
6import {BehaviorSubject} from 'rxjs';
7import {IWSEvent} from '../websocket/global';
Matteo Scandolo1c5905f2017-01-04 17:41:15 -08008import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -08009import {AuthService} from '../rest/auth.rest';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080010
11let service: IStoreHelpersService;
12let subject: BehaviorSubject<any>;
13let resource: ng.resource.IResourceClass<any>;
14let $resource: ng.resource.IResourceService;
15
16describe('The StoreHelpers service', () => {
17
18 beforeEach(() => {
19 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080020 .module('test', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080021 .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080022 .service('ModelRest', ModelRest) // NOTE evaluate mock
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080023 .service('StoreHelpers', StoreHelpers)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024 .service('AuthService', AuthService)
25 .value('AppConfig', {});
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080026
27 angular.mock.module('test');
28 });
29
30 beforeEach(angular.mock.inject((
31 StoreHelpers: IStoreHelpersService,
32 _$resource_: ng.resource.IResourceService
33 ) => {
34 $resource = _$resource_;
35 resource = $resource('/test');
36 service = StoreHelpers;
37 }));
38
39 it('should have an update collection method', () => {
40 expect(service.updateCollection).toBeDefined();
41 });
42
Matteo Scandolo04964232017-01-07 12:53:46 -080043
44 it('should convert a core model name in an URL', () => {
45 expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
Matteo Scandolo08464e52017-01-17 13:35:27 -080046 expect(service.urlFromCoreModel('Xos')).toBe('/core/xoses');
Matteo Scandolo1aee1982017-02-17 08:33:23 -080047 expect(service.urlFromCoreModel('SiteRole')).toBe('/core/siteroles');
48 expect(service.urlFromCoreModel('SliceRole')).toBe('/core/sliceroles');
49 expect(service.urlFromCoreModel('SlicePrivilege')).toBe('/core/sliceprivileges');
Matteo Scandolo04964232017-01-07 12:53:46 -080050 });
51
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080052 describe('when updating a collection', () => {
53
54 beforeEach(() => {
55 subject = new BehaviorSubject([
56 new resource({id: 1, name: 'test'})
57 ]);
58 });
59
60 it('should remove a model if it has been deleted', () => {
61 const event: IWSEvent = {
62 model: 'Test',
63 msg: {
64 object: {
65 id: 1,
66 name: 'test'
67 },
68 changed_fields: ['deleted']
69 }
70 };
71 service.updateCollection(event, subject);
72 expect(subject.value.length).toBe(0);
73 });
74
75 it('should update a model if it has been updated', () => {
76 const event: IWSEvent = {
77 model: 'Test',
78 msg: {
79 object: {
80 id: 1,
81 name: 'test-updated'
82 },
83 changed_fields: ['name']
84 }
85 };
86 service.updateCollection(event, subject);
87 expect(subject.value.length).toBe(1);
88 expect(subject.value[0].name).toBe('test-updated');
89 });
90
91 it('should add a model if it has been created', () => {
92 const event: IWSEvent = {
93 model: 'Test',
94 msg: {
95 object: {
96 id: 2,
97 name: 'another-test'
98 },
99 changed_fields: ['created']
100 }
101 };
102 service.updateCollection(event, subject);
103 expect(subject.value.length).toBe(2);
104 expect(subject.value[0].name).toBe('test');
105 expect(subject.value[1].name).toBe('another-test');
106 });
107
108 describe('when adding a model', () => {
109
110 beforeEach(() => {
111 const event: IWSEvent = {
112 model: 'Test',
113 msg: {
114 object: {
115 id: 2,
116 name: 'another-test'
117 },
118 changed_fields: ['created']
119 }
120 };
121 service.updateCollection(event, subject);
122 });
123
124 it('should create a resource', () => {
125 expect(subject.value[1].$save).toBeDefined();
126 expect(subject.value[1].$delete).toBeDefined();
127 });
128
129 xit('should automatically create the appropriate resource', () => {
130 // TODO test that the url of the resource is the correct one,
131 // use httpbackend and mock a call?? any faster way??
132 });
133 });
134 });
135
136});