Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
YonatanKra / auth.spec.ts
Last active September 30, 2023 04:32
Tauri-demo: refactor login and signup mocks
function setUserCreds(firebaseAuth: any, successful: boolean, user = {
uid: '123',
email: '[email protected]'
}) {
return async () => {
(firebaseAuth.getAuth as any).mockReturnValue({
currentUser: successful ? user : null
});
if (successful) firebaseAuth.authChangeCallback();
@YonatanKra
YonatanKra / auth.ts
Created September 30, 2023 04:17
Tauri-demo: implement onAuthStateChanged
constructor() {
super();
onAuthStateChanged(getAuth(), () => this.dispatchEvent(new CustomEvent('user-status-change')));
}
@YonatanKra
YonatanKra / auth.spec.ts
Created September 30, 2023 04:14
Tauri-demo: add onAuthStateChange callback call to login and signup emulators
function setLogin(firebaseAuth: any, successful: boolean) {
(firebaseAuth.signInWithEmailAndPassword as any).mockImplementation(async () => {
const user = {
uid: '123',
email: '[email protected]'
};
(firebaseAuth.getAuth as any).mockReturnValue({
currentUser: successful ? user : null
});
@YonatanKra
YonatanKra / app.spec.ts
Created September 29, 2023 10:29
Tauri-demo: logout button click event logs us out
it('should call `logout` on auth component when logout button is clicked', () => {
isLoggedIn = true;
app.connectedCallback();
getLogoutButton()?.dispatchEvent(new CustomEvent('click'));
expect(authComponent.logout).toHaveBeenCalled();
});
@YonatanKra
YonatanKra / app.spec.ts
Last active September 25, 2023 19:09
Tauri-demo: logout button appearance
describe('logout button', () => {
it('should add a logout button to the header when user is logged in', () => {
isLoggedIn = true;
app.connectedCallback();
expect(isLoginButtonVisible()).toBe(true);
});
it('should remove the logout button from the header when user is logged out', () => {
isLoggedIn = true;
app.connectedCallback();
@YonatanKra
YonatanKra / auth.spec.ts
Created September 25, 2023 07:18
Tauri-demo: logout
describe('logout', () => {
let firebaseAuth: any;
beforeEach(async () => {
firebaseAuth = await import('firebase/auth');
});
it('should call firebase signOut', async () => {
auth.logout();
expect(firebaseAuth.signOut).toHaveBeenCalledWith(firebaseAuth.getAuth());
@YonatanKra
YonatanKra / auth.spec.ts
Created September 24, 2023 03:12
Tauri-demo: login with auto signup
import { Auth } from './auth';
const COMPONENT_NAME = 'yag-test';
vi.mock('firebase/auth', () => {
return {
getAuth: vi.fn().mockReturnValue({
currentUser: null
}),
signInWithEmailAndPassword: vi.fn(),
fetchSignInMethodsForEmail: vi.fn(),
@YonatanKra
YonatanKra / auth.spec.ts
Last active September 23, 2023 19:23
Tauri-demo: fire user state event only on successful login
it('should emit `user-status-change` event when login successful', async () => {
const firebaseAuth = await import('firebase/auth');
(firebaseAuth.signInWithEmailAndPassword as any).mockImplementation(async () => {
(firebaseAuth.getAuth as any).mockReturnValue({
currentUser: {
uid: '123',
email: '[email protected]'
}
});
});
@YonatanKra
YonatanKra / auth.spec.ts
Created September 23, 2023 19:05
Tauri-demo: call signInWithEmailAndPassword with email and password
it('should call `signInWithEmailAndPassword` with auth, email and password', async () => {
const email = '[email protected]';
const password = '123456';
const firebaseAuth = await import('firebase/auth');
await auth.login(email, password);
expect(firebaseAuth.signInWithEmailAndPassword).toHaveBeenCalledWith(firebaseAuth.getAuth(), email, password);
});
@YonatanKra
YonatanKra / auth.spec.ts
Created September 23, 2023 18:41
Tauri-demo: test login method with firebase auth
it('should toggle `isLoggedIn` if login successful', async () => {
const firebaseAuth = await import('firebase/auth');
(firebaseAuth.signInWithEmailAndPassword as any).mockImplementation(async () => {
const user = {
uid: '123',
email: '[email protected]'
};
(firebaseAuth.getAuth as any).mockReturnValue({