blob: a4778171ea3f2c4d81a981c43bf5b8f73c53225a [file] [log] [blame]
Matteo Scandolo5b0dcc02016-12-08 11:00:07 -08001///<reference path="../../typings/globals/jasmine/index.d.ts"/>
Matteo Scandolo40f8fa92016-12-07 09:21:35 -08002
Matteo Scandolo5b0dcc02016-12-08 11:00:07 -08003import {ObservableCollectionHandler} from '../../src/app/services/helpers/store.service';
4import {IWSEvent} from '../../src/app/interfaces/ws.interface';
Matteo Scandolo40f8fa92016-12-07 09:21:35 -08005import {BehaviorSubject} from 'rxjs';
6
7describe('Service: Observable Collection Handler', () => {
8
9 let subject: BehaviorSubject<any>;
10 let observable;
11
12 beforeEach(() => {
13 subject = new BehaviorSubject([]);
14 observable = subject.asObservable();
15 });
16
17 it('Should have an update method', () => {
18 expect(ObservableCollectionHandler.update).toBeDefined();
19 });
20
21 it('should add an element to the observable', (done) => {
22 const event: IWSEvent = {
23 model: 'Test',
24 msg: {
25 pk: 1,
26 changed_fields: [],
27 object: {id: 1, foo: 'bar'}
28 }
29 };
30
31 ObservableCollectionHandler.update(event, subject);
32
33 subject.subscribe(
34 (collection: any[]) => {
35 expect(collection.length).toBe(1);
36 expect(collection[0].foo).toEqual('bar');
37 done();
38 }
39 );
40 });
41
42 describe('when the subject already have content', () => {
43 beforeEach(() => {
44 subject.next([{id: 1, foo: 'bar'}, {id: 2, foo: 'baz'}]);
45 });
46
47 it('should update an element', (done) => {
48 const event: IWSEvent = {
49 model: 'Test',
50 msg: {
51 pk: 1,
52 changed_fields: [],
53 object: {id: 1, foo: 'updated'}
54 }
55 };
56
57 ObservableCollectionHandler.update(event, subject);
58
59 subject.subscribe(
60 (collection: any[]) => {
61 expect(collection.length).toBe(2);
62 expect(collection[0].foo).toEqual('updated');
63 done();
64 }
65 );
66 });
67
68 it('should delete an element', (done) => {
69 const event: IWSEvent = {
70 model: 'Test',
71 msg: {
72 pk: 1,
73 changed_fields: ['deleted'],
74 object: {id: 1, foo: 'deleted'}
75 }
76 };
77
78 ObservableCollectionHandler.update(event, subject);
79
80 subject.subscribe(
81 (collection: any[]) => {
82 expect(collection.length).toBe(1);
83 expect(collection[0].foo).toEqual('baz');
84 done();
85 }
86 );
87 });
88 });
89});