Skip to content

Instantly share code, notes, and snippets.

@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: 'test@test.com'
}
});
});
@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 = 't@t.com';
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: 'test@test.com'
};
(firebaseAuth.getAuth as any).mockReturnValue({
@YonatanKra
YonatanKra / firebase.spec.ts
Last active September 23, 2023 02:22
Tauri-demo: firebase app init
import { Firebase } from './firebase';
describe('firebase', () => {
let firebase: Firebase;
beforeAll(() => {
vi.mock('firebase/app', () => {
return {
initializeApp: () => 'MockFirebaseApp'
}
const numberInput = document.createElement('input');
numberInput.type = 'number';
export function getValidValue(value: string) {
if (!this.isUserInput) {
numberInput.value = value;
return numberInput.value;
}
if (value === '' || value === '-' || value === '.') {
return value;
@YonatanKra
YonatanKra / auth.spec.ts
Created September 20, 2023 06:33
Tauri-demo: the auth component
import { Auth } from './auth';
describe('login', () => {
let auth: Auth;
beforeAll(() => {
customElements.define('yag-auth', Auth);
});
beforeEach(() => {
@YonatanKra
YonatanKra / app.ts
Created September 20, 2023 05:08
Tauri-demo: refactor login-attempt listeners
get #loginElement () {
return this.shadowRoot!.querySelector('yag-login') as HTMLElement;
}
#setLoginListener = () => {
this.#loginElement.addEventListener('login-attempt', this.#handleLoginAttempt);
}
#unsetLoginListener = () => {
this.#loginElement?.removeEventListener('login-attempt', this.#handleLoginAttempt);
@YonatanKra
YonatanKra / app.ts
Created September 20, 2023 05:03
Tauri-demo: implement login attempt listener
#handleLoginAttempt = (e: Event) => {
const { email, password } = (<CustomEvent>e).detail;
this.#authComponent?.login(email, password);
}
#setViewAccordingToUserStatus = () => {
if (!this.#authComponent!.isLoggedIn || this.#authComponent!.isLoggedIn?.() === false) {
this.shadowRoot!.innerHTML = `<yag-login></yag-login>`;
const loginElement = this.shadowRoot!.querySelector('yag-login') as HTMLElement;
loginElement.addEventListener('login-attempt', this.#handleLoginAttempt);