|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | 2 | import { render } from "vitest-browser-react"; |
3 | 3 | import { I18nProvider } from "../../i18n/I18nProvider"; |
| 4 | +import { |
| 5 | + DIRECTION_NEXT, |
| 6 | + DIRECTION_PREVIOUS, |
| 7 | + type DocumentNavigationContextValue, |
| 8 | +} from "../../navigation/DocumentNavigationContext"; |
| 9 | +import { TestDocumentNavigationContextProvider } from "../../navigation/TestDocumentNavigationContextProvider"; |
4 | 10 | import { UnitexAnnotation } from "./UnitexAnnotation"; |
5 | 11 |
|
6 | | -function TestWrapper({ children }: { children: React.ReactNode }) { |
7 | | - return <I18nProvider>{children}</I18nProvider>; |
| 12 | +function TestWrapper({ |
| 13 | + navigateToBodyTargetSelector, |
| 14 | + children, |
| 15 | +}: { |
| 16 | + navigateToBodyTargetSelector?: DocumentNavigationContextValue["navigateToBodyTargetSelector"]; |
| 17 | + children: React.ReactNode; |
| 18 | +}) { |
| 19 | + return ( |
| 20 | + <TestDocumentNavigationContextProvider |
| 21 | + value={{ |
| 22 | + navigateToBodyTargetSelector, |
| 23 | + }} |
| 24 | + > |
| 25 | + <I18nProvider>{children}</I18nProvider> |
| 26 | + </TestDocumentNavigationContextProvider> |
| 27 | + ); |
8 | 28 | } |
9 | 29 |
|
10 | 30 | describe("UnitexAnnotation", () => { |
@@ -46,4 +66,69 @@ describe("UnitexAnnotation", () => { |
46 | 66 |
|
47 | 67 | expect(onToggle).toHaveBeenCalledTimes(1); |
48 | 68 | }); |
| 69 | + |
| 70 | + it("should disable navigation buttons when annotation is not displayed", async () => { |
| 71 | + const screen = await render( |
| 72 | + <UnitexAnnotation |
| 73 | + annotation={{ term: "example", frequency: 5, displayed: false }} |
| 74 | + color="blue" |
| 75 | + onToggle={() => {}} |
| 76 | + />, |
| 77 | + { |
| 78 | + wrapper: TestWrapper, |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + const previousButton = screen.getByRole("button", { |
| 83 | + name: "Aller au précédent", |
| 84 | + }); |
| 85 | + const nextButton = screen.getByRole("button", { |
| 86 | + name: "Aller au suivant", |
| 87 | + }); |
| 88 | + |
| 89 | + await expect.element(previousButton).toBeDisabled(); |
| 90 | + await expect.element(nextButton).toBeDisabled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should call navigateToBodyTargetSelector when navigation buttons are clicked", async () => { |
| 94 | + const navigateToBodyTargetSelector = vi.fn(); |
| 95 | + const screen = await render( |
| 96 | + <UnitexAnnotation |
| 97 | + annotation={{ term: "example", frequency: 5, displayed: true }} |
| 98 | + color="blue" |
| 99 | + onToggle={() => {}} |
| 100 | + />, |
| 101 | + { |
| 102 | + wrapper: ({ children }) => ( |
| 103 | + <TestWrapper |
| 104 | + navigateToBodyTargetSelector={navigateToBodyTargetSelector} |
| 105 | + > |
| 106 | + {children} |
| 107 | + </TestWrapper> |
| 108 | + ), |
| 109 | + }, |
| 110 | + ); |
| 111 | + |
| 112 | + const previousButton = screen.getByRole("button", { |
| 113 | + name: "Aller au précédent", |
| 114 | + }); |
| 115 | + const nextButton = screen.getByRole("button", { |
| 116 | + name: "Aller au suivant", |
| 117 | + }); |
| 118 | + |
| 119 | + await expect.element(previousButton).toBeEnabled(); |
| 120 | + await expect.element(nextButton).toBeEnabled(); |
| 121 | + |
| 122 | + await previousButton.click(); |
| 123 | + expect(navigateToBodyTargetSelector).toHaveBeenCalledWith( |
| 124 | + '[data-term="example"]', |
| 125 | + DIRECTION_PREVIOUS, |
| 126 | + ); |
| 127 | + |
| 128 | + await nextButton.click(); |
| 129 | + expect(navigateToBodyTargetSelector).toHaveBeenCalledWith( |
| 130 | + '[data-term="example"]', |
| 131 | + DIRECTION_NEXT, |
| 132 | + ); |
| 133 | + }); |
49 | 134 | }); |
0 commit comments