|
| 1 | +import type { UID } from "@strapi/strapi"; |
| 2 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 3 | +import { getPopulateQuery, queryCache } from "../getPopulateQuery"; |
| 4 | + |
| 5 | +describe("getPopulateQuery: cache", () => { |
| 6 | + const mockStrapi = { |
| 7 | + log: { debug: vi.fn() }, |
| 8 | + getModel: vi.fn(), |
| 9 | + plugin: vi.fn().mockReturnValue({ config: vi.fn() }), |
| 10 | + }; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + // @ts-expect-error enough to mock |
| 14 | + global.strapi = mockStrapi; |
| 15 | + mockStrapi.log.debug.mockClear(); |
| 16 | + mockStrapi.getModel.mockClear(); |
| 17 | + mockStrapi.plugin.mockClear(); |
| 18 | + |
| 19 | + // reset cache |
| 20 | + Object.keys(queryCache).forEach((key) => { |
| 21 | + delete queryCache[key]; |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + test("uses cache by default", () => { |
| 26 | + mockStrapi.getModel.mockReturnValue({ |
| 27 | + attributes: { media: { type: "media" } }, |
| 28 | + }); |
| 29 | + |
| 30 | + getPopulateQuery("model" as UID.Schema); |
| 31 | + |
| 32 | + expect(queryCache).toEqual({ model: { populate: { media: true } } }); |
| 33 | + }); |
| 34 | + |
| 35 | + test("uses cache if enabled", () => { |
| 36 | + mockStrapi.plugin.mockReturnValue({ |
| 37 | + config: vi |
| 38 | + .fn() |
| 39 | + .mockImplementation((key) => (key === "cache" ? true : undefined)), |
| 40 | + }); |
| 41 | + mockStrapi.getModel.mockReturnValue({ |
| 42 | + attributes: { media: { type: "media" } }, |
| 43 | + }); |
| 44 | + |
| 45 | + getPopulateQuery("model" as UID.Schema); |
| 46 | + |
| 47 | + expect(queryCache).toEqual({ model: { populate: { media: true } } }); |
| 48 | + }); |
| 49 | + |
| 50 | + test("skips cache if disabled", () => { |
| 51 | + mockStrapi.plugin.mockReturnValue({ |
| 52 | + config: vi |
| 53 | + .fn() |
| 54 | + .mockImplementation((key) => (key === "cache" ? false : undefined)), |
| 55 | + }); |
| 56 | + mockStrapi.getModel.mockReturnValue({ |
| 57 | + attributes: { media: { type: "media" } }, |
| 58 | + }); |
| 59 | + |
| 60 | + getPopulateQuery("model" as UID.Schema); |
| 61 | + |
| 62 | + expect(queryCache).toEqual({}); |
| 63 | + }); |
| 64 | + |
| 65 | + test("cache does not mutate original object", () => { |
| 66 | + mockStrapi.plugin.mockReturnValue({ |
| 67 | + config: vi |
| 68 | + .fn() |
| 69 | + .mockImplementation((key) => (key === "cache" ? true : undefined)), |
| 70 | + }); |
| 71 | + mockStrapi.getModel.mockReturnValue({ |
| 72 | + attributes: { |
| 73 | + media: { type: "media" }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const result = getPopulateQuery("model" as UID.Schema); |
| 78 | + // @ts-expect-error: simulating race condition where `queryCache` gets overwritten by other call |
| 79 | + queryCache.model.populate.media = false; |
| 80 | + |
| 81 | + expect(result).toEqual({ |
| 82 | + populate: { media: true }, |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments