mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-22 20:51:29 +10:00
FirebaseStub: added auth User class
This commit is contained in:
@ -13,7 +13,7 @@ test('returns anonymous user 1 when signing in anonymously', async () => {
|
|||||||
const user = await FirebaseStub.auth().signInAnonymously();
|
const user = await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
expect(user).toBeTruthy();
|
||||||
expect(user).toEqual(AuthConstants.anonymousUser1);
|
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('calls onAuthStateChanged observer with anonymous user 1 when signing in anonymously', async () => {
|
test('calls onAuthStateChanged observer with anonymous user 1 when signing in anonymously', async () => {
|
||||||
@ -31,7 +31,7 @@ test('calls onAuthStateChanged observer with anonymous user 1 when signing in an
|
|||||||
await FirebaseStub.auth().signInAnonymously();
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
expect(user).toBeTruthy();
|
||||||
expect(user).toEqual(AuthConstants.anonymousUser1);
|
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
||||||
expect(error).toBeNull();
|
expect(error).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import Constants from '../constants/auth';
|
import Constants from '../constants/auth';
|
||||||
|
import User from './user';
|
||||||
import { delay } from '../../../src/utils/index';
|
import { delay } from '../../../src/utils/index';
|
||||||
|
|
||||||
const singleton = Symbol('');
|
const singleton = Symbol('');
|
||||||
@ -14,6 +15,7 @@ class Auth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._uuid = uuidv4();
|
this._uuid = uuidv4();
|
||||||
|
this._currentUser = null;
|
||||||
this._onAuthStateChangedObservers = [];
|
this._onAuthStateChangedObservers = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +27,10 @@ class Auth {
|
|||||||
return this[singleton];
|
return this[singleton];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get currentUser() {
|
||||||
|
return this._currentUser;
|
||||||
|
}
|
||||||
|
|
||||||
get uuid() {
|
get uuid() {
|
||||||
return this._uuid;
|
return this._uuid;
|
||||||
}
|
}
|
||||||
@ -46,11 +52,21 @@ class Auth {
|
|||||||
async signInAnonymously() {
|
async signInAnonymously() {
|
||||||
const user = Constants.anonymousUser1;
|
const user = Constants.anonymousUser1;
|
||||||
|
|
||||||
|
this._currentUser = new User(
|
||||||
|
user.displayName,
|
||||||
|
user.email,
|
||||||
|
user.isAnonymous,
|
||||||
|
user.uid,
|
||||||
|
async () => {},
|
||||||
|
);
|
||||||
|
|
||||||
await delay(Constants.defaultDelayInMilliseconds);
|
await delay(Constants.defaultDelayInMilliseconds);
|
||||||
|
|
||||||
this.onAuthStateChangedObservers.forEach((observer) => observer(user));
|
this.onAuthStateChangedObservers.forEach((observer) =>
|
||||||
|
observer(this._currentUser),
|
||||||
|
);
|
||||||
|
|
||||||
return user;
|
return this._currentUser;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
60
__mocks__/gatsby-plugin-firebase/auth/user.js
Normal file
60
__mocks__/gatsby-plugin-firebase/auth/user.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* eslint-disable no-underscore-dangle */
|
||||||
|
import Constants from '../constants/auth';
|
||||||
|
import { delay } from '../../../src/utils/index';
|
||||||
|
|
||||||
|
class User {
|
||||||
|
/**
|
||||||
|
* Creates a new user.
|
||||||
|
*
|
||||||
|
* @param {string|null} displayName Display name.
|
||||||
|
* @param {string|null} email Email.
|
||||||
|
* @param {boolean} isAnonymous Is anonymous.
|
||||||
|
* @param {string} uid The user's unique ID.
|
||||||
|
* @param {function():Promise<void>} deleteUser Delete user callback.
|
||||||
|
*/
|
||||||
|
constructor(displayName, email, isAnonymous, uid, deleteUser) {
|
||||||
|
if (!uid) {
|
||||||
|
throw new Error('uid must be provided.');
|
||||||
|
} else if (typeof uid !== 'string') {
|
||||||
|
throw new Error('uid should be a string.');
|
||||||
|
} else {
|
||||||
|
this._uid = uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!deleteUser) {
|
||||||
|
throw new Error('deleteUser must be provided.');
|
||||||
|
} else if (typeof deleteUser !== 'function') {
|
||||||
|
throw new Error('deleteUser should be a function.');
|
||||||
|
} else {
|
||||||
|
this._deleteUser = deleteUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._displayName = displayName;
|
||||||
|
this._email = email;
|
||||||
|
this._isAnonymous = isAnonymous;
|
||||||
|
}
|
||||||
|
|
||||||
|
get displayName() {
|
||||||
|
return this._displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
get email() {
|
||||||
|
return this._email;
|
||||||
|
}
|
||||||
|
|
||||||
|
get isAnonymous() {
|
||||||
|
return this._isAnonymous;
|
||||||
|
}
|
||||||
|
|
||||||
|
get uid() {
|
||||||
|
return this._uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete() {
|
||||||
|
await delay(Constants.defaultDelayInMilliseconds);
|
||||||
|
|
||||||
|
await this._deleteUser();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default User;
|
||||||
@ -2,7 +2,6 @@ const anonymousUser1 = {
|
|||||||
displayName: 'Anonymous User 1',
|
displayName: 'Anonymous User 1',
|
||||||
email: 'anonymous1@noemail.com',
|
email: 'anonymous1@noemail.com',
|
||||||
isAnonymous: true,
|
isAnonymous: true,
|
||||||
name: 'Anonymous 1',
|
|
||||||
uid: 'anonym123',
|
uid: 'anonym123',
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -10,7 +9,6 @@ const anonymousUser2 = {
|
|||||||
displayName: 'Anonymous User 2',
|
displayName: 'Anonymous User 2',
|
||||||
email: 'anonymous2@noemail.com',
|
email: 'anonymous2@noemail.com',
|
||||||
isAnonymous: true,
|
isAnonymous: true,
|
||||||
name: 'Anonymous 2',
|
|
||||||
uid: 'anonym456',
|
uid: 'anonym456',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user