-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup jest (2 tests) and separate jobs
- Loading branch information
Showing
12 changed files
with
10,082 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
import type { Config } from 'jest'; | ||
import nextJest from 'next/jest.js'; | ||
|
||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: './', | ||
}); | ||
|
||
// Add any custom config to be passed to Jest | ||
const config: Config = { | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
moduleNameMapper: { // Handle Module Path Aliases | ||
'^@/(.*)$': '<rootDir>/$1', | ||
'^next/router$': '<rootDir>/tests/__mocks__/router.ts', | ||
}, | ||
coverageProvider: 'v8', | ||
testEnvironment: 'jsdom', | ||
// Add more setup options before each test is run | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'], | ||
}; | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
export default createJestConfig(config); |
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 |
---|---|---|
@@ -1 +1,42 @@ | ||
import '@testing-library/jest-dom/extend-expect' | ||
import '@testing-library/jest-dom'; | ||
|
||
class IntersectionObserver { | ||
root: Element | Document | null; | ||
rootMargin: string; | ||
thresholds: ReadonlyArray<number>; | ||
callback: IntersectionObserverCallback; | ||
options?: IntersectionObserverInit; | ||
|
||
constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit) { | ||
this.callback = callback; | ||
this.options = options; | ||
this.root = options?.root || null; | ||
this.rootMargin = options?.rootMargin || '0px'; | ||
this.thresholds = options?.threshold ? (Array.isArray(options.threshold) ? options.threshold : [options.threshold]) : [0]; | ||
} | ||
|
||
observe(target: Element) { | ||
const entry: IntersectionObserverEntry = { | ||
time: Date.now(), | ||
target, | ||
isIntersecting: true, | ||
intersectionRatio: 1, | ||
intersectionRect: target.getBoundingClientRect(), | ||
boundingClientRect: target.getBoundingClientRect(), | ||
rootBounds: this.root ? (this.root as Element).getBoundingClientRect() : null, | ||
}; | ||
this.callback([entry], this); | ||
} | ||
|
||
unobserve() {} | ||
|
||
disconnect() {} | ||
|
||
takeRecords(): IntersectionObserverEntry[] { | ||
return []; | ||
} | ||
} | ||
|
||
(global as any).IntersectionObserver = IntersectionObserver; | ||
|
||
export {}; |
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
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,22 @@ | ||
// __mocks__/next/router.ts | ||
const useRouter = () => { | ||
return { | ||
route: '/', | ||
pathname: '', | ||
query: '', | ||
asPath: '', | ||
push: jest.fn(), | ||
replace: jest.fn(), | ||
reload: jest.fn(), | ||
back: jest.fn(), | ||
prefetch: jest.fn().mockResolvedValue(undefined), | ||
beforePopState: jest.fn(), | ||
events: { | ||
on: jest.fn(), | ||
off: jest.fn(), | ||
emit: jest.fn(), | ||
}, | ||
}; | ||
}; | ||
|
||
export { useRouter }; |
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,60 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
|
||
import Footer from '@/components/Footer'; | ||
|
||
// Mock the next/link component | ||
jest.mock('next/link', () => { | ||
const MockLink = ({ children, href }: { children: React.ReactNode; href: string }) => { | ||
return <a href={href}>{children}</a>; | ||
}; | ||
MockLink.displayName = 'MockLink'; | ||
return MockLink; | ||
}); | ||
|
||
describe('Footer', () => { | ||
it('renders without crashing', () => { | ||
render(<Footer />); | ||
}); | ||
|
||
it('displays all navigation links', () => { | ||
render(<Footer />); | ||
const links = ['Home', 'Articles', 'Communities', 'Posts', 'About', 'Login', 'Register']; | ||
links.forEach((link) => { | ||
expect(screen.getByText(link)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays social media icons', () => { | ||
render(<Footer />); | ||
const socialMediaPlatforms = ['Facebook', 'Youtube', 'Instagram', 'Twitter']; | ||
socialMediaPlatforms.forEach((platform) => { | ||
expect(screen.getByText(platform, { selector: 'span' })).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays copyright information', () => { | ||
render(<Footer />); | ||
expect(screen.getByText(/© 2023 SciCommons. All rights reserved./)).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays Terms and Conditions link', () => { | ||
render(<Footer />); | ||
expect(screen.getByText('Terms and Conditions')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays Privacy Policy link', () => { | ||
render(<Footer />); | ||
expect(screen.getByText('Privacy Policy')).toBeInTheDocument(); | ||
}); | ||
|
||
it('applies correct CSS classes for light mode', () => { | ||
render(<Footer />); | ||
const footer = screen.getByRole('contentinfo'); | ||
expect(footer).toHaveClass('bg-gray-200'); | ||
}); | ||
|
||
// Note: Testing dark mode might require additional setup or a different approach | ||
// as it often depends on a theme context or CSS variables | ||
}); |
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,79 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
|
||
import FormInput from '@/components/FormInput'; | ||
|
||
// No need to mock the tooltip components anymore | ||
|
||
describe('FormInput', () => { | ||
const mockRegister = jest.fn(); | ||
const mockErrors = {}; | ||
|
||
const defaultProps = { | ||
name: 'testInput', | ||
type: 'text', | ||
placeholder: 'Enter text', | ||
register: mockRegister, | ||
requiredMessage: 'This field is required', | ||
errors: mockErrors, | ||
}; | ||
|
||
it('renders input field correctly', () => { | ||
render(<FormInput {...defaultProps} />); | ||
expect(screen.getByPlaceholderText('Enter text')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders label when provided', () => { | ||
render(<FormInput {...defaultProps} label="Test Label" />); | ||
expect(screen.getByText('Test Label')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders textarea when textArea prop is true', () => { | ||
render(<FormInput {...defaultProps} textArea={true} />); | ||
expect(screen.getByRole('textbox')).toHaveProperty('tagName', 'TEXTAREA'); | ||
}); | ||
|
||
it('applies readonly attribute when readOnly prop is true', () => { | ||
render(<FormInput {...defaultProps} readOnly={true} />); | ||
expect(screen.getByRole('textbox')).toHaveAttribute('readonly'); | ||
}); | ||
|
||
it('displays error message when there is an error', () => { | ||
const errorsWithMessage = { testInput: { message: 'Error message' } }; | ||
render(<FormInput {...defaultProps} errors={errorsWithMessage} />); | ||
expect(screen.getByText('Error message')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls register function with correct parameters', () => { | ||
render( | ||
<FormInput | ||
{...defaultProps} | ||
patternValue={/test/} | ||
patternMessage="Invalid pattern" | ||
minLengthValue={5} | ||
minLengthMessage="Too short" | ||
maxLengthValue={10} | ||
maxLengthMessage="Too long" | ||
/> | ||
); | ||
expect(mockRegister).toHaveBeenCalledWith('testInput', { | ||
required: 'This field is required', | ||
pattern: { value: /test/, message: 'Invalid pattern' }, | ||
minLength: { value: 5, message: 'Too short' }, | ||
maxLength: { value: 10, message: 'Too long' }, | ||
}); | ||
}); | ||
|
||
it('applies error styles when there is an error', () => { | ||
const errorsWithMessage = { testInput: { message: 'Error message' } }; | ||
render(<FormInput {...defaultProps} errors={errorsWithMessage} />); | ||
expect(screen.getByRole('textbox')).toHaveClass('border-red-500'); | ||
}); | ||
|
||
it('does not apply error styles when readOnly is true, even if there is an error', () => { | ||
const errorsWithMessage = { testInput: { message: 'Error message' } }; | ||
render(<FormInput {...defaultProps} errors={errorsWithMessage} readOnly={true} />); | ||
expect(screen.getByRole('textbox')).not.toHaveClass('border-red-500'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.