|
| 1 | +import React from "react"; |
| 2 | +import { render, waitFor, act, fireEvent } from "@testing-library/react-native"; |
| 3 | + |
| 4 | + |
| 5 | +// 1) Navigation hook mock |
| 6 | +const mockNavigate = jest.fn(); |
| 7 | + |
| 8 | +jest.mock("@react-navigation/native", () => ({ |
| 9 | + useNavigation: () => ({ navigate: mockNavigate }), |
| 10 | +})); |
| 11 | + |
| 12 | + |
| 13 | +// 2) Mock Alert to avoid noisy test failures |
| 14 | +jest.mock("react-native/Libraries/Alert/Alert", () => ({ |
| 15 | + alert: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +// 3) Mock SearchBar / CategoryFilter / HeroBanner (чтобы не зависеть от их UI) |
| 19 | +jest.mock("../utils/SearchBar", () => { |
| 20 | + const React = require("react"); |
| 21 | + const { View, Pressable, Text } = require("react-native"); |
| 22 | + return ({ onSearch }) => ( |
| 23 | + <View> |
| 24 | + {/* “Кнопка” для теста поиска */} |
| 25 | + <Pressable testID="mock-search" onPress={() => onSearch("salad")}> |
| 26 | + <Text>MockSearch</Text> |
| 27 | + </Pressable> |
| 28 | + </View> |
| 29 | + ); |
| 30 | +}); |
| 31 | + |
| 32 | +jest.mock("../utils/CategoryFilter", () => { |
| 33 | + const React = require("react"); |
| 34 | + const { View, Pressable, Text } = require("react-native"); |
| 35 | + return ({ onSelect }) => ( |
| 36 | + <View> |
| 37 | + {/* “Кнопка” для теста категории */} |
| 38 | + <Pressable testID="mock-category" onPress={() => onSelect("starters")}> |
| 39 | + <Text>MockCategory</Text> |
| 40 | + </Pressable> |
| 41 | + </View> |
| 42 | + ); |
| 43 | +}); |
| 44 | + |
| 45 | +jest.mock("../utils/HeroBanner", () => { |
| 46 | + const React = require("react"); |
| 47 | + const { View, Text } = require("react-native"); |
| 48 | + return () => ( |
| 49 | + <View> |
| 50 | + <Text>HeroBanner</Text> |
| 51 | + </View> |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +// 4) Mock database module |
| 56 | +jest.mock("../database/database", () => ({ |
| 57 | + initDatabase: jest.fn(), |
| 58 | + saveMenuItems: jest.fn(), |
| 59 | + getMenuItems: jest.fn(), |
| 60 | + filterByQueryAndCategories: jest.fn(), |
| 61 | +})); |
| 62 | + |
| 63 | +import HomeScreen from "../screens/Home"; |
| 64 | +import { |
| 65 | + initDatabase, |
| 66 | + saveMenuItems, |
| 67 | + getMenuItems, |
| 68 | + filterByQueryAndCategories, |
| 69 | +} from "../database/database"; |
| 70 | + |
| 71 | +describe("HomeScreen", () => { |
| 72 | + beforeEach(() => { |
| 73 | + jest.clearAllMocks(); |
| 74 | + mockNavigate.mockReset(); |
| 75 | + }); |
| 76 | + |
| 77 | + test("renders items from DB when getMenuItems returns data", async () => { |
| 78 | + initDatabase.mockResolvedValueOnce(); |
| 79 | + getMenuItems.mockResolvedValueOnce([ |
| 80 | + { |
| 81 | + id: 1, |
| 82 | + name: "Greek Salad", |
| 83 | + description: "Fresh and delicious", |
| 84 | + price: 12.99, |
| 85 | + category: "starters", |
| 86 | + image: "https://example.com/salad.png", |
| 87 | + }, |
| 88 | + ]); |
| 89 | + |
| 90 | + const { queryByText, findByText } = render( |
| 91 | + <HomeScreen route={{ params: {} }} /> |
| 92 | + ); |
| 93 | + |
| 94 | + // item appears |
| 95 | + expect(await findByText("Greek Salad")).toBeTruthy(); |
| 96 | + |
| 97 | + // empty state should NOT be visible once items exist |
| 98 | + expect(queryByText("No items found matching your criteria")).toBeNull(); |
| 99 | + |
| 100 | + // fetch не нужен, потому что DB не пустая |
| 101 | + expect(saveMenuItems).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + test("fetches remote menu and saves to DB when DB is empty", async () => { |
| 105 | + initDatabase.mockResolvedValueOnce(); |
| 106 | + getMenuItems.mockResolvedValueOnce([]); // DB empty => should fetch |
| 107 | + |
| 108 | + global.fetch = jest.fn(async () => ({ |
| 109 | + json: async () => ({ |
| 110 | + menu: [ |
| 111 | + { |
| 112 | + id: 10, |
| 113 | + name: "Lemon Dessert", |
| 114 | + description: "Sweet", |
| 115 | + price: 7.5, |
| 116 | + category: "desserts", |
| 117 | + image: "lemon-dessert.jpg", |
| 118 | + }, |
| 119 | + ], |
| 120 | + }), |
| 121 | + })); |
| 122 | + |
| 123 | + const { findByText } = render(<HomeScreen route={{ params: {} }} />); |
| 124 | + |
| 125 | + // after fetch+save, item appears |
| 126 | + expect(await findByText("Lemon Dessert")).toBeTruthy(); |
| 127 | + |
| 128 | + // saveMenuItems should have been called with mapped image urls |
| 129 | + await waitFor(() => { |
| 130 | + expect(saveMenuItems).toHaveBeenCalledTimes(1); |
| 131 | + const arg = saveMenuItems.mock.calls[0][0]; |
| 132 | + expect(arg[0].name).toBe("Lemon Dessert"); |
| 133 | + expect(String(arg[0].image)).toContain("raw=true"); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + test("applies filtering after debounce (500ms) when search/category changes", async () => { |
| 138 | + jest.useFakeTimers(); |
| 139 | + |
| 140 | + initDatabase.mockResolvedValueOnce(); |
| 141 | + // initial load from DB |
| 142 | + getMenuItems.mockResolvedValueOnce([ |
| 143 | + { |
| 144 | + id: 1, |
| 145 | + name: "Greek Salad", |
| 146 | + description: "Fresh", |
| 147 | + price: 12.99, |
| 148 | + category: "starters", |
| 149 | + image: "https://example.com/salad.png", |
| 150 | + }, |
| 151 | + { |
| 152 | + id: 2, |
| 153 | + name: "Burger", |
| 154 | + description: "Big", |
| 155 | + price: 15.0, |
| 156 | + category: "mains", |
| 157 | + image: "https://example.com/burger.png", |
| 158 | + }, |
| 159 | + ]); |
| 160 | + |
| 161 | + // filtered result when user searches “salad” and selects "starters" |
| 162 | + filterByQueryAndCategories.mockResolvedValueOnce([ |
| 163 | + { |
| 164 | + id: 1, |
| 165 | + name: "Greek Salad", |
| 166 | + description: "Fresh", |
| 167 | + price: 12.99, |
| 168 | + category: "starters", |
| 169 | + image: "https://example.com/salad.png", |
| 170 | + }, |
| 171 | + ]); |
| 172 | + |
| 173 | + const { getByTestId, queryByText, findByText } = render( |
| 174 | + <HomeScreen route={{ params: {} }} /> |
| 175 | + ); |
| 176 | + |
| 177 | + // initial items appear |
| 178 | + expect(await findByText("Greek Salad")).toBeTruthy(); |
| 179 | + expect(queryByText("Burger")).toBeTruthy(); |
| 180 | + |
| 181 | + // trigger search + category through mocked components |
| 182 | + act(() => { |
| 183 | + fireEvent.press(getByTestId("mock-search")); |
| 184 | + fireEvent.press(getByTestId("mock-category")); |
| 185 | + }); |
| 186 | + |
| 187 | + |
| 188 | + // advance debounce |
| 189 | + await act(async () => { |
| 190 | + jest.advanceTimersByTime(500); |
| 191 | + }); |
| 192 | + |
| 193 | + await waitFor(() => { |
| 194 | + expect(filterByQueryAndCategories).toHaveBeenCalled(); |
| 195 | + }); |
| 196 | + |
| 197 | + // after filter, burger should disappear, salad remains |
| 198 | + await waitFor(() => { |
| 199 | + expect(queryByText("Greek Salad")).toBeTruthy(); |
| 200 | + expect(queryByText("Burger")).toBeNull(); |
| 201 | + }); |
| 202 | + |
| 203 | + jest.useRealTimers(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments