Skip to content

Commit

Permalink
write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acrantel committed Jun 25, 2023
1 parent b6d877f commit 5c2f9a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
9 changes: 0 additions & 9 deletions frontend2/src/__test__/App.test.tsx

This file was deleted.

27 changes: 27 additions & 0 deletions frontend2/src/components/sidebar/__test__/Sidebar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import Sidebar from "../";
import { DEFAULT_EPISODE } from "../../../utils/constants";
import { EpisodeContext } from "../../../contexts/EpisodeContext";
import { MemoryRouter } from "react-router-dom";

test('should link to default episode', () => {
render(<MemoryRouter><Sidebar /></MemoryRouter>);
const linkElement = screen.getByText('Resources').closest('a')?.getAttribute('href');
expect(linkElement).toEqual(expect.stringContaining(`/${DEFAULT_EPISODE}/resources`));
});

test('should collapse sidebar', () => {
render(<MemoryRouter><Sidebar collapsed={true} /></MemoryRouter>);
expect(screen.queryByText('Home')).toBeNull();
});

test('should link to episode in surrounding context', () => {
render(<MemoryRouter>
<EpisodeContext.Provider value={{ episode: "something", setEpisode: (_) => undefined }}>
<Sidebar />
</EpisodeContext.Provider></MemoryRouter>
);
const linkElement = screen.getByText('Resources').closest('a')?.getAttribute('href');
expect(linkElement).toEqual(expect.stringContaining(`/something/resources`));
});
9 changes: 4 additions & 5 deletions frontend2/src/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React from "react";
import React, { useContext } from "react";
import SidebarSection from "./SidebarSection";
import SidebarItem from "./SidebarItem";
import {
ClipboardDocumentIcon, HomeIcon, MapIcon,
TrophyIcon, ChartBarIcon, ClockIcon,
UserGroupIcon, ArrowUpTrayIcon, PlayCircleIcon,
} from "@heroicons/react/24/outline";
import { useParams } from "react-router-dom";
import { DEFAULT_EPISODE } from "../../utils/constants";
import { EpisodeContext } from "../../contexts/EpisodeContext";

interface SidebarProps {
collapsed?: boolean;
Expand All @@ -17,8 +16,8 @@ const Sidebar: React.FC<SidebarProps> = ({
collapsed
}) => {
collapsed = collapsed ?? false;
const { episode } = useParams();
const linkBase = `/${episode ?? DEFAULT_EPISODE}/`;
const { episode } = useContext(EpisodeContext);
const linkBase = `/${episode}/`;

return (
collapsed ? null : (
Expand Down
3 changes: 2 additions & 1 deletion frontend2/src/contexts/EpisodeContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createContext } from "react";
import { DEFAULT_EPISODE } from "../utils/constants";

export const EpisodeContext = createContext({
// the default episode.
episode: "bc23",
episode: DEFAULT_EPISODE,
setEpisode: (episode: string) => {
console.log("default episode");
},
Expand Down

0 comments on commit 5c2f9a8

Please sign in to comment.