Skip to content
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
22 changes: 21 additions & 1 deletion src/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function MockFirebaseUser(ref, data) {
this.emailVerified = !!data.emailVerified;
this.isAnonymous = !!data.isAnonymous;
this.metadata = data.metadata;
this.providerData = data.providerData;
this.providerData = data.providerData || [];
this.providerId = data.providerId;
this.refreshToken = data.refreshToken;
}
Expand Down Expand Up @@ -123,6 +123,26 @@ MockFirebaseUser.prototype.getIdToken = function (forceRefresh) {
});
};

MockFirebaseUser.prototype.toJSON = function() {
Copy link
Owner Author

Choose a reason for hiding this comment

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

This definitely would be useful. Anyone willing to write us some tests?

const json = {
uid: this.uid,
email: this.email,
emailVerified: this.emailVerified,
displayName: this.displayName,
photoURL: this.photoURL,
phoneNumber: this.phoneNumber,
};
if (this.metadata) {
json.createdAt = this.metadata.createdAt;
json.lastLoginAt = this.metadata.lastLoginAt;
}
json.providerData = [];
for (const entry of this.providerData) {
json.providerData.push(entry.toJSON());
}
return json;
};

MockFirebaseUser.prototype.getIdTokenResult = function (forceRefresh) {
if (forceRefresh) {
this._refreshIdToken();
Expand Down
42 changes: 42 additions & 0 deletions test/unit/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,48 @@ describe('User', function() {
return expect(user._refreshIdToken()).not.to.be.rejected;
});
});

describe('#toJSON', () => {
describe('most fields', () => {
it('should be the same', () => {
const user = new User(auth, {});
const json = user.toJSON();
[
'uid',
'email',
'emailVerified',
'displayName',
'photoURL',
'phoneNumber',
'providerData',
].forEach(k => expect(json[k]).to.deep.equal(user[k]));
});
});

describe('.metadata', () => {
it('keys should be missing if omitted', () => {
const user = new User(auth, {});
expect(user.toJSON()).not.to.haveOwnProperty('lastLoginAt');
expect(user.toJSON()).not.to.haveOwnProperty('createdAt');
});

it('should populate to lastLogin if present', () => {
const metadata = {
lastLoginAt: new Date(11).getTime().toString(10),
};
const user = new User(auth, { metadata: metadata, });
expect(user.toJSON().lastLoginAt).to.equal(metadata.lastLoginAt);
});

it('should populate to createdAt if present', () => {
const metadata = {
createdAt: new Date(12).getTime().toString(10),
};
const user = new User(auth, { metadata: metadata, });
expect(user.toJSON().createdAt).to.equal(metadata.createdAt);
});
});
});
});

function randomTimestamp() {
Expand Down