import { expect, test } from 'bun:test';
import { render, fireEvent } from '@testing-library/preact';
import { Pb } from '../context.ts';
import { pb } from '../preload.ts';
import SignUp from './SignUp.tsx';

test('has a link to log in', () => {
	const { getAllByText, getByLabelText, getByText } = render(
		<SignUp />
	);

	expect(getAllByText(/log in/i)).not.toHaveLength(0);
});

test('can sign up', () => {
	const username = 'foo';
	const email = 'foo@example.com';
	const password = '12345678';

	const { getByText, getByLabelText } = render(
		<Pb.Provider value={pb}>
			<SignUp />
		</Pb.Provider>
	);

	fireEvent.change(getByLabelText(/username/i), {target: {value: username}});
	fireEvent.change(getByLabelText(/email/i), {target: {value: email}});
	fireEvent.change(getByLabelText(/password/i), {target: {value: password}});
	fireEvent.change(getByLabelText(/confirm password/i), {target: {value: password}});

	fireEvent.click(getByText(/continue/i));

	expect(pb.authStore.isValid()).toBeTrue();
});
