Add tests for index page

This commit is contained in:
Stedoss 2022-11-01 22:37:37 +00:00
parent a36604ea4b
commit b3ebd978d9
8 changed files with 1802 additions and 307 deletions

View File

@ -144,6 +144,4 @@ More for the frontend, but backend doesn't have the best either. More checks mak
Even if it's just a few, it will show off how I unit test for the frontend. Even if it's just a few, it will show off how I unit test for the frontend.
### Error loading states
### Add some comments around ### Add some comments around

View File

@ -1,5 +0,0 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

View File

@ -0,0 +1,8 @@
import type { Config } from "jest";
const config: Config = {
preset: "ts-jest",
testEnvironment: "jsdom"
};
export default config;

File diff suppressed because it is too large Load Diff

View File

@ -14,19 +14,23 @@
"@emotion/styled": "^11.10.5", "@emotion/styled": "^11.10.5",
"@mui/material": "^5.10.11", "@mui/material": "^5.10.11",
"@tanstack/react-query": "^4.13.4", "@tanstack/react-query": "^4.13.4",
"@testing-library/jest-dom": "^5.16.5",
"localforage": "^1.10.0", "localforage": "^1.10.0",
"match-sorter": "^6.3.1", "match-sorter": "^6.3.1",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-router-dom": "^6.4.2", "react-router-dom": "^6.4.2",
"sort-by": "^1.2.0" "sort-by": "^1.2.0",
"ts-node": "^10.9.1"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@types/react": "^18.0.22", "@types/react": "^18.0.22",
"@types/react-dom": "^18.0.7", "@types/react-dom": "^18.0.7",
"@types/testing-library__jest-dom": "^5.14.5",
"@vitejs/plugin-react": "^2.2.0", "@vitejs/plugin-react": "^2.2.0",
"jest": "^29.2.2", "jest": "^29.2.2",
"jest-environment-jsdom": "^29.2.2",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"typescript": "^4.6.4", "typescript": "^4.6.4",
"vite": "^3.2.0" "vite": "^3.2.0"

View File

@ -14,11 +14,13 @@ export function Index() {
const api = new API(Environment.BaseAPIUrl); const api = new API(Environment.BaseAPIUrl);
const { data: venueData } = useQuery<TVenue[], Error>(["venues", filteredCategories, filteredTags, nameTextFilter], () => api.getVenues({nameFilter: nameTextFilter, categories: filteredCategories, tags: filteredTags})); const { data: venueData, isError: venueError } = useQuery<TVenue[], Error>(["venues", filteredCategories, filteredTags, nameTextFilter], () => api.getVenues(
{nameFilter: nameTextFilter, categories: filteredCategories, tags: filteredTags}
));
const { data: categoryData } = useQuery<TCategory[], Error>(["categories"], api.getCategories); const { data: categoryData, isError: categoryError } = useQuery<TCategory[], Error>(["categories"], api.getCategories);
const { data: tagData } = useQuery<TTag[], Error>(["tags"], api.getTags); const { data: tagData, isError: tagError } = useQuery<TTag[], Error>(["tags"], api.getTags);
const handleSearchChange = (event: any) => { const handleSearchChange = (event: any) => {
const { value } = event.target; const { value } = event.target;
@ -33,12 +35,20 @@ export function Index() {
return( return(
<> <>
<h1>Leeds Beer Quest</h1> <h1>Leeds Beer Quest</h1>
<div style={{ display: "flex", justifyContent: "space-around" }}> {venueError || categoryError || tagError ?
<TextField label={"Name"} variant="outlined" onChange={handleSearchChange} /> <div>
<DropdownFilter filters={tagData ?? []} setSelectedFilters={setFilteredTags} prefillText={"Tags"} /> There has been an error fetching data!
<DropdownFilter filters={categoryData ?? []} setSelectedFilters={setFilteredCategories} prefillText={"Categories"} /> </div>
</div> :
<VenueGrid venueData={venueData ?? []} /> <div>
<div style={{ display: "flex", justifyContent: "space-around" }}>
<TextField label={"Name"} variant="outlined" onChange={handleSearchChange} />
<DropdownFilter filters={tagData ?? []} setSelectedFilters={setFilteredTags} prefillText={"Tags"} />
<DropdownFilter filters={categoryData ?? []} setSelectedFilters={setFilteredCategories} prefillText={"Categories"} />
</div>
<VenueGrid venueData={venueData ?? []} />
</div>
}
</> </>
); );
}; };

View File

@ -1,4 +1,3 @@
import { expect, jest, describe, it } from '@jest/globals';
import { API } from '../lib/api'; import { API } from '../lib/api';
import { TCategory, TTag } from '../lib/types'; import { TCategory, TTag } from '../lib/types';

View File

@ -0,0 +1,38 @@
// import { expect, jest, describe, it } from '@jest/globals';
import { render } from '@testing-library/react';
import { Index } from '../../pages';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
describe("Given the index page", () => {
it ("should render the page heading", () => {
const { getByText } = render(
<QueryClientProvider client={queryClient} >
<Index/>
</QueryClientProvider>
);
let el = getByText("Leeds Beer Quest");
expect(el).not.toBeNull();
});
it ("should show an error when data fails to load", async () => {
const { findByText } = render(
<QueryClientProvider client={queryClient} >
<Index/>
</QueryClientProvider>
);
let el = await findByText("There has been an error fetching data!");
expect(el).not.toBeNull();
});
});