saleor-apps-redis_apl/apps/emails-and-messages/src/modules/feature-flag-service/feature-flag-service.test.ts
Krzysztof Wolski 6250095a4e
📧 Introduce new event - Gift Card Sent (#661)
* Introduce new event - Gift Card Sent

* Add feature flag service and use it with gift card event

* Add saleor version check on install
2023-06-28 11:39:26 +02:00

48 lines
1.5 KiB
TypeScript

import { it, describe, expect, afterEach, vi } from "vitest";
import { FeatureFlagService } from "./feature-flag-service";
import * as fetchSaleorVersionExports from "./fetch-saleor-version";
import { Client } from "urql";
describe("FeatureFlagService", function () {
const createMockedClient = () => ({} as Client);
afterEach(() => {
vi.resetAllMocks();
});
it("No API calls, when version is passed to the constructor", async () => {
const passedVersion = "3.13.0";
const service = new FeatureFlagService({
client: createMockedClient(),
saleorVersion: passedVersion,
});
const versionFetchSpy = vi
.spyOn(fetchSaleorVersionExports, "fetchSaleorVersion")
.mockResolvedValue("XXXX");
expect(await service.getSaleorVersion()).toEqual(passedVersion);
expect(versionFetchSpy).not.toHaveBeenCalled();
});
it("Use cached version, when once fetched", async () => {
const fetchedVersion = "3.13.0";
const service = new FeatureFlagService({
client: createMockedClient(),
});
const versionFetchSpy = vi
.spyOn(fetchSaleorVersionExports, "fetchSaleorVersion")
.mockResolvedValue(fetchedVersion);
expect(await service.getSaleorVersion()).toEqual(fetchedVersion);
expect(versionFetchSpy).toHaveBeenCalledOnce();
// Request version once again - should be cached
expect(await service.getSaleorVersion()).toEqual(fetchedVersion);
expect(versionFetchSpy).toHaveBeenCalledOnce();
});
});