Skip to content

Commit

Permalink
feat(utils): Add $exists method to AFUnwrappedSnapshot (#471)
Browse files Browse the repository at this point in the history
* feat(utils): Add  method to AFUnwrappedSnapshot

* fix(tests): Fix failing tests for

* fix(tests): Dont throw a fit
  • Loading branch information
davideast authored Aug 24, 2016
1 parent 986685a commit f67aab1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/auth/auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export enum AuthMethods {
export interface AuthConfiguration {
method?: AuthMethods;
provider?: AuthProviders;
scope?: string[];
}

export interface FirebaseAuthState {
Expand Down
40 changes: 20 additions & 20 deletions src/database/firebase_list_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
defaultFirebase,
FirebaseApp,
FirebaseAppConfig,
AngularFire
AngularFire,
} from '../angularfire2';
import {
addProviders,
inject
} from '@angular/core/testing';
import * as utils from '../utils';
import {Query} from '../interfaces';
import {Subscription, Observable, Subject} from 'rxjs';
import { Query, AFUnwrappedDataSnapshot } from '../interfaces';
import { Subscription, Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/skip';
import 'rxjs/add/operator/take';
Expand Down Expand Up @@ -526,26 +526,26 @@ describe('FirebaseListFactory', () => {

it('should return an object value with a $key property', () => {
const unwrapped = utils.unwrapMapFn(snapshot as firebase.database.DataSnapshot);
expect(unwrapped).toEqual({
$key: 'key',
unwrapped: true
});
expect(unwrapped.$key).toEqual(snapshot.ref.key);
});


it('should return an object value with a $value property if value is scalar', () => {
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => 5 }) as firebase.database.DataSnapshot)).toEqual({
$key: 'key',
$value: 5
});
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => false }) as firebase.database.DataSnapshot)).toEqual({
$key: 'key',
$value: false
});
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => 'lol' }) as firebase.database.DataSnapshot)).toEqual({
$key: 'key',
$value: 'lol'
});
const existsFn = () => { return true; }
const unwrappedValue5 = utils.unwrapMapFn(Object.assign(snapshot, { val: () => 5, exists: existsFn }) as firebase.database.DataSnapshot);
const unwrappedValueFalse = utils.unwrapMapFn(Object.assign(snapshot, { val: () => false, exists: existsFn }) as firebase.database.DataSnapshot);
const unwrappedValueLol = utils.unwrapMapFn(Object.assign(snapshot, { val: () => 'lol', exists: existsFn }) as firebase.database.DataSnapshot);

expect(unwrappedValue5.$key).toEqual('key');
expect(unwrappedValue5.$value).toEqual(5);
expect(unwrappedValue5.$exists()).toEqual(true);

expect(unwrappedValueFalse.$key).toEqual('key');
expect(unwrappedValueFalse.$value).toEqual(false);
expect(unwrappedValueFalse.$exists()).toEqual(true);

expect(unwrappedValueLol.$key).toEqual('key');
expect(unwrappedValueLol.$value).toEqual('lol');
expect(unwrappedValueLol.$exists()).toEqual(true);
});
});

Expand Down
33 changes: 25 additions & 8 deletions src/database/firebase_object_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,50 @@ describe('FirebaseObjectFactory', () => {


it('should emit a null value if no value is present when subscribed', (done: any) => {
subscription = observable.subscribe(val => {
expect(val).toEqual({ $key: (<any>observable)._ref.key, $value: null });
subscription = observable.subscribe(unwrapped => {
const expectedObject = { $key: (<any>observable)._ref.key, $value: null };
expect(unwrapped.$key).toEqual(expectedObject.$key);
expect(unwrapped.$value).toEqual(expectedObject.$value);
expect(unwrapped.$exists()).toEqual(false);
done();
});
});


it('should emit unwrapped data by default', (done: any) => {
ref.set({ unwrapped: 'bar' }, () => {
subscription = observable.subscribe(val => {
if (!val) return;
expect(val).toEqual({ $key: ref.key, unwrapped: 'bar' });
ref.set({ data: 'bar' }, () => {
subscription = observable.subscribe(unwrapped => {
if (!unwrapped) return;
const expectedObject = { $key: ref.key, data: 'bar' };
expect(unwrapped.$key).toEqual(expectedObject.$key);
expect(unwrapped.data).toEqual(expectedObject.data);
expect(unwrapped.$exists()).toEqual(true);
done();
});
});
});

it('should emit unwrapped data with a $value property for primitive values', (done: any) => {
it('should emit unwrapped data with $ properties for primitive values', (done: any) => {
ref.set('fiiiireeee', () => {
subscription = observable.subscribe(val => {
if (!val) return;
expect(val).toEqual({ $key: ref.key, $value: 'fiiiireeee' });
expect(val.$key).toEqual(ref.key);
expect(val.$value).toEqual('fiiiireeee');
expect(val.$exists()).toEqual(true);
done();
});
});
});

it('should emit null for $ properties for primitive values', (done: any) => {
subscription = observable.subscribe(val => {
if (!val) return;
expect(val.$key).toEqual(ref.key);
expect(val.$value).toEqual(null);
expect(val.$exists()).toEqual(false);
done();
});
});

it('should emit snapshots if preserveSnapshot option is true', (done: any) => {
observable = FirebaseObjectFactory(`${rootFirebase}/questions/${i}`, { preserveSnapshot: true });
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface FirebaseOperationCases {
export interface AFUnwrappedDataSnapshot {
$key: string;
$value?: string | number | boolean;
$exists: () => boolean;
}

export interface Query {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export interface CheckUrlRef {
isQuery?: () => any;
}

/**
* Unwraps the data returned in the DataSnapshot. Exposes the DataSnapshot key and exists methods through the $key and $exists properties respectively. If the value is primitive, it is unwrapped using a $value property. The $ properies mean they cannot be saved in the Database as those characters are invalid.
* @param {DataSnapshot} snapshot - The snapshot to unwrap
* @return AFUnwrappedDataSnapshot
* @example
* unwrapMapFn(snapshot) => { name: 'David', $key: 'david', $exists: Function }
*/
export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrappedDataSnapshot {
var unwrapped = isPresent(snapshot.val()) ? snapshot.val() : { $value: null };
if ((/string|number|boolean/).test(typeof unwrapped)) {
Expand All @@ -46,6 +53,9 @@ export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrapp
};
}
unwrapped.$key = snapshot.ref.key;
unwrapped.$exists = () => {
return snapshot.exists();
};
return unwrapped;
}

Expand Down

0 comments on commit f67aab1

Please sign in to comment.