|
| 1 | +import React from "react"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import CheatSheet from "./cheatsheet"; |
| 4 | + |
| 5 | +// Mock the QuickInsert component |
| 6 | +jest.mock("./quickinsert", () => { |
| 7 | + return function QuickInsert(props: any) { |
| 8 | + return ( |
| 9 | + <a |
| 10 | + href="#insert" |
| 11 | + onClick={(e) => { |
| 12 | + e.preventDefault(); |
| 13 | + props.onClick(); |
| 14 | + }} |
| 15 | + data-testid={`quick-insert-${props.source}`} |
| 16 | + > |
| 17 | + {props.source} |
| 18 | + </a> |
| 19 | + ); |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe("CheatSheet", () => { |
| 24 | + const defaultProps = { |
| 25 | + displayCheatSheet: false, |
| 26 | + insertSource: jest.fn(), |
| 27 | + toggleCheatSheet: jest.fn(), |
| 28 | + }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("when cheatsheet is hidden", () => { |
| 35 | + test("shows 'Show Cheatsheet' link", () => { |
| 36 | + render(<CheatSheet {...defaultProps} />); |
| 37 | + const link = screen.getByText(/Show Cheatsheet/i); |
| 38 | + expect(link).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + test("calls toggleCheatSheet when clicked", () => { |
| 42 | + render(<CheatSheet {...defaultProps} />); |
| 43 | + const link = screen.getByText(/Show Cheatsheet/i); |
| 44 | + |
| 45 | + fireEvent.click(link); |
| 46 | + |
| 47 | + expect(defaultProps.toggleCheatSheet).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + |
| 50 | + test("does not show greek letters", () => { |
| 51 | + render(<CheatSheet {...defaultProps} />); |
| 52 | + const greekLettersText = screen.queryByText(/Greek Letters/i); |
| 53 | + expect(greekLettersText).not.toBeInTheDocument(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("when cheatsheet is shown", () => { |
| 58 | + const visibleProps = { ...defaultProps, displayCheatSheet: true }; |
| 59 | + |
| 60 | + test("shows 'Hide Cheatsheet' link", () => { |
| 61 | + render(<CheatSheet {...visibleProps} />); |
| 62 | + const link = screen.getByText(/Hide Cheatsheet/i); |
| 63 | + expect(link).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + test("calls toggleCheatSheet when Hide Cheatsheet is clicked", () => { |
| 67 | + render(<CheatSheet {...visibleProps} />); |
| 68 | + const link = screen.getByText(/Hide Cheatsheet/i); |
| 69 | + |
| 70 | + fireEvent.click(link); |
| 71 | + |
| 72 | + expect(defaultProps.toggleCheatSheet).toHaveBeenCalledTimes(1); |
| 73 | + }); |
| 74 | + |
| 75 | + test("shows Greek Letters section", () => { |
| 76 | + render(<CheatSheet {...visibleProps} />); |
| 77 | + const greekLettersText = screen.getByText(/Greek Letters/i); |
| 78 | + expect(greekLettersText).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + test("shows Symbols section", () => { |
| 82 | + render(<CheatSheet {...visibleProps} />); |
| 83 | + const symbolsText = screen.getByText(/Symbols/i); |
| 84 | + expect(symbolsText).toBeInTheDocument(); |
| 85 | + }); |
| 86 | + |
| 87 | + test("shows Accents section", () => { |
| 88 | + render(<CheatSheet {...visibleProps} />); |
| 89 | + const accentsText = screen.getByText(/Accents/i); |
| 90 | + expect(accentsText).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + test("shows Layout / Common section", () => { |
| 94 | + render(<CheatSheet {...visibleProps} />); |
| 95 | + const layoutText = screen.getByText(/Layout \/ Common/i); |
| 96 | + expect(layoutText).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + test("renders greek letter quick inserts", () => { |
| 100 | + render(<CheatSheet {...visibleProps} />); |
| 101 | + |
| 102 | + const alphaInsert = screen.getByTestId("quick-insert-\\alpha"); |
| 103 | + expect(alphaInsert).toBeInTheDocument(); |
| 104 | + |
| 105 | + const betaInsert = screen.getByTestId("quick-insert-\\beta"); |
| 106 | + expect(betaInsert).toBeInTheDocument(); |
| 107 | + }); |
| 108 | + |
| 109 | + test("calls insertSource when a greek letter is clicked", () => { |
| 110 | + render(<CheatSheet {...visibleProps} />); |
| 111 | + |
| 112 | + const alphaInsert = screen.getByTestId("quick-insert-\\alpha"); |
| 113 | + fireEvent.click(alphaInsert); |
| 114 | + |
| 115 | + expect(defaultProps.insertSource).toHaveBeenCalledWith("\\alpha"); |
| 116 | + }); |
| 117 | + |
| 118 | + test("renders symbol quick inserts", () => { |
| 119 | + render(<CheatSheet {...visibleProps} />); |
| 120 | + |
| 121 | + const infinityInsert = screen.getByTestId("quick-insert-\\infty"); |
| 122 | + expect(infinityInsert).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + test("calls insertSource when a symbol is clicked", () => { |
| 126 | + render(<CheatSheet {...visibleProps} />); |
| 127 | + |
| 128 | + const infinityInsert = screen.getByTestId("quick-insert-\\infty"); |
| 129 | + fireEvent.click(infinityInsert); |
| 130 | + |
| 131 | + expect(defaultProps.insertSource).toHaveBeenCalledWith("\\infty"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("renders accent quick inserts", () => { |
| 135 | + render(<CheatSheet {...visibleProps} />); |
| 136 | + |
| 137 | + const hatInsert = screen.getByTestId("quick-insert-\\hat{x}"); |
| 138 | + expect(hatInsert).toBeInTheDocument(); |
| 139 | + }); |
| 140 | + |
| 141 | + test("calls insertSource when an accent is clicked", () => { |
| 142 | + render(<CheatSheet {...visibleProps} />); |
| 143 | + |
| 144 | + const hatInsert = screen.getByTestId("quick-insert-\\hat{x}"); |
| 145 | + fireEvent.click(hatInsert); |
| 146 | + |
| 147 | + expect(defaultProps.insertSource).toHaveBeenCalledWith("\\hat{x}"); |
| 148 | + }); |
| 149 | + |
| 150 | + test("renders layout/common quick inserts", () => { |
| 151 | + render(<CheatSheet {...visibleProps} />); |
| 152 | + |
| 153 | + const fracInsert = screen.getByTestId("quick-insert-\\frac{a}{b}"); |
| 154 | + expect(fracInsert).toBeInTheDocument(); |
| 155 | + }); |
| 156 | + |
| 157 | + test("calls insertSource when a layout item is clicked", () => { |
| 158 | + render(<CheatSheet {...visibleProps} />); |
| 159 | + |
| 160 | + const fracInsert = screen.getByTestId("quick-insert-\\frac{a}{b}"); |
| 161 | + fireEvent.click(fracInsert); |
| 162 | + |
| 163 | + expect(defaultProps.insertSource).toHaveBeenCalledWith("\\frac{a}{b}"); |
| 164 | + }); |
| 165 | + |
| 166 | + test("renders uppercase greek letters", () => { |
| 167 | + render(<CheatSheet {...visibleProps} />); |
| 168 | + |
| 169 | + const gammaInsert = screen.getByTestId("quick-insert-\\Gamma"); |
| 170 | + expect(gammaInsert).toBeInTheDocument(); |
| 171 | + }); |
| 172 | + |
| 173 | + test("calls insertSource when an uppercase greek letter is clicked", () => { |
| 174 | + render(<CheatSheet {...visibleProps} />); |
| 175 | + |
| 176 | + const gammaInsert = screen.getByTestId("quick-insert-\\Gamma"); |
| 177 | + fireEvent.click(gammaInsert); |
| 178 | + |
| 179 | + expect(defaultProps.insertSource).toHaveBeenCalledWith("\\Gamma"); |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments