From 219ddf056b63ae848226b92188af8eef9795dab3 Mon Sep 17 00:00:00 2001 From: Galen Warren Date: Sat, 8 Sep 2018 11:31:09 -0400 Subject: [PATCH] fix: fix addAttribute to ignore empty value --- src/firebase/realtime/__tests__/realtime.js | 91 --------------------- src/firebase/realtime/index.js | 1 - src/firebase/realtime/realtime.js | 26 ------ src/operators.js | 17 ++-- 4 files changed, 11 insertions(+), 124 deletions(-) delete mode 100644 src/firebase/realtime/__tests__/realtime.js delete mode 100644 src/firebase/realtime/index.js delete mode 100644 src/firebase/realtime/realtime.js diff --git a/src/firebase/realtime/__tests__/realtime.js b/src/firebase/realtime/__tests__/realtime.js deleted file mode 100644 index aab1fb5..0000000 --- a/src/firebase/realtime/__tests__/realtime.js +++ /dev/null @@ -1,91 +0,0 @@ -import Vue from 'vue'; -import { transformValue, firebaseRealtimeSource } from '../realtime'; - -describe('firebase realtime', () => { - describe('transformValue', () => { - it('should transform null to undefined', () => { - expect(transformValue(null)).toBe(undefined); - }); - - it('should not transform non-null values', () => { - expect(transformValue(1)).toBe(1); - expect(transformValue('test')).toBe('test'); - - const obj = { foo: 'bar ' }; - expect(transformValue(obj)).toBe(obj); - }); - }); - - describe('firebaseRealtimeSource', () => { - it('should properly deliver values', callback => { - const value = { - val() { - return 12; - }, - }; - - const firebase = { - database() { - return { - ref() { - return { - on(eventName, valueHandler) { - expect(eventName).toBe('value'); - Vue.nextTick(() => valueHandler(value)); - }, - off() { - expect(valueHandler).toHaveBeenCalledTimes(1); - expect(errorHandler).toHaveBeenCalledTimes(0); - callback(); - }, - }; - }, - }; - }, - }; - - const path = '/users/123'; - const value$ = firebaseRealtimeSource(firebase, path); - - const valueHandler = jest.fn(receivedValue => { - expect(receivedValue).toBe(value); - subscription.unsubscribe(); - }); - const errorHandler = jest.fn(); - - const subscription = value$.subscribe(valueHandler, errorHandler); - }); - - it('should properly handle errors', callback => { - const firebase = { - database() { - return { - ref() { - return { - on(eventName, valueHandler, errorHandler) { - expect(eventName).toBe('value'); - Vue.nextTick(() => errorHandler(new Error('failure'))); - }, - off() { - expect(valueHandler).toHaveBeenCalledTimes(0); - expect(errorHandler).toHaveBeenCalledTimes(1); - callback(); - }, - }; - }, - }; - }, - }; - - const path = '/users/123'; - const value$ = firebaseRealtimeSource(firebase, path); - - const valueHandler = jest.fn(); - const errorHandler = jest.fn(error => { - expect(error.message).toBe('failure'); - }); - - value$.subscribe(valueHandler, errorHandler); - }); - }); -}); diff --git a/src/firebase/realtime/index.js b/src/firebase/realtime/index.js deleted file mode 100644 index 15330d1..0000000 --- a/src/firebase/realtime/index.js +++ /dev/null @@ -1 +0,0 @@ -export { firebaseRealtimeSource } from './realtime'; diff --git a/src/firebase/realtime/realtime.js b/src/firebase/realtime/realtime.js deleted file mode 100644 index e008cd6..0000000 --- a/src/firebase/realtime/realtime.js +++ /dev/null @@ -1,26 +0,0 @@ -import { Observable } from 'rxjs'; -import log from 'picolog'; - -// in this database, a null value means the value doesn't exist, so translate -// to undefined for the purposes of this library -export function transformValue(value) { - return value === null ? undefined : value; -} - -export function firebaseRealtimeSource(firebase, path) { - return Observable.create(subscriber => { - const valueHandler = value => subscriber.next(transformValue(value.val())); - const errorHandler = error => subscriber.error(error); - - const database = firebase.database(); - const ref = database.ref(path); - - log.trace('firebaseRealtime:on', path); - ref.on('value', valueHandler, errorHandler); - - return () => { - log.trace('firebaseRealtime:off', path); - ref.off(); - }; - }); -} diff --git a/src/operators.js b/src/operators.js index a0a02c6..6894a19 100644 --- a/src/operators.js +++ b/src/operators.js @@ -1,4 +1,5 @@ import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; export function afterUnsubscribe(action, delay) { return source => @@ -16,11 +17,15 @@ export function afterUnsubscribe(action, delay) { export function addAttribute(key, attributeValue) { return source => Observable.create(subscriber => { - return source.subscribe( - value => - subscriber.next(Object.assign({}, value, { [key]: attributeValue })), - error => subscriber.error(error), - () => subscriber.complete() - ); + return source + .pipe( + map( + value => + value + ? Object.assign({}, value, { [key]: attributeValue }) + : value + ) + ) + .subscribe(subscriber); }); }