Skip to content

Commit f67aab1

Browse files
authored
feat(utils): Add $exists method to AFUnwrappedSnapshot (#471)
* feat(utils): Add method to AFUnwrappedSnapshot * fix(tests): Fix failing tests for * fix(tests): Dont throw a fit
1 parent 986685a commit f67aab1

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

src/auth/auth_backend.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export enum AuthMethods {
3737
export interface AuthConfiguration {
3838
method?: AuthMethods;
3939
provider?: AuthProviders;
40+
scope?: string[];
4041
}
4142

4243
export interface FirebaseAuthState {

src/database/firebase_list_factory.spec.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import {
1313
defaultFirebase,
1414
FirebaseApp,
1515
FirebaseAppConfig,
16-
AngularFire
16+
AngularFire,
1717
} from '../angularfire2';
1818
import {
1919
addProviders,
2020
inject
2121
} from '@angular/core/testing';
2222
import * as utils from '../utils';
23-
import {Query} from '../interfaces';
24-
import {Subscription, Observable, Subject} from 'rxjs';
23+
import { Query, AFUnwrappedDataSnapshot } from '../interfaces';
24+
import { Subscription, Observable, Subject } from 'rxjs';
2525
import 'rxjs/add/operator/do';
2626
import 'rxjs/add/operator/skip';
2727
import 'rxjs/add/operator/take';
@@ -526,26 +526,26 @@ describe('FirebaseListFactory', () => {
526526

527527
it('should return an object value with a $key property', () => {
528528
const unwrapped = utils.unwrapMapFn(snapshot as firebase.database.DataSnapshot);
529-
expect(unwrapped).toEqual({
530-
$key: 'key',
531-
unwrapped: true
532-
});
529+
expect(unwrapped.$key).toEqual(snapshot.ref.key);
533530
});
534531

535-
536532
it('should return an object value with a $value property if value is scalar', () => {
537-
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => 5 }) as firebase.database.DataSnapshot)).toEqual({
538-
$key: 'key',
539-
$value: 5
540-
});
541-
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => false }) as firebase.database.DataSnapshot)).toEqual({
542-
$key: 'key',
543-
$value: false
544-
});
545-
expect(utils.unwrapMapFn(Object.assign(snapshot, { val: () => 'lol' }) as firebase.database.DataSnapshot)).toEqual({
546-
$key: 'key',
547-
$value: 'lol'
548-
});
533+
const existsFn = () => { return true; }
534+
const unwrappedValue5 = utils.unwrapMapFn(Object.assign(snapshot, { val: () => 5, exists: existsFn }) as firebase.database.DataSnapshot);
535+
const unwrappedValueFalse = utils.unwrapMapFn(Object.assign(snapshot, { val: () => false, exists: existsFn }) as firebase.database.DataSnapshot);
536+
const unwrappedValueLol = utils.unwrapMapFn(Object.assign(snapshot, { val: () => 'lol', exists: existsFn }) as firebase.database.DataSnapshot);
537+
538+
expect(unwrappedValue5.$key).toEqual('key');
539+
expect(unwrappedValue5.$value).toEqual(5);
540+
expect(unwrappedValue5.$exists()).toEqual(true);
541+
542+
expect(unwrappedValueFalse.$key).toEqual('key');
543+
expect(unwrappedValueFalse.$value).toEqual(false);
544+
expect(unwrappedValueFalse.$exists()).toEqual(true);
545+
546+
expect(unwrappedValueLol.$key).toEqual('key');
547+
expect(unwrappedValueLol.$value).toEqual('lol');
548+
expect(unwrappedValueLol.$exists()).toEqual(true);
549549
});
550550
});
551551

src/database/firebase_object_factory.spec.ts

+25-8
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,50 @@ describe('FirebaseObjectFactory', () => {
7171

7272

7373
it('should emit a null value if no value is present when subscribed', (done: any) => {
74-
subscription = observable.subscribe(val => {
75-
expect(val).toEqual({ $key: (<any>observable)._ref.key, $value: null });
74+
subscription = observable.subscribe(unwrapped => {
75+
const expectedObject = { $key: (<any>observable)._ref.key, $value: null };
76+
expect(unwrapped.$key).toEqual(expectedObject.$key);
77+
expect(unwrapped.$value).toEqual(expectedObject.$value);
78+
expect(unwrapped.$exists()).toEqual(false);
7679
done();
7780
});
7881
});
7982

8083

8184
it('should emit unwrapped data by default', (done: any) => {
82-
ref.set({ unwrapped: 'bar' }, () => {
83-
subscription = observable.subscribe(val => {
84-
if (!val) return;
85-
expect(val).toEqual({ $key: ref.key, unwrapped: 'bar' });
85+
ref.set({ data: 'bar' }, () => {
86+
subscription = observable.subscribe(unwrapped => {
87+
if (!unwrapped) return;
88+
const expectedObject = { $key: ref.key, data: 'bar' };
89+
expect(unwrapped.$key).toEqual(expectedObject.$key);
90+
expect(unwrapped.data).toEqual(expectedObject.data);
91+
expect(unwrapped.$exists()).toEqual(true);
8692
done();
8793
});
8894
});
8995
});
9096

91-
it('should emit unwrapped data with a $value property for primitive values', (done: any) => {
97+
it('should emit unwrapped data with $ properties for primitive values', (done: any) => {
9298
ref.set('fiiiireeee', () => {
9399
subscription = observable.subscribe(val => {
94100
if (!val) return;
95-
expect(val).toEqual({ $key: ref.key, $value: 'fiiiireeee' });
101+
expect(val.$key).toEqual(ref.key);
102+
expect(val.$value).toEqual('fiiiireeee');
103+
expect(val.$exists()).toEqual(true);
96104
done();
97105
});
98106
});
99107
});
100108

109+
it('should emit null for $ properties for primitive values', (done: any) => {
110+
subscription = observable.subscribe(val => {
111+
if (!val) return;
112+
expect(val.$key).toEqual(ref.key);
113+
expect(val.$value).toEqual(null);
114+
expect(val.$exists()).toEqual(false);
115+
done();
116+
});
117+
});
101118

102119
it('should emit snapshots if preserveSnapshot option is true', (done: any) => {
103120
observable = FirebaseObjectFactory(`${rootFirebase}/questions/${i}`, { preserveSnapshot: true });

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface FirebaseOperationCases {
1717
export interface AFUnwrappedDataSnapshot {
1818
$key: string;
1919
$value?: string | number | boolean;
20+
$exists: () => boolean;
2021
}
2122

2223
export interface Query {

src/utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ export interface CheckUrlRef {
3838
isQuery?: () => any;
3939
}
4040

41+
/**
42+
* 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.
43+
* @param {DataSnapshot} snapshot - The snapshot to unwrap
44+
* @return AFUnwrappedDataSnapshot
45+
* @example
46+
* unwrapMapFn(snapshot) => { name: 'David', $key: 'david', $exists: Function }
47+
*/
4148
export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrappedDataSnapshot {
4249
var unwrapped = isPresent(snapshot.val()) ? snapshot.val() : { $value: null };
4350
if ((/string|number|boolean/).test(typeof unwrapped)) {
@@ -46,6 +53,9 @@ export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrapp
4653
};
4754
}
4855
unwrapped.$key = snapshot.ref.key;
56+
unwrapped.$exists = () => {
57+
return snapshot.exists();
58+
};
4959
return unwrapped;
5060
}
5161

0 commit comments

Comments
 (0)