Skip to content

fix(utils): Make object $key and $exists properties non-enumerable #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/database/firebase_object_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ describe('FirebaseObjectFactory', () => {


it('should emit unwrapped data by default', (done: any) => {
ref.set({ data: 'bar' }, () => {
const fixtureData = { data: 'bar' };
ref.set(fixtureData, () => {
subscription = observable.subscribe(unwrapped => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observable subscription shouldn't be needed here. Can you remove it and just use the inner code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davideast I'm not sure I follow. How can we assert the object properties after ref.set(), other than by subscribing to the object observable?

If it'll take too long to explain, please feel free to just tell me what to do here specifically, and I'm sure I'll figure it out as I implement the changes. 😎

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my brain was malfunctioning. I looked at this too early in the morning. I forgot that there's a FirebaseObjectObservable variable in the outer scope. All is good!

if (!unwrapped) return;
const expectedObject = { $key: ref.key, data: 'bar' };
expect(unwrapped.$key).toEqual(expectedObject.$key);
expect(unwrapped.data).toEqual(expectedObject.data);
expect(unwrapped.$key).toEqual(ref.key);
expect(unwrapped).toEqual(fixtureData);
expect(unwrapped.$exists()).toEqual(true);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion src/database/firebase_object_observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('FirebaseObjectObservable', () => {
});
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
app = firebaseApp;
ref = firebase.database().ref()
ref = firebase.database().ref();
O = new FirebaseObjectObservable((observer:Observer<any>) => {
}, ref);
})();
Expand Down
14 changes: 10 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrapp
$value: unwrapped
};
}
unwrapped.$key = snapshot.ref.key;
unwrapped.$exists = () => {
return snapshot.exists();
};
Object.defineProperty(unwrapped, '$key', {
value: snapshot.ref.key,
enumerable: false
});
Object.defineProperty(unwrapped, '$exists', {
value: () => {
return snapshot.exists();
},
enumerable: false
});
return unwrapped;
}

Expand Down