Skip to content

Commit

Permalink
v0.41.0 Update tests
Browse files Browse the repository at this point in the history
Added DOMPurify module for sanitizing user input for forms.

Update vscode launch.json for debugging.

Update `.gitignore` to ignore files prefixed with a single underscore
for files that I do not want added to the repository.

Update test script to fix error adding the `--detectOpenHandles`
argument.
testing-library/dom-testing-library#524

Update tests
- Added mocks for the app setup for the theme, routing, and state store.
- Created `commonSetup.js` module that re-exports multiple mocks for a
single import instead of multiple lines of mock imports.
- Refactor `Home.test.tsx` to use the new `commonSetup.js` for importing
the mocks.
  • Loading branch information
lgoodcode committed Apr 6, 2022
1 parent 2389e61 commit 022fd1b
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ coverage
# production
build

# regexp to ignore specific files
**/__*
# ignore specific files prefixed with a single underscore
**/_[!_]*

# eslintcache for the lint-staged pre-commit
.eslintcache
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
Expand Down
34 changes: 32 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whitelist",
"version": "0.40.0",
"version": "0.41.0",
"private": true,
"engines": {
"node": "^17.x"
Expand Down Expand Up @@ -35,6 +35,7 @@
"bfj": "^7.0.2",
"browserslist": "^4.20.0",
"camelcase": "^6.2.1",
"dompurify": "^2.3.6",
"dotenv": "^14.2.0",
"dotenv-expand": "^8.0.3",
"fs-extra": "^10.0.1",
Expand Down Expand Up @@ -132,6 +133,7 @@
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^14.0.0",
"@types/aos": "^3.0.4",
"@types/dompurify": "^2.3.3",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"@types/react": "^17.0.43",
Expand Down
2 changes: 2 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ if (
argv.push(hasSourceControl ? '--watch' : '--watchAll')
}

argv.push('--detectOpenHandles')

jest.run(argv)
7 changes: 7 additions & 0 deletions src/__tests__/commonSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file is used to easily export more than two mock implementations
* through a single import file instead of multiple lines.
*/
export { default as StoreMock } from './mocks/StoreMock'
export { default as RoutingMock } from './mocks/RoutingMock'
export { default as ThemeMock } from './mocks/ThemeMock'
6 changes: 6 additions & 0 deletions src/__tests__/mocks/RoutingMock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BrowserRouter } from 'react-router-dom'
import type { ReactNode } from 'react'

export default ({ children }: { children: ReactNode }) => (
<BrowserRouter>{children}</BrowserRouter>
)
7 changes: 7 additions & 0 deletions src/__tests__/mocks/StoreMock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Provider } from 'react-redux'
import store from 'app/store'
import type { ReactNode } from 'react'

export default ({ children }: { children: ReactNode }) => (
<Provider store={store}>{children}</Provider>
)
11 changes: 11 additions & 0 deletions src/__tests__/mocks/ThemeMock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ThemeProvider } from '@mui/material/styles'
import type { ReactNode } from 'react'
import { StyleTheme, ComponentTheme } from 'assets/theme'

const ThemeMock = ({ children }: { children: ReactNode }) => (
<ThemeProvider theme={StyleTheme('dark')}>
<ThemeProvider theme={ComponentTheme}>{children}</ThemeProvider>
</ThemeProvider>
)

export default ThemeMock
21 changes: 8 additions & 13 deletions src/__tests__/pages/Home.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import * as React from 'react'
import { BrowserRouter } from 'react-router-dom'
import { ThemeProvider } from '@mui/material/styles'
import { render, screen } from '@testing-library/react'
import { Provider } from 'react-redux'
import { StoreMock, RoutingMock, ThemeMock } from '../commonSetup'
import Home from 'pages/Home'
import store from 'app/store'
import { StyleTheme } from 'assets/theme'

// Need to import it manually because it has to set the window
// property, whereas jest.mock() simply imports the module
// eslint-disable-next-line jest/no-mocks-import
import './__mocks__/intersectionObserverMock'
import '../mocks/intersectionObserverMock'

test('renders home page without crashing', () => {
render(
<Provider store={store}>
<BrowserRouter>
<ThemeProvider theme={StyleTheme('dark')}>
<StoreMock>
<RoutingMock>
<ThemeMock>
<Home />
</ThemeProvider>
</BrowserRouter>
</Provider>
</ThemeMock>
</RoutingMock>
</StoreMock>
)
expect(screen.getByText(/your go-to helium center/i)).toBeInTheDocument()
})

0 comments on commit 022fd1b

Please sign in to comment.