Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/email/email.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,86 @@ describe('EmailService', () => {
it('should be defined', () => {
expect(service).toBeDefined();
});

describe('sendVerificationCode', () => {
it('should call signInWithOtp with correct parameters', async () => {
SupabaseServiceMock.client.auth.signInWithOtp.mockResolvedValue({
error: null,
});

const userEmail = 'test@example.com';
await service.sendVerificationCode(userEmail);

expect(
SupabaseServiceMock.client.auth.signInWithOtp,
).toHaveBeenCalledWith({
email: userEmail,
options: { shouldCreateUser: false },
});
});
it('should throw an error if signInWithOtp fails', async () => {
const error = new Error('Sign-in failed');
SupabaseServiceMock.client.auth.signInWithOtp.mockResolvedValueOnce(
{ error },
);

const userEmail = 'test@example.com';
await expect(
service.sendVerificationCode(userEmail),
).rejects.toThrow(error);

expect(
SupabaseServiceMock.client.auth.signInWithOtp,
).toHaveBeenCalledWith({
email: userEmail,
options: { shouldCreateUser: false },
});
});
});
describe('verifyVerificationCode', () => {
it('should call verifyOtp with correct parameters', async () => {
SupabaseServiceMock.client.auth.verifyOtp.mockResolvedValueOnce({
error: null,
});

const userEmail = 'test@example.com';
const submittedCode = '123456';
const result = await service.verifyVerificationCode(
userEmail,
submittedCode,
);

expect(
SupabaseServiceMock.client.auth.verifyOtp,
).toHaveBeenCalledWith({
email: userEmail,
token: submittedCode,
type: 'email',
});
expect(result).toBe(true);
});

it('should return false if verifyOtp fails', async () => {
const error = new Error('Verification failed');
SupabaseServiceMock.client.auth.verifyOtp.mockResolvedValueOnce({
error,
});

const userEmail = 'test@example.com';
const submittedCode = '123456';
const result = await service.verifyVerificationCode(
userEmail,
submittedCode,
);

expect(
SupabaseServiceMock.client.auth.verifyOtp,
).toHaveBeenCalledWith({
email: userEmail,
token: submittedCode,
type: 'email',
});
expect(result).toBe(false);
});
});
});
Loading