Skip to content

Instantly share code, notes, and snippets.

@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: '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(() => {