|
| 1 | +// Render ContentBrowserScreen as a plain function call with hooks stubbed, then check the FlatList it mounts. |
| 2 | +// React reuses the skeleton's native list for the real one, so both must pin removeClippedSubviews to false: |
| 3 | +// leaving it undefined (Android's default) and flipping it later drops every tile on Android TV. |
| 4 | +let mockState: any[] = []; |
| 5 | +jest.mock("react", () => { |
| 6 | + const actual = jest.requireActual("react"); |
| 7 | + let call = 0; |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + useState: (init: any) => { |
| 11 | + const value = call < mockState.length ? mockState[call] : init; |
| 12 | + call++; |
| 13 | + return [value, () => {}]; |
| 14 | + }, |
| 15 | + useRef: (init: any) => ({ current: init }), |
| 16 | + useEffect: () => {}, |
| 17 | + __resetCalls: () => { call = 0; } |
| 18 | + }; |
| 19 | +}); |
| 20 | +jest.mock("react-native", () => ({ |
| 21 | + Dimensions: { get: () => ({ width: 1920, height: 1080 }), addEventListener: () => ({ remove: () => {} }) }, |
| 22 | + Platform: { OS: "android", isTV: true, select: (o: any) => o.android ?? o.default }, |
| 23 | + StyleSheet: { create: (s: any) => s, flatten: (s: any) => s }, |
| 24 | + ActivityIndicator: "ActivityIndicator", |
| 25 | + Image: "Image", |
| 26 | + View: "View", |
| 27 | + FlatList: "FlatList", |
| 28 | + Text: "Text", |
| 29 | + TouchableHighlight: "TouchableHighlight", |
| 30 | + BackHandler: { addEventListener: () => ({ remove: () => {} }) } |
| 31 | +})); |
| 32 | +jest.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); |
| 33 | +jest.mock("react-native-vector-icons/MaterialIcons", () => "Icon"); |
| 34 | +jest.mock("react-native-svg", () => ({ SvgUri: "SvgUri" })); |
| 35 | +jest.mock("../../helpers", () => { |
| 36 | + const styles = jest.requireActual("../../helpers/Styles"); |
| 37 | + return { |
| 38 | + Styles: styles.Styles, |
| 39 | + Colors: styles.Colors, |
| 40 | + Typography: styles.Typography, |
| 41 | + CachedData: { lastFocusedIndex: {}, savedFocusIndex: () => undefined }, |
| 42 | + ProviderAuthHelper: {}, |
| 43 | + ProviderSettingsHelper: {}, |
| 44 | + AnnouncementsHelper: {} |
| 45 | + }; |
| 46 | +}); |
| 47 | +jest.mock("../../components", () => ({ MenuHeader: "MenuHeader", SkeletonCard: "SkeletonCard", EmptyState: "EmptyState" })); |
| 48 | +jest.mock("../../providers", () => ({ getProvider: () => ({ name: "Test Provider", logos: { dark: "" } }) })); |
| 49 | + |
| 50 | +import React from "react"; |
| 51 | +import { ContentBrowserScreen } from "../ContentBrowserScreen"; |
| 52 | + |
| 53 | +function findType(node: any, type: string): any { |
| 54 | + if (!node || typeof node !== "object") return null; |
| 55 | + if (Array.isArray(node)) { for (const n of node) { const f = findType(n, type); if (f) return f; } return null; } |
| 56 | + if (node.type === type) return node; |
| 57 | + return findType(node.props?.children, type); |
| 58 | +} |
| 59 | + |
| 60 | +function render(state: any[]) { |
| 61 | + mockState = state; |
| 62 | + (React as any).__resetCalls(); |
| 63 | + return ContentBrowserScreen({ navigateTo: () => {}, sidebarState: () => {}, providerId: "test" }) as any; |
| 64 | +} |
| 65 | + |
| 66 | +describe("ContentBrowserScreen list on Android TV", () => { |
| 67 | + it("mounts the skeleton list with subview clipping off", () => { |
| 68 | + const list = findType(render([]), "FlatList"); |
| 69 | + expect(list.props.data[0].id).toBe("skeleton-0"); |
| 70 | + expect(list.props.removeClippedSubviews).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("keeps subview clipping off once the items load", () => { |
| 74 | + const items = [{ type: "folder", id: "f1", title: "Folder 1", path: "/f1" }]; |
| 75 | + const list = findType(render([items, false]), "FlatList"); |
| 76 | + expect(list.props.data).toBe(items); |
| 77 | + expect(list.props.removeClippedSubviews).toBe(false); |
| 78 | + }); |
| 79 | +}); |
0 commit comments