|
| 1 | +import { RecyclerViewManager } from "../recyclerview/RecyclerViewManager"; |
| 2 | +import { WarningMessages } from "../errors/WarningMessages"; |
| 3 | +import { FlashListProps } from "../FlashListProps"; |
| 4 | + |
| 5 | +describe("RecyclerViewManager", () => { |
| 6 | + let consoleWarnSpy: jest.SpyInstance; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + consoleWarnSpy.mockRestore(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe("keyExtractor warning with maintainVisibleContentPosition", () => { |
| 17 | + const createMockProps = (overrides = {}) => |
| 18 | + ({ |
| 19 | + data: [{ id: 1 }, { id: 2 }, { id: 3 }], |
| 20 | + renderItem: jest.fn(), |
| 21 | + ...overrides, |
| 22 | + } as FlashListProps<unknown>); |
| 23 | + |
| 24 | + const createManager = (props: FlashListProps<unknown>) => { |
| 25 | + return new RecyclerViewManager(props); |
| 26 | + }; |
| 27 | + |
| 28 | + it("should warn when onStartReached is defined but keyExtractor is not", () => { |
| 29 | + const props = createMockProps({ |
| 30 | + onStartReached: jest.fn(), |
| 31 | + keyExtractor: undefined, |
| 32 | + }); |
| 33 | + |
| 34 | + createManager(props); |
| 35 | + |
| 36 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 37 | + WarningMessages.keyExtractorNotDefinedForMVCP |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should not warn when both onStartReached and keyExtractor are defined", () => { |
| 42 | + const props = createMockProps({ |
| 43 | + onStartReached: jest.fn(), |
| 44 | + keyExtractor: (item: any) => item.id.toString(), |
| 45 | + }); |
| 46 | + |
| 47 | + createManager(props); |
| 48 | + |
| 49 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should not warn when onStartReached is not defined", () => { |
| 53 | + const props = createMockProps({ |
| 54 | + onStartReached: undefined, |
| 55 | + keyExtractor: undefined, |
| 56 | + }); |
| 57 | + |
| 58 | + createManager(props); |
| 59 | + |
| 60 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should not warn when onStartReached is not defined but keyExtractor is", () => { |
| 64 | + const props = createMockProps({ |
| 65 | + onStartReached: undefined, |
| 66 | + keyExtractor: (item: any) => item.id.toString(), |
| 67 | + }); |
| 68 | + |
| 69 | + createManager(props); |
| 70 | + |
| 71 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments