|
| 1 | +import { beforeEach, describe, expect, jest, test } from "@jest/globals"; |
| 2 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import { useFoundry } from "./foundry-context"; |
| 4 | +import { useCollectionObject } from "./hooks"; |
| 5 | +import { CollectionReadResponse } from "./types"; |
| 6 | + |
| 7 | +jest.mock("./foundry-context"); |
| 8 | +jest.mock("@crowdstrike/foundry-js"); |
| 9 | + |
| 10 | +describe("useCollectionObject", () => { |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + test("returns object when read succeeds", async () => { |
| 16 | + // mock falcon api collection response |
| 17 | + const mockReadResponse = { special_key: "special_value" }; |
| 18 | + const mockCollection = { |
| 19 | + read: jest |
| 20 | + .fn<() => Promise<CollectionReadResponse>>() |
| 21 | + .mockResolvedValue(mockReadResponse), |
| 22 | + }; |
| 23 | + const mockFalcon = { |
| 24 | + collection: jest.fn().mockReturnValue(mockCollection), |
| 25 | + }; |
| 26 | + |
| 27 | + // mock useFoundry to use mock falcon api |
| 28 | + (useFoundry as jest.Mock).mockReturnValue({ |
| 29 | + isInitialized: true, |
| 30 | + falcon: mockFalcon, |
| 31 | + }); |
| 32 | + |
| 33 | + const { result } = renderHook(() => |
| 34 | + useCollectionObject("test-collection", "test-key") |
| 35 | + ); |
| 36 | + |
| 37 | + // wait for ready |
| 38 | + await waitFor(() => { |
| 39 | + expect(result.current[1]).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + // ensure falcon api called correctly |
| 43 | + expect(mockFalcon.collection).toHaveBeenCalledWith({ |
| 44 | + collection: "test-collection", |
| 45 | + }); |
| 46 | + expect(mockCollection.read).toHaveBeenCalledWith("test-key"); |
| 47 | + |
| 48 | + // ensure response contains value |
| 49 | + expect(result.current[0]).toEqual(mockReadResponse); // value |
| 50 | + expect(result.current[1]).toBe(true); // complete |
| 51 | + expect(result.current[2]).toBe(null); // error |
| 52 | + }); |
| 53 | + |
| 54 | + test("returns error when read fails", async () => { |
| 55 | + // mock falcon api collection response |
| 56 | + const errorMessage = "error_message"; |
| 57 | + const mockReadResponse = { errors: [{ code: 500, message: errorMessage }] }; |
| 58 | + const mockCollection = { |
| 59 | + read: jest |
| 60 | + .fn<() => Promise<CollectionReadResponse>>() |
| 61 | + .mockResolvedValue(mockReadResponse), |
| 62 | + }; |
| 63 | + const mockFalcon = { |
| 64 | + collection: jest.fn().mockReturnValue(mockCollection), |
| 65 | + }; |
| 66 | + |
| 67 | + // mock useFoundry to use mock falcon api |
| 68 | + (useFoundry as jest.Mock).mockReturnValue({ |
| 69 | + isInitialized: true, |
| 70 | + falcon: mockFalcon, |
| 71 | + }); |
| 72 | + |
| 73 | + const { result } = renderHook(() => |
| 74 | + useCollectionObject("test-collection", "test-key") |
| 75 | + ); |
| 76 | + |
| 77 | + // wait for ready |
| 78 | + await waitFor(() => { |
| 79 | + expect(result.current[1]).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + // ensure response contains value |
| 83 | + expect(result.current[0]).toEqual(null); // value |
| 84 | + expect(result.current[1]).toBe(true); // complete |
| 85 | + expect(result.current[2]).toBe(errorMessage); // error |
| 86 | + }); |
| 87 | +}); |
0 commit comments