|
| 1 | +// @ts-nocheck |
| 2 | +// ai written test |
| 3 | +import { render, waitFor } from "@testing-library/react"; |
| 4 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 5 | +import { useShikiHighlighter } from "../hook"; |
| 6 | +import { createFineGrainedBundle } from "../bundle"; |
| 7 | +import React from "react"; |
| 8 | +import type { HighlighterOptions } from "../types"; |
| 9 | + |
| 10 | +// Mock the bundle module |
| 11 | +vi.mock("../bundle", () => ({ |
| 12 | + createFineGrainedBundle: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +// Mock the toJsxRuntime function to avoid full rendering |
| 16 | +vi.mock("hast-util-to-jsx-runtime", () => ({ |
| 17 | + toJsxRuntime: vi |
| 18 | + .fn() |
| 19 | + .mockImplementation(() => <div data-testid="mocked-hast">Mocked HAST</div>), |
| 20 | +})); |
| 21 | + |
| 22 | +// Helper component for testing useShikiHighlighter hook |
| 23 | +const TestComponent = ({ |
| 24 | + code, |
| 25 | + language, |
| 26 | + theme, |
| 27 | + options, |
| 28 | +}: { |
| 29 | + code: string; |
| 30 | + language: any; |
| 31 | + theme: any; |
| 32 | + options?: HighlighterOptions; |
| 33 | +}) => { |
| 34 | + const highlighted = useShikiHighlighter(code, language, theme, options); |
| 35 | + return <div data-testid="test-component">{highlighted}</div>; |
| 36 | +}; |
| 37 | + |
| 38 | +describe("Fine-grained bundle", () => { |
| 39 | + const mockCodeToHast = vi |
| 40 | + .fn() |
| 41 | + .mockResolvedValue({ type: "element", tagName: "div" }); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + vi.resetAllMocks(); |
| 45 | + // Mock the bundle with a predefined codeToHast function |
| 46 | + (createFineGrainedBundle as any).mockResolvedValue({ |
| 47 | + codeToHast: mockCodeToHast, |
| 48 | + bundledLanguages: {}, |
| 49 | + bundledThemes: {}, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should use createCodegenBundle when fineGrainedBundle is provided", async () => { |
| 54 | + const code = 'const test = "Hello";'; |
| 55 | + const fineGrainedBundle = { |
| 56 | + langs: ["typescript"], |
| 57 | + themes: ["github-dark"], |
| 58 | + engine: "javascript" as const, |
| 59 | + }; |
| 60 | + |
| 61 | + render( |
| 62 | + <TestComponent |
| 63 | + code={code} |
| 64 | + language="typescript" |
| 65 | + theme="github-dark" |
| 66 | + options={{ fineGrainedBundle }} |
| 67 | + /> |
| 68 | + ); |
| 69 | + |
| 70 | + // Wait for the asynchronous operations to complete |
| 71 | + await waitFor(() => { |
| 72 | + // Include precompiled: false when checking call arguments |
| 73 | + expect(createFineGrainedBundle).toHaveBeenCalledWith({ |
| 74 | + ...fineGrainedBundle, |
| 75 | + precompiled: false, |
| 76 | + }); |
| 77 | + expect(mockCodeToHast).toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should cache the bundle for the same configuration", async () => { |
| 82 | + const code = 'const test = "Hello";'; |
| 83 | + const fineGrainedBundle = { |
| 84 | + langs: ["typescript"], |
| 85 | + themes: ["github-dark"], |
| 86 | + engine: "javascript" as const, |
| 87 | + }; |
| 88 | + |
| 89 | + // Render with the same config twice |
| 90 | + const { rerender } = render( |
| 91 | + <TestComponent |
| 92 | + code={code} |
| 93 | + language="typescript" |
| 94 | + theme="github-dark" |
| 95 | + options={{ fineGrainedBundle }} |
| 96 | + /> |
| 97 | + ); |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + // Re-render with same config |
| 104 | + rerender( |
| 105 | + <TestComponent |
| 106 | + code={code + "// new comment"} |
| 107 | + language="typescript" |
| 108 | + theme="github-dark" |
| 109 | + options={{ fineGrainedBundle }} |
| 110 | + /> |
| 111 | + ); |
| 112 | + |
| 113 | + // Should not create a new bundle |
| 114 | + await waitFor(() => { |
| 115 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should create a new bundle when configuration changes", async () => { |
| 120 | + const code = 'const test = "Hello";'; |
| 121 | + const fineGrainedBundle1 = { |
| 122 | + langs: ["typescript"], |
| 123 | + themes: ["github-dark"], |
| 124 | + engine: "javascript" as const, |
| 125 | + }; |
| 126 | + |
| 127 | + const fineGrainedBundle2 = { |
| 128 | + langs: ["typescript", "javascript"], |
| 129 | + themes: ["github-dark", "github-light"], |
| 130 | + engine: "javascript" as const, |
| 131 | + }; |
| 132 | + |
| 133 | + // Render with first config |
| 134 | + const { rerender } = render( |
| 135 | + <TestComponent |
| 136 | + code={code} |
| 137 | + language="typescript" |
| 138 | + theme="github-dark" |
| 139 | + options={{ fineGrainedBundle: fineGrainedBundle1 }} |
| 140 | + /> |
| 141 | + ); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + // Include precompiled: false when checking call arguments |
| 145 | + expect(createFineGrainedBundle).toHaveBeenCalledWith({ |
| 146 | + ...fineGrainedBundle1, |
| 147 | + precompiled: false, |
| 148 | + }); |
| 149 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(1); |
| 150 | + }); |
| 151 | + |
| 152 | + // Re-render with different config |
| 153 | + rerender( |
| 154 | + <TestComponent |
| 155 | + code={code} |
| 156 | + language="typescript" |
| 157 | + theme="github-dark" |
| 158 | + options={{ fineGrainedBundle: fineGrainedBundle2 }} |
| 159 | + /> |
| 160 | + ); |
| 161 | + |
| 162 | + // Should create a new bundle |
| 163 | + await waitFor(() => { |
| 164 | + // Include precompiled: false when checking call arguments |
| 165 | + expect(createFineGrainedBundle).toHaveBeenCalledWith({ |
| 166 | + ...fineGrainedBundle2, |
| 167 | + precompiled: false, |
| 168 | + }); |
| 169 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(2); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it("should create deterministic cache keys regardless of order", async () => { |
| 174 | + const code = 'const test = "Hello";'; |
| 175 | + |
| 176 | + // Same configuration with different order |
| 177 | + const fineGrainedBundle1 = { |
| 178 | + langs: ["typescript", "javascript"], |
| 179 | + themes: ["github-dark", "github-light"], |
| 180 | + engine: "javascript" as const, |
| 181 | + precompiled: false, |
| 182 | + }; |
| 183 | + |
| 184 | + const fineGrainedBundle2 = { |
| 185 | + langs: ["javascript", "typescript"], |
| 186 | + themes: ["github-light", "github-dark"], |
| 187 | + engine: "javascript" as const, |
| 188 | + precompiled: false, |
| 189 | + }; |
| 190 | + |
| 191 | + // Render with first config |
| 192 | + const { rerender } = render( |
| 193 | + <TestComponent |
| 194 | + code={code} |
| 195 | + language="typescript" |
| 196 | + theme="github-dark" |
| 197 | + options={{ fineGrainedBundle: fineGrainedBundle1 }} |
| 198 | + /> |
| 199 | + ); |
| 200 | + |
| 201 | + await waitFor(() => { |
| 202 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(1); |
| 203 | + }); |
| 204 | + |
| 205 | + // Re-render with same config but different order |
| 206 | + rerender( |
| 207 | + <TestComponent |
| 208 | + code={code} |
| 209 | + language="typescript" |
| 210 | + theme="github-dark" |
| 211 | + options={{ fineGrainedBundle: fineGrainedBundle2 }} |
| 212 | + /> |
| 213 | + ); |
| 214 | + |
| 215 | + // Should not create a new bundle |
| 216 | + await waitFor(() => { |
| 217 | + expect(createFineGrainedBundle).toHaveBeenCalledTimes(1); |
| 218 | + }); |
| 219 | + }); |
| 220 | +}); |
0 commit comments