-
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.
Add Jest testing setup and ParticleSystem component tests
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
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,42 @@ | ||
import { render } from '@testing-library/react' | ||
import { ThemeProvider } from 'next-themes' | ||
import { ParticleSystem } from '../particle-system' | ||
import 'jest-canvas-mock' | ||
|
||
// Mock window.matchMedia | ||
beforeAll(() => { | ||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(query => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // deprecated | ||
removeListener: jest.fn(), // deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}) | ||
}) | ||
|
||
const renderWithTheme = (component: React.ReactNode) => { | ||
return render( | ||
<ThemeProvider attribute="class"> | ||
{component} | ||
</ThemeProvider> | ||
) | ||
} | ||
|
||
describe('ParticleSystem', () => { | ||
it('renders without crashing', () => { | ||
const { container } = renderWithTheme(<ParticleSystem />) | ||
expect(container).toBeInTheDocument() | ||
}) | ||
|
||
it('creates a canvas element', () => { | ||
const { container } = renderWithTheme(<ParticleSystem />) | ||
const canvas = container.querySelector('canvas') | ||
expect(canvas).toBeInTheDocument() | ||
}) | ||
}) |
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,18 @@ | ||
const nextJest = require('next/jest') | ||
|
||
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 customJestConfig = { | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
testEnvironment: 'jest-environment-jsdom', | ||
moduleNameMapper: { | ||
'^@/(.*)$': '<rootDir>/$1', | ||
}, | ||
} | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig) |
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,3 @@ | ||
// Learn more: https://github.com/testing-library/jest-dom | ||
import '@testing-library/jest-dom' | ||
import 'jest-canvas-mock' |
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