From 1ad608ba24c07ab2d5832437a0d4aa52ceb95d59 Mon Sep 17 00:00:00 2001 From: Maria Duran Date: Tue, 17 Sep 2024 19:18:39 -0500 Subject: [PATCH] updated tests --- app/__tests__/Dashboard.test.tsx | 69 ++++++++------------------------ 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/app/__tests__/Dashboard.test.tsx b/app/__tests__/Dashboard.test.tsx index deef25b..593942c 100644 --- a/app/__tests__/Dashboard.test.tsx +++ b/app/__tests__/Dashboard.test.tsx @@ -1,13 +1,11 @@ - import React from 'react'; import { render, screen } from '@testing-library/react'; -import '@testing-library/jest-dom'; -import DashboardPage from '../dashboard/page'; import DashboardClient from '../dashboard/DashboardClient'; +import DashboardServer from '../dashboard/DashboardServer'; +import Page from '../dashboard/page'; import { cookies } from 'next/headers'; import { redirect } from 'next/navigation'; -// Mock next/headers and next/navigation jest.mock('next/headers', () => ({ cookies: jest.fn(), })); @@ -16,61 +14,28 @@ jest.mock('next/navigation', () => ({ redirect: jest.fn(), })); -// Sample user data for the tests +jest.mock('../dashboard/DashboardServer', () => jest.fn(() =>
Mocked DashboardServer
)); + const mockUserData = { id: '1', email: 'test@example.com', - fullName: 'Test', - lastName: 'User', + fullName: 'John', + lastName: 'Doe', }; -describe('DashboardPage (Server Component)', () => { - it('should render DashboardClient when user data is found in cookies', () => { - // Mock cookies to return user data - (cookies as jest.Mock).mockReturnValue({ - get: jest.fn().mockReturnValue({ - value: JSON.stringify(mockUserData), - }), +describe('Dashboard Components', () => { + describe('DashboardClient', () => { + it('renders the user\'s full name', () => { + render(); + + expect(screen.getByText(/Hello John!/)).toBeInTheDocument(); }); - - render(); - - // Ensure DashboardClient is rendered - const greeting = screen.getByText(`Hello ${mockUserData.fullName}!`); - expect(greeting).toBeInTheDocument(); }); -}); -describe('DashboardClient (Client Component)', () => { - it('renders the user information and ExampleEquations component', () => { - render(); - - // Check if the user's name is displayed - const greeting = screen.getByText(`Hello ${mockUserData.fullName}!`); - expect(greeting).toBeInTheDocument(); - - // Instead of matching "ExampleEquations", look for the actual content it renders, like buttons or headers - const exampleEquationText = screen.getByText(/Select an Example/i); // Assuming ExampleEquations renders this text - expect(exampleEquationText).toBeInTheDocument(); - - // Alternatively, if ExampleEquations renders buttons, use getByRole: - const firstExampleButton = screen.getByRole('button', { name: /x \+ 1 = 2/i }); - expect(firstExampleButton).toBeInTheDocument(); + describe('Page', () => { + it('renders the DashboardServer component', () => { + render(); + expect(screen.getByText('Mocked DashboardServer')).toBeInTheDocument(); }); - - it('should render correctly when userData is provided', () => { - const mockUserData = { - id: '1', - email: 'test@example.com', - fullName: 'Test', - lastName: 'User' - }; - - render(); - - const greeting = screen.getByText(/Hello Test!/i); - expect(greeting).toBeInTheDocument(); - }); - }); - \ No newline at end of file +});