From d6ba427acf6e85e43c2945aa385c28472ecf4e79 Mon Sep 17 00:00:00 2001 From: andrew-cat Date: Fri, 27 Sep 2024 00:10:27 +0200 Subject: [PATCH] add tests --- .../src/stories/MultipleConfigCatConfigs.tsx | 4 +- src/ConfigCatHOC.test.tsx | 15 +++++ src/ConfigCatHooks.test.tsx | 57 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/sandbox/src/stories/MultipleConfigCatConfigs.tsx b/sandbox/src/stories/MultipleConfigCatConfigs.tsx index 272b12f..3cc1f0e 100644 --- a/sandbox/src/stories/MultipleConfigCatConfigs.tsx +++ b/sandbox/src/stories/MultipleConfigCatConfigs.tsx @@ -6,8 +6,8 @@ import { HocComponent} from './Hoc'; const CC_CONFIGID = { BACKEND: "BACKEND", SHARED: "SHARED"}; const CC_SDK = { - BACKEND: "fnvWCKwpfPETf3O6BQgQAg/iMRmhH6DmECovlBvRtjpDw", - SHARED:"configcat-sdk-1/fnvWCKwpfPETf3O6BQgQAg/Idklvfo85EaPWoX9zOygNA"} + BACKEND: "TODO - INSERT SDKKEY", + SHARED:"TODO - INSERT SDKKEY"} const userObject = new User('microFrontendUser1'); diff --git a/src/ConfigCatHOC.test.tsx b/src/ConfigCatHOC.test.tsx index ebc381c..15ae3fd 100644 --- a/src/ConfigCatHOC.test.tsx +++ b/src/ConfigCatHOC.test.tsx @@ -60,3 +60,18 @@ it("withConfigCatClient default settings should work", async () => { render(); await screen.findByText("Feature flag value: Cat", void 0, { timeout: 2000 }); }); + +it("withConfigCatClient with invalid providerId should fail", () => { + const spy = jest.spyOn(console, "error"); + spy.mockImplementation(() => { }); + + const providerId = "PROVIDER_IS_NOT_EXIST"; + + const TestComponent = () => { + const TestHocComponentWithConfigCatClient = withConfigCatClient(TestHOCComponent, providerId); + return (); + }; + expect(() => render()) + .toThrow(`withConfigCatClient must be used in ConfigCatProvider with id="${providerId}"!`); + spy.mockRestore(); +}); diff --git a/src/ConfigCatHooks.test.tsx b/src/ConfigCatHooks.test.tsx index 18716e1..56730f0 100644 --- a/src/ConfigCatHooks.test.tsx +++ b/src/ConfigCatHooks.test.tsx @@ -92,3 +92,60 @@ it("useFeatureFlag Manual poll with forceRefresh should work", async () => { await render(); await screen.findByText("Feature flag value: Cat", void 0, { timeout: 2000 }); }); + +it("useFeatureFlag with invalid providerId should fail", () => { + const spy = jest.spyOn(console, "error"); + spy.mockImplementation(() => { }); + + const providerId = "PROVIDER_IS_NOT_EXIST"; + + const TestComponent = () => { + useFeatureFlag("stringDefaultCat", null, void 0, providerId); + return (
); + }; + expect(() => render()) + .toThrow(`useFeatureFlag must be used in ConfigCatProvider with id="${providerId}"!`); + spy.mockRestore(); +}); + +it("useFeatureFlag with providerId should work", async () => { + + const providerId = "BACKEND"; + + const TestComponent = () => { + const { value: featureFlag } = useFeatureFlag("stringDefaultCat", "NOT_CAT", void 0, providerId); + return (< div>Feature flag value: {featureFlag}
); + }; + await render(); + await screen.findByText("Feature flag value: Cat", void 0, { timeout: 2000 }); +}); + +it("useConfigCatClient with invalid providerId should fail", () => { + const spy = jest.spyOn(console, "error"); + spy.mockImplementation(() => { }); + + const providerId = "PROVIDER_IS_NOT_EXIST"; + + const TestComponent = () => { + useConfigCatClient(providerId); + return (
); + }; + expect(() => render()) + .toThrow(`useConfigCatClient must be used in ConfigCatProvider with id="${providerId}"!`); + spy.mockRestore(); +}); + +it("useConfigCatClient with providerId should work", async () => { + + const providerId = "BACKEND"; + + const TestComponent = () => { + const [stringDefaultCat, setStringDefaultCat] = useState(""); + const client = useConfigCatClient(providerId); + useEffect(() => { client.getValueAsync("stringDefaultCat", "", void 0).then((v) => setStringDefaultCat(v)); }); + + return (< div>Feature flag value: {stringDefaultCat}
); + }; + await render(); + await screen.findByText("Feature flag value: Cat", void 0, { timeout: 2000 }); +}); \ No newline at end of file