|
| 1 | +import collectionFixture from "../fixtures/opensea/get-collection.json"; |
| 2 | + |
| 3 | +// Mock the fetch function |
| 4 | +global.fetch = jest.fn(); |
| 5 | + |
| 6 | +jest.mock("../../src/utils/logger", () => { |
| 7 | + const base = { |
| 8 | + debug: jest.fn(), |
| 9 | + info: jest.fn(), |
| 10 | + warn: jest.fn(), |
| 11 | + error: jest.fn(), |
| 12 | + }; |
| 13 | + return { |
| 14 | + logger: base, |
| 15 | + prefixedLogger: () => base, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +describe("fetchCollection", () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + // Clear the module cache to reset LRU cache |
| 23 | + jest.resetModules(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should fetch collection data successfully", async () => { |
| 27 | + // Re-import to get fresh cache |
| 28 | + const { fetchCollection: freshFetchCollection } = await import( |
| 29 | + "../../src/opensea" |
| 30 | + ); |
| 31 | + |
| 32 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 33 | + ok: true, |
| 34 | + json: async () => collectionFixture, |
| 35 | + }); |
| 36 | + |
| 37 | + const result = await freshFetchCollection("glyphbots"); |
| 38 | + |
| 39 | + expect(result).toEqual(collectionFixture); |
| 40 | + expect(result?.image_url).toBe( |
| 41 | + "https://i2c.seadn.io/collection/glyphbots/image_type_logo/eb2761fda8e04533a74e6477a35ab0/20eb2761fda8e04533a74e6477a35ab0.png" |
| 42 | + ); |
| 43 | + expect(result?.collection).toBe("glyphbots"); |
| 44 | + expect(result?.name).toBe("GlyphBots"); |
| 45 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 46 | + expect(global.fetch).toHaveBeenCalledWith( |
| 47 | + "https://api.opensea.io/api/v2/collections/glyphbots", |
| 48 | + expect.objectContaining({ |
| 49 | + method: "GET", |
| 50 | + headers: expect.objectContaining({ |
| 51 | + Accept: "application/json", |
| 52 | + }), |
| 53 | + }) |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should cache collection data and return cached result on second call", async () => { |
| 58 | + // Re-import to get fresh cache |
| 59 | + const { fetchCollection: freshFetchCollection } = await import( |
| 60 | + "../../src/opensea" |
| 61 | + ); |
| 62 | + |
| 63 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 64 | + ok: true, |
| 65 | + json: async () => collectionFixture, |
| 66 | + }); |
| 67 | + |
| 68 | + const firstResult = await freshFetchCollection("glyphbots"); |
| 69 | + expect(firstResult).toEqual(collectionFixture); |
| 70 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 71 | + |
| 72 | + // Second call should use cache |
| 73 | + const secondResult = await freshFetchCollection("glyphbots"); |
| 74 | + expect(secondResult).toEqual(collectionFixture); |
| 75 | + // Fetch should still only be called once due to caching |
| 76 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should return undefined when API request fails", async () => { |
| 80 | + // Re-import to get fresh cache |
| 81 | + const { fetchCollection: freshFetchCollection } = await import( |
| 82 | + "../../src/opensea" |
| 83 | + ); |
| 84 | + |
| 85 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 86 | + ok: false, |
| 87 | + status: 404, |
| 88 | + statusText: "Not Found", |
| 89 | + text: async () => "Not Found", |
| 90 | + }); |
| 91 | + |
| 92 | + const result = await freshFetchCollection("nonexistent"); |
| 93 | + |
| 94 | + expect(result).toBeUndefined(); |
| 95 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle fetch errors gracefully", async () => { |
| 99 | + // Re-import to get fresh cache |
| 100 | + const { fetchCollection: freshFetchCollection } = await import( |
| 101 | + "../../src/opensea" |
| 102 | + ); |
| 103 | + |
| 104 | + (global.fetch as jest.Mock).mockRejectedValueOnce( |
| 105 | + new Error("Network error") |
| 106 | + ); |
| 107 | + |
| 108 | + const result = await freshFetchCollection("glyphbots"); |
| 109 | + |
| 110 | + expect(result).toBeUndefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should include image_url field for Discord embeds", async () => { |
| 114 | + // Re-import to get fresh cache |
| 115 | + const { fetchCollection: freshFetchCollection } = await import( |
| 116 | + "../../src/opensea" |
| 117 | + ); |
| 118 | + |
| 119 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 120 | + ok: true, |
| 121 | + json: async () => collectionFixture, |
| 122 | + }); |
| 123 | + |
| 124 | + const result = await freshFetchCollection("glyphbots"); |
| 125 | + |
| 126 | + expect(result?.image_url).toBeDefined(); |
| 127 | + expect(typeof result?.image_url).toBe("string"); |
| 128 | + expect(result?.image_url).toContain("seadn.io"); |
| 129 | + }); |
| 130 | +}); |
0 commit comments