Added slices
Change-Id: I9dfaa9348fa82da844a04c0c2a58ce07e9fa3a28
diff --git a/src/app/services/helpers/store.service.spec.ts b/src/app/services/helpers/store.service.spec.ts
new file mode 100644
index 0000000..d70d649
--- /dev/null
+++ b/src/app/services/helpers/store.service.spec.ts
@@ -0,0 +1,88 @@
+
+import {ObservableCollectionHandler} from './store.service';
+import {IWSEvent} from '../../interfaces/ws.interface';
+import {BehaviorSubject} from 'rxjs';
+
+describe('Service: Observable Collection Handler', () => {
+
+ let subject: BehaviorSubject<any>;
+ let observable;
+
+ beforeEach(() => {
+ subject = new BehaviorSubject([]);
+ observable = subject.asObservable();
+ });
+
+ it('Should have an update method', () => {
+ expect(ObservableCollectionHandler.update).toBeDefined();
+ });
+
+ it('should add an element to the observable', (done) => {
+ const event: IWSEvent = {
+ model: 'Test',
+ msg: {
+ pk: 1,
+ changed_fields: [],
+ object: {id: 1, foo: 'bar'}
+ }
+ };
+
+ ObservableCollectionHandler.update(event, subject);
+
+ subject.subscribe(
+ (collection: any[]) => {
+ expect(collection.length).toBe(1);
+ expect(collection[0].foo).toEqual('bar');
+ done();
+ }
+ );
+ });
+
+ describe('when the subject already have content', () => {
+ beforeEach(() => {
+ subject.next([{id: 1, foo: 'bar'}, {id: 2, foo: 'baz'}]);
+ });
+
+ it('should update an element', (done) => {
+ const event: IWSEvent = {
+ model: 'Test',
+ msg: {
+ pk: 1,
+ changed_fields: [],
+ object: {id: 1, foo: 'updated'}
+ }
+ };
+
+ ObservableCollectionHandler.update(event, subject);
+
+ subject.subscribe(
+ (collection: any[]) => {
+ expect(collection.length).toBe(2);
+ expect(collection[0].foo).toEqual('updated');
+ done();
+ }
+ );
+ });
+
+ it('should delete an element', (done) => {
+ const event: IWSEvent = {
+ model: 'Test',
+ msg: {
+ pk: 1,
+ changed_fields: ['deleted'],
+ object: {id: 1, foo: 'deleted'}
+ }
+ };
+
+ ObservableCollectionHandler.update(event, subject);
+
+ subject.subscribe(
+ (collection: any[]) => {
+ expect(collection.length).toBe(1);
+ expect(collection[0].foo).toEqual('baz');
+ done();
+ }
+ );
+ });
+ });
+});
diff --git a/src/app/services/helpers/store.service.ts b/src/app/services/helpers/store.service.ts
new file mode 100644
index 0000000..80e8e05
--- /dev/null
+++ b/src/app/services/helpers/store.service.ts
@@ -0,0 +1,40 @@
+import {Injectable} from '@angular/core';
+import {IWSEvent} from '../../interfaces/ws.interface';
+import {BehaviorSubject} from 'rxjs';
+import * as _ from 'lodash';
+
+/**
+ * @whatItDoes Update a BehaviorSubject after receiving an event from an Observable
+ * @stable
+ */
+
+@Injectable()
+export class ObservableCollectionHandler {
+
+ static update(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
+ const collection: any[] = subject.value;
+
+ const index: number = _.findIndex(collection, (i) => {
+ return i.id === event.msg.object.id;
+ });
+ const exist: boolean = index > -1;
+ const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted');
+
+ // remove
+ if (exist && isDeleted) {
+ _.remove(collection, {id: event.msg.object.id});
+ }
+ // Replace item at index using native splice
+ else if (exist && !isDeleted) {
+ collection.splice(index, 1, event.msg.object);
+ }
+ // if the element is not deleted add it
+ else if (!exist && !isDeleted) {
+ collection.push(event.msg.object);
+ }
+
+ subject.next(collection);
+
+ return subject;
+ }
+}