-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for course assignments functionality
- Loading branch information
1 parent
ac133d1
commit 1cd8654
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import '@testing-library/jest-dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import CourseAssignments from './CourseAssignments'; | ||
|
||
const renderWithRouter = (component: React.ReactNode) => { | ||
return render( | ||
<BrowserRouter> | ||
{component} | ||
</BrowserRouter> | ||
); | ||
}; | ||
|
||
describe('CourseAssignments', () => { | ||
const mockCourseId = 101; | ||
const mockCourseName = 'Test Course'; | ||
|
||
it('renders the component correctly', () => { | ||
renderWithRouter(<CourseAssignments courseId={mockCourseId} courseName={mockCourseName} />); | ||
|
||
// Check if the course name is displayed | ||
expect(screen.getByText(`Assignments for ${mockCourseName}`)).toBeInTheDocument(); | ||
|
||
// Check if the table renders | ||
const table = screen.getByRole('table'); | ||
expect(table).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders assignments in the table', () => { | ||
renderWithRouter(<CourseAssignments courseId={mockCourseId} courseName={mockCourseName} />); | ||
|
||
// Check for table rows (excluding header row) | ||
const rows = screen.getAllByRole('row'); | ||
expect(rows.length).toBeGreaterThan(1); // Header + assignment rows | ||
}); | ||
|
||
it('triggers edit and delete actions correctly', async () => { | ||
// Spy on console.log to check if handlers are called | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
||
renderWithRouter(<CourseAssignments courseId={mockCourseId} courseName={mockCourseName} />); | ||
|
||
// Find and verify buttons | ||
const editButtons = screen.getAllByRole('button', { name: /edit/i }); | ||
const deleteButtons = screen.getAllByRole('button', { name: /delete/i }); | ||
|
||
expect(editButtons).toHaveLength(4); // Adjust based on your table rows | ||
expect(deleteButtons).toHaveLength(4); | ||
|
||
// Trigger clicks | ||
await userEvent.click(editButtons[0]); | ||
await userEvent.click(deleteButtons[0]); | ||
|
||
// Check exact console log outputs | ||
const firstAssignment = { | ||
id: expect.any(Number), | ||
name: expect.stringContaining('Assignment 1'), | ||
courseName: mockCourseName, | ||
description: expect.any(String), | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
}; | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith('Edit assignment:', expect.objectContaining(firstAssignment)); | ||
expect(consoleSpy).toHaveBeenCalledWith('Delete assignment:', expect.objectContaining(firstAssignment)); | ||
|
||
// Clean up mock | ||
consoleSpy.mockRestore(); | ||
}); | ||
|
||
}); |