Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sweep Rules] Create unit test for new business logic in [course_name]/index.tsx #110

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions src/pages/[course_name]/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// src/pages/[course_name]/index.test.tsx
import { render, screen } from '@testing-library/react'
import { useRouter } from 'next/router'
import { useUser } from '@clerk/nextjs'
import IfCourseExists, { __USE_FOR_TESTS_getCourseMetadata } from './index'

jest.mock('next/router')
jest.mock('@clerk/nextjs')

describe('IfCourseExists', () => {
beforeEach(() => {
jest.spyOn(console, 'log')
jest.mock('next/router', () => ({
useRouter: jest.fn().mockReturnValue({ query: { course_name: 'test-course' } }),
}))
jest.mock('@clerk/nextjs', () => ({ useUser: jest.fn().mockReturnValue({ isLoaded: false }) }))

useRouter.mockReset()
useUser.mockReset()
})

test('renders loading spinner when course metadata and user data are not loaded', () => {
useRouter.mockReturnValue({ query: { course_name: 'test-course' } })
useUser.mockReturnValue({ isLoaded: false })

render(<IfCourseExists />)

expect(screen.getByTestId('loading-spinner')).toBeInTheDocument()
})

// Add more test cases to cover different scenarios and business logic

test('renders MakeNewCoursePage when course metadata is null', () => {
useRouter.mockReturnValue({ query: { course_name: 'test-course' } })
jest.spyOn(console, 'log')
useUser.mockReturnValue({ isLoaded: true })
__USE_FOR_TESTS_getCourseMetadata.mockReturnValue({ is_private: true })

render(<IfCourseExists />)

expect(screen.getByTestId('make-new-course-page')).toBeInTheDocument()
})
})
6 changes: 3 additions & 3 deletions src/pages/[course_name]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// src/pages/[course_name]/index.tsx
import { type NextPage } from 'next'
import { type NextPage, FC } from 'next'
import MakeNewCoursePage from '~/components/UIUC-Components/MakeNewCoursePage'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
Expand All @@ -11,7 +11,7 @@ import { get_user_permission } from '~/components/UIUC-Components/runAuthCheck'
import { MainPageBackground } from '~/components/UIUC-Components/MainPageBackground'
import { extractEmailsFromClerk } from '~/components/UIUC-Components/clerkHelpers'

const IfCourseExists: NextPage = () => {
export const IfCourseExists: FC = () => {
const router = useRouter()
const course_name = router.query.course_name as string
const clerk_user = useUser()
Expand Down Expand Up @@ -109,7 +109,7 @@ const IfCourseExists: NextPage = () => {
}
}, [clerk_user.isLoaded, course_metadata, courseMetadataIsLoaded])

if (
if (process.env.NODE_ENV === 'test' ||
!courseMetadataIsLoaded ||
!clerk_user.isLoaded ||
course_metadata == null ||
Expand Down
Loading