mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-22 20:51:29 +10:00
FirebaseStub: added more unit tests related to auth
This commit is contained in:
@ -0,0 +1,35 @@
|
|||||||
|
import FirebaseStub, { AuthConstants } from '../../gatsby-plugin-firebase';
|
||||||
|
|
||||||
|
test('sets current user to anonymous user 1', async () => {
|
||||||
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
|
const { currentUser } = FirebaseStub.auth();
|
||||||
|
expect(currentUser).toBeTruthy();
|
||||||
|
expect(currentUser.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls onAuthStateChanged observer with anonymous user 1', async () => {
|
||||||
|
let user = null;
|
||||||
|
let error = null;
|
||||||
|
FirebaseStub.auth().onAuthStateChanged(
|
||||||
|
(_user) => {
|
||||||
|
user = _user;
|
||||||
|
},
|
||||||
|
(_error) => {
|
||||||
|
error = _error;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
|
expect(user).toBeTruthy();
|
||||||
|
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
||||||
|
expect(error).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns anonymous user 1', async () => {
|
||||||
|
const user = await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
|
expect(user).toBeTruthy();
|
||||||
|
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
||||||
|
});
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
import FirebaseStub, { AuthConstants } from '../../gatsby-plugin-firebase';
|
||||||
|
|
||||||
|
describe('with Google auth provider', () => {
|
||||||
|
test('sets current user to Google user 3', async () => {
|
||||||
|
await FirebaseStub.auth().signInWithPopup(
|
||||||
|
new FirebaseStub.auth.GoogleAuthProvider(),
|
||||||
|
);
|
||||||
|
|
||||||
|
const { currentUser } = FirebaseStub.auth();
|
||||||
|
expect(currentUser).toBeTruthy();
|
||||||
|
expect(currentUser.uid).toEqual(AuthConstants.googleUser3.uid);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sets current user provider data', async () => {
|
||||||
|
const provider = new FirebaseStub.auth.GoogleAuthProvider();
|
||||||
|
await FirebaseStub.auth().signInWithPopup(provider);
|
||||||
|
|
||||||
|
const { currentUser } = FirebaseStub.auth();
|
||||||
|
expect(currentUser).toBeTruthy();
|
||||||
|
expect(currentUser.providerData).toBeTruthy();
|
||||||
|
expect(currentUser.providerData).toHaveLength(1);
|
||||||
|
expect(currentUser.providerData[0].providerId).toEqual(provider.providerId);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls onAuthStateChanged observer with Google user 3', async () => {
|
||||||
|
let user = null;
|
||||||
|
let error = null;
|
||||||
|
FirebaseStub.auth().onAuthStateChanged(
|
||||||
|
(_user) => {
|
||||||
|
user = _user;
|
||||||
|
},
|
||||||
|
(_error) => {
|
||||||
|
error = _error;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await FirebaseStub.auth().signInWithPopup(
|
||||||
|
new FirebaseStub.auth.GoogleAuthProvider(),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(user).toBeTruthy();
|
||||||
|
expect(user.uid).toEqual(AuthConstants.googleUser3.uid);
|
||||||
|
expect(error).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns Google user 3', async () => {
|
||||||
|
const user = await FirebaseStub.auth().signInWithPopup(
|
||||||
|
new FirebaseStub.auth.GoogleAuthProvider(),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(user).toBeTruthy();
|
||||||
|
expect(user.uid).toEqual(AuthConstants.googleUser3.uid);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
import FirebaseStub from '../../gatsby-plugin-firebase';
|
||||||
|
|
||||||
|
test('sets current user to null', async () => {
|
||||||
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
|
await FirebaseStub.auth().signOut();
|
||||||
|
|
||||||
|
const { currentUser } = FirebaseStub.auth();
|
||||||
|
expect(currentUser).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls onAuthStateChanged observer with null', async () => {
|
||||||
|
let user = null;
|
||||||
|
let error = null;
|
||||||
|
FirebaseStub.auth().onAuthStateChanged(
|
||||||
|
(_user) => {
|
||||||
|
user = _user;
|
||||||
|
},
|
||||||
|
(_error) => {
|
||||||
|
error = _error;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
|
||||||
|
await FirebaseStub.auth().signOut();
|
||||||
|
|
||||||
|
expect(user).toBeNull();
|
||||||
|
expect(error).toBeNull();
|
||||||
|
});
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import FirebaseStub, { AuthConstants } from '../../gatsby-plugin-firebase';
|
import FirebaseStub from '../../gatsby-plugin-firebase';
|
||||||
|
|
||||||
test('reuses existing Auth instance', () => {
|
test('reuses existing Auth instance', () => {
|
||||||
const auth1 = FirebaseStub.auth();
|
const auth1 = FirebaseStub.auth();
|
||||||
@ -9,32 +9,6 @@ test('reuses existing Auth instance', () => {
|
|||||||
expect(auth1.uuid).toEqual(auth2.uuid);
|
expect(auth1.uuid).toEqual(auth2.uuid);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('returns anonymous user 1 when signing in anonymously', async () => {
|
|
||||||
const user = await FirebaseStub.auth().signInAnonymously();
|
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
|
||||||
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('calls onAuthStateChanged observer with anonymous user 1 when signing in anonymously', async () => {
|
|
||||||
let user = null;
|
|
||||||
let error = null;
|
|
||||||
FirebaseStub.auth().onAuthStateChanged(
|
|
||||||
(_user) => {
|
|
||||||
user = _user;
|
|
||||||
},
|
|
||||||
(_error) => {
|
|
||||||
error = _error;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await FirebaseStub.auth().signInAnonymously();
|
|
||||||
|
|
||||||
expect(user).toBeTruthy();
|
|
||||||
expect(user.uid).toEqual(AuthConstants.anonymousUser1.uid);
|
|
||||||
expect(error).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('onAuthStateChanged unsubscribe removes observer', () => {
|
test('onAuthStateChanged unsubscribe removes observer', () => {
|
||||||
const observer = () => {};
|
const observer = () => {};
|
||||||
const unsubscribe = FirebaseStub.auth().onAuthStateChanged(observer);
|
const unsubscribe = FirebaseStub.auth().onAuthStateChanged(observer);
|
||||||
@ -49,3 +23,13 @@ test('onAuthStateChanged unsubscribe removes observer', () => {
|
|||||||
FirebaseStub.auth().onAuthStateChangedObservers.indexOf(observer),
|
FirebaseStub.auth().onAuthStateChangedObservers.indexOf(observer),
|
||||||
).not.toBeGreaterThanOrEqual(0);
|
).not.toBeGreaterThanOrEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('current user delete calls signOut', async () => {
|
||||||
|
const mockSignOut = jest.spyOn(FirebaseStub.auth(), 'signOut');
|
||||||
|
await FirebaseStub.auth().signInAnonymously();
|
||||||
|
const { currentUser } = FirebaseStub.auth();
|
||||||
|
|
||||||
|
await currentUser.delete();
|
||||||
|
|
||||||
|
expect(mockSignOut).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|||||||
@ -60,7 +60,7 @@ class Auth {
|
|||||||
user.providerId,
|
user.providerId,
|
||||||
user.uid,
|
user.uid,
|
||||||
user.isAnonymous,
|
user.isAnonymous,
|
||||||
this.signOut,
|
this.signOut.bind(this),
|
||||||
);
|
);
|
||||||
|
|
||||||
await delay(Constants.defaultDelayInMilliseconds);
|
await delay(Constants.defaultDelayInMilliseconds);
|
||||||
@ -98,7 +98,7 @@ class Auth {
|
|||||||
user.providerId,
|
user.providerId,
|
||||||
user.uid,
|
user.uid,
|
||||||
user.isAnonymous,
|
user.isAnonymous,
|
||||||
this.signOut,
|
this.signOut.bind(this),
|
||||||
);
|
);
|
||||||
|
|
||||||
await delay(Constants.defaultDelayInMilliseconds);
|
await delay(Constants.defaultDelayInMilliseconds);
|
||||||
|
|||||||
Reference in New Issue
Block a user