Skip to content

Commit

Permalink
Create login.test.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 9, 2024
1 parent 2da78b1 commit 4e29251
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions projects/pi-nexus-iam/frontend/login.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { Login } from './Login';

describe('Login component', () => {
it('renders correctly', () => {
const { getByText } = render(<Login />);
expect(getByText('Login')).toBeInTheDocument();
});

it('calls login function on submit', () => {
const loginFn = jest.fn();
const { getByText } = render(<Login login={loginFn} />);
const form = getByText('Login');
fireEvent.submit(form);
expect(loginFn).toHaveBeenCalledTimes(1);
});

it('displays error message on invalid credentials', async () => {
const loginFn = jest.fn(() => Promise.reject(new Error('Invalid credentials')));
const { getByText } = render(<Login login={loginFn} />);
const form = getByText('Login');
fireEvent.submit(form);
await waitFor(() => getByText('Invalid credentials'));
expect(getByText('Invalid credentials')).toBeInTheDocument();
});
});

0 comments on commit 4e29251

Please sign in to comment.