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