blob: b2085e30ba9e729055cb97084f2c2e4e2d5c252d [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';
5import {ModelHelpers} from './model.helpers';
6import {ModelRest} from '../rest/model.rest';
7import {BehaviorSubject} from 'rxjs';
8import {IWSEvent} from '../websocket/global';
9
10let service: IStoreHelpersService;
11let subject: BehaviorSubject<any>;
12let resource: ng.resource.IResourceClass<any>;
13let $resource: ng.resource.IResourceService;
14
15describe('The StoreHelpers service', () => {
16
17 beforeEach(() => {
18 angular
19 .module('test', ['ngResource'])
20 .service('ModelHelpers', ModelHelpers) // NOTE evaluate mock
21 .service('ModelRest', ModelRest) // NOTE evaluate mock
22 .service('StoreHelpers', StoreHelpers);
23
24 angular.mock.module('test');
25 });
26
27 beforeEach(angular.mock.inject((
28 StoreHelpers: IStoreHelpersService,
29 _$resource_: ng.resource.IResourceService
30 ) => {
31 $resource = _$resource_;
32 resource = $resource('/test');
33 service = StoreHelpers;
34 }));
35
36 it('should have an update collection method', () => {
37 expect(service.updateCollection).toBeDefined();
38 });
39
40 describe('when updating a collection', () => {
41
42 beforeEach(() => {
43 subject = new BehaviorSubject([
44 new resource({id: 1, name: 'test'})
45 ]);
46 });
47
48 it('should remove a model if it has been deleted', () => {
49 const event: IWSEvent = {
50 model: 'Test',
51 msg: {
52 object: {
53 id: 1,
54 name: 'test'
55 },
56 changed_fields: ['deleted']
57 }
58 };
59 service.updateCollection(event, subject);
60 expect(subject.value.length).toBe(0);
61 });
62
63 it('should update a model if it has been updated', () => {
64 const event: IWSEvent = {
65 model: 'Test',
66 msg: {
67 object: {
68 id: 1,
69 name: 'test-updated'
70 },
71 changed_fields: ['name']
72 }
73 };
74 service.updateCollection(event, subject);
75 expect(subject.value.length).toBe(1);
76 expect(subject.value[0].name).toBe('test-updated');
77 });
78
79 it('should add a model if it has been created', () => {
80 const event: IWSEvent = {
81 model: 'Test',
82 msg: {
83 object: {
84 id: 2,
85 name: 'another-test'
86 },
87 changed_fields: ['created']
88 }
89 };
90 service.updateCollection(event, subject);
91 expect(subject.value.length).toBe(2);
92 expect(subject.value[0].name).toBe('test');
93 expect(subject.value[1].name).toBe('another-test');
94 });
95
96 describe('when adding a model', () => {
97
98 beforeEach(() => {
99 const event: IWSEvent = {
100 model: 'Test',
101 msg: {
102 object: {
103 id: 2,
104 name: 'another-test'
105 },
106 changed_fields: ['created']
107 }
108 };
109 service.updateCollection(event, subject);
110 });
111
112 it('should create a resource', () => {
113 expect(subject.value[1].$save).toBeDefined();
114 expect(subject.value[1].$delete).toBeDefined();
115 });
116
117 xit('should automatically create the appropriate resource', () => {
118 // TODO test that the url of the resource is the correct one,
119 // use httpbackend and mock a call?? any faster way??
120 });
121 });
122 });
123
124});