Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikpaliwal committed Mar 2, 2024
1 parent 80392d6 commit 26c4911
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/router/aboutRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function AboutRoutes({
{...(sitemap && sitemapDefaults)}
/>,
<Route
key="about-not-found"
key="AboutNotFound"
path="/about/*"
render={(): ReactNode => <NotFoundPage />}
/>,
Expand Down
149 changes: 148 additions & 1 deletion src/router/notFoundRoutes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import { Router, Switch } from 'react-router-dom'
import FragmentariumRoutes from './fragmentariumRoutes'
import { getServices } from 'test-support/AppDriver'
import { createMemoryHistory } from 'history'
import AboutRoutes from './aboutRoutes'
import FragmentariumRoutes from './fragmentariumRoutes'
import BibliographyRoutes from './bibliographyRoutes'
import CorpusRoutes from './corpusRoutes'
import DictionaryRoutes from './dictionaryRoutes'
import SignRoutes from './signRoutes'
import ToolsRoutes from './toolsRoutes'

describe('NotFoundPage rendering in FragmentariumRoutes', () => {
const nonExistentRoutes = [
Expand All @@ -28,3 +34,144 @@ describe('NotFoundPage rendering in FragmentariumRoutes', () => {
})
})
})

describe('NotFoundPage rendering in AboutRoutes', () => {
const nonExistentAboutRoutes = [
'/about/non-existent-page',
'/about/invalid-section',
'/about/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...AboutRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})

describe('NotFoundPage rendering in BibliographyRoutes', () => {
const nonExistentAboutRoutes = [
'/bibliography/search/non-existent',
'/bibliography/afo-register/non-existent-page',
'/bibliography/afo-register/invalid-section',
'/bibliography/afo-register/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...BibliographyRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})

describe('NotFoundPage rendering in CorpusRoutes', () => {
const nonExistentAboutRoutes = [
'/corpus/Corpus.12345/non-existent-page',
'/corpus/Corpus.12345/invalid-section',
'/corpus/Corpus.12345/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...CorpusRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})

describe('NotFoundPage rendering in DictionaryRoutes', () => {
const nonExistentAboutRoutes = [
'/dictionary/search/non-existent',
'/dictionary/Dictionary.12345/non-existent-page',
'/dictionary/Dictionary.12345/invalid-section',
'/dictionary/Dictionary.12345/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...DictionaryRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})

describe('NotFoundPage rendering in SignRoutes', () => {
const nonExistentAboutRoutes = [
'/signs/search/non-existent',
'/signs/Signs.12345/non-existent-page',
'/signs/Signs.12345/invalid-section',
'/signs/Signs.12345/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...SignRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})

describe('NotFoundPage rendering in ToolsRoutes', () => {
const nonExistentAboutRoutes = [
'/tools/date-converter/non-existent-page',
'/tools/date-converter/invalid-section',
'/tools/date-converter/undefined-route',
]
nonExistentAboutRoutes.forEach((path) => {
const history = createMemoryHistory({ initialEntries: [path] })
test(`renders NotFoundPage for "${path}"`, () => {
render(
<Router history={history}>
<Switch>
{[...ToolsRoutes({ ...getServices(), sitemap: false })]}
</Switch>
</Router>
)
expect(
screen.getByText(/The page you are looking for does not exist./i)
).toBeInTheDocument()
})
})
})
2 changes: 1 addition & 1 deletion src/router/toolsRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function ToolsRoutes({
{...(sitemap && sitemapDefaults)}
/>,
<Route
key="tools-not-found"
key="ToolsNotFound"
path="/tools/*"
exact
render={(): ReactNode => <NotFoundPage />}
Expand Down

0 comments on commit 26c4911

Please sign in to comment.