|
1 | 1 | import { fireEvent, screen, waitFor } from "@testing-library/react-native"; |
2 | 2 | import { renderWithQueryClient } from "../render-with-query-client"; |
3 | 3 |
|
| 4 | +type MockSearchParams = { |
| 5 | + uri: string; |
| 6 | + title?: string; |
| 7 | + fileName?: string; |
| 8 | + productFileId?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +const defaultSearchParams: MockSearchParams = { |
| 12 | + uri: "https://example.com/test.pdf", |
| 13 | + title: "Test PDF", |
| 14 | + fileName: "HOW TO BECOME AN ELITE PLAYER AND BE BETTER THAN 99%.pdf", |
| 15 | + productFileId: "pf1", |
| 16 | +}; |
| 17 | +let mockSearchParams: MockSearchParams = { ...defaultSearchParams }; |
| 18 | + |
4 | 19 | jest.mock("expo-router", () => ({ |
5 | | - useLocalSearchParams: () => ({ |
6 | | - uri: "https://example.com/test.pdf", |
7 | | - title: "Test PDF", |
8 | | - fileName: "HOW TO BECOME AN ELITE PLAYER AND BE BETTER THAN 99%.pdf", |
9 | | - productFileId: "pf1", |
10 | | - }), |
| 20 | + useLocalSearchParams: () => mockSearchParams, |
11 | 21 | Stack: { |
12 | 22 | Screen: ({ options }: { options?: { headerRight?: () => React.ReactNode } }) => { |
13 | 23 | const React = require("react"); |
@@ -101,6 +111,7 @@ describe("PdfViewerScreen", () => { |
101 | 111 | beforeEach(() => { |
102 | 112 | const { File } = require("expo-file-system"); |
103 | 113 | const Sharing = require("expo-sharing"); |
| 114 | + mockSearchParams = { ...defaultSearchParams }; |
104 | 115 | mockOnError = null; |
105 | 116 | File.downloadFileAsync.mockReset(); |
106 | 117 | File.downloadFileAsync.mockResolvedValue({ uri: "file:///cache/test.pdf" }); |
@@ -207,6 +218,24 @@ describe("PdfViewerScreen", () => { |
207 | 218 | expect(destination.uri).not.toContain("%"); |
208 | 219 | }); |
209 | 220 |
|
| 221 | + it("downloads to a sanitized fallback cache destination when product file id is missing", async () => { |
| 222 | + const { File } = require("expo-file-system"); |
| 223 | + mockSearchParams = { |
| 224 | + uri: "https://example.com/test.pdf", |
| 225 | + title: "Test PDF", |
| 226 | + fileName: "100% bonus #1.pdf", |
| 227 | + }; |
| 228 | + |
| 229 | + renderWithProviders(); |
| 230 | + |
| 231 | + await waitFor(() => expect(screen.getByTestId("pdf-component")).toBeTruthy()); |
| 232 | + |
| 233 | + const destination = File.downloadFileAsync.mock.calls[0][1]; |
| 234 | + expect(destination.uri).toBe("/cache/pdf-viewer/100_ bonus _1.pdf"); |
| 235 | + expect(destination.uri).not.toContain("%"); |
| 236 | + expect(destination.uri).not.toContain("#"); |
| 237 | + }); |
| 238 | + |
210 | 239 | it("shares the cached PDF file when available", async () => { |
211 | 240 | const { File } = require("expo-file-system"); |
212 | 241 | const Sharing = require("expo-sharing"); |
|
0 commit comments