Skip to content

Commit 8ecca95

Browse files
authored
Merge pull request #36 from dmurvihill/add-user-json
Add user json
2 parents 746646c + 35f1094 commit 8ecca95

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/user.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function MockFirebaseUser(ref, data) {
1919
this.emailVerified = !!data.emailVerified;
2020
this.isAnonymous = !!data.isAnonymous;
2121
this.metadata = data.metadata;
22-
this.providerData = data.providerData;
22+
this.providerData = data.providerData || [];
2323
this.providerId = data.providerId;
2424
this.refreshToken = data.refreshToken;
2525
}
@@ -123,6 +123,26 @@ MockFirebaseUser.prototype.getIdToken = function (forceRefresh) {
123123
});
124124
};
125125

126+
MockFirebaseUser.prototype.toJSON = function() {
127+
const json = {
128+
uid: this.uid,
129+
email: this.email,
130+
emailVerified: this.emailVerified,
131+
displayName: this.displayName,
132+
photoURL: this.photoURL,
133+
phoneNumber: this.phoneNumber,
134+
};
135+
if (this.metadata) {
136+
json.createdAt = this.metadata.createdAt;
137+
json.lastLoginAt = this.metadata.lastLoginAt;
138+
}
139+
json.providerData = [];
140+
for (const entry of this.providerData) {
141+
json.providerData.push(entry.toJSON());
142+
}
143+
return json;
144+
};
145+
126146
MockFirebaseUser.prototype.getIdTokenResult = function (forceRefresh) {
127147
if (forceRefresh) {
128148
this._refreshIdToken();

test/unit/user.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,48 @@ describe('User', function() {
463463
return expect(user._refreshIdToken()).not.to.be.rejected;
464464
});
465465
});
466+
467+
describe('#toJSON', () => {
468+
describe('most fields', () => {
469+
it('should be the same', () => {
470+
const user = new User(auth, {});
471+
const json = user.toJSON();
472+
[
473+
'uid',
474+
'email',
475+
'emailVerified',
476+
'displayName',
477+
'photoURL',
478+
'phoneNumber',
479+
'providerData',
480+
].forEach(k => expect(json[k]).to.deep.equal(user[k]));
481+
});
482+
});
483+
484+
describe('.metadata', () => {
485+
it('keys should be missing if omitted', () => {
486+
const user = new User(auth, {});
487+
expect(user.toJSON()).not.to.haveOwnProperty('lastLoginAt');
488+
expect(user.toJSON()).not.to.haveOwnProperty('createdAt');
489+
});
490+
491+
it('should populate to lastLogin if present', () => {
492+
const metadata = {
493+
lastLoginAt: new Date(11).getTime().toString(10),
494+
};
495+
const user = new User(auth, { metadata: metadata, });
496+
expect(user.toJSON().lastLoginAt).to.equal(metadata.lastLoginAt);
497+
});
498+
499+
it('should populate to createdAt if present', () => {
500+
const metadata = {
501+
createdAt: new Date(12).getTime().toString(10),
502+
};
503+
const user = new User(auth, { metadata: metadata, });
504+
expect(user.toJSON().createdAt).to.equal(metadata.createdAt);
505+
});
506+
});
507+
});
466508
});
467509

468510
function randomTimestamp() {

0 commit comments

Comments
 (0)