Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
YonatanKra / app.spec.ts
Created October 5, 2023 03:20
Tauri-demo: display alert when user logs in but not verified
it('should display an alert if user is logged in and email not verified', () => {
app.connectedCallback();
authComponent.isUserEmailVerified.mockReturnValue(false);
const spy = vi.spyOn(app, 'alert');
setLoginStatus(true);
expect(spy).toHaveBeenCalledWith({message: 'Please verify your email address', title: 'Email not verified'});
});
@YonatanKra
YonatanKra / app.spec.ts
Last active October 5, 2023 03:16
Tauri-demo: add an alert
describe('alert', () => {
it('should display an alert with given message and title', () => {
app.connectedCallback();
const message = 'some message';
const title = 'some title';
app.alert({message, title});
const alert = app.shadowRoot?.querySelector('#alert');
expect(alert?.getAttribute('Headline')).toBe(title);
expect(alert?.getAttribute('text')).toBe(message);
});
@YonatanKra
YonatanKra / auth.ts
Created October 4, 2023 16:39
Tauri-demo: refactor the login method
async #isUserRegistered(email: string) {
const signInMethods = await fetchSignInMethodsForEmail(getAuth(), email);
return signInMethods.length > 0;
}
async #registerUser(email: string, password: string) {
await createUserWithEmailAndPassword(getAuth(), email, password);
await sendEmailVerification(getAuth().currentUser!);
}
@YonatanKra
YonatanKra / app.spec.ts
Created October 3, 2023 10:20
Tauri-demo: app tests to wait for Auth init
import { App } from './app';
customElements.define('yag-app', App);
class MockAuth extends HTMLElement {
constructor() {
super();
authComponent = this;
}
@YonatanKra
YonatanKra / auth.ts
Created September 30, 2023 04:35
Tauri-demo: extract auth change event handler
#handleAuthChange = () => {
this.dispatchEvent(new CustomEvent('user-status-change'));
}
constructor() {
super();
onAuthStateChanged(getAuth(), this.#handleAuthChange);
}
@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: 'test@test.com'
}) {
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: 'test@test.com'
};
(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();