Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cat committed Sep 26, 2024
1 parent a6ba8d6 commit d6ba427
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sandbox/src/stories/MultipleConfigCatConfigs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
15 changes: 15 additions & 0 deletions src/ConfigCatHOC.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ it("withConfigCatClient default settings should work", async () => {
render(<ConfigCatProvider sdkKey={sdkKey}><TestComponent /></ConfigCatProvider>);
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 (<TestHocComponentWithConfigCatClient />);
};
expect(() => render(<ConfigCatProvider sdkKey={sdkKey}><TestComponent /></ConfigCatProvider>))
.toThrow(`withConfigCatClient must be used in ConfigCatProvider with id="${providerId}"!`);
spy.mockRestore();
});
57 changes: 57 additions & 0 deletions src/ConfigCatHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,60 @@ it("useFeatureFlag Manual poll with forceRefresh should work", async () => {
await render(<ConfigCatProvider sdkKey={sdkKey} pollingMode={PollingMode.ManualPoll}><TestComponent /></ConfigCatProvider>);
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 (<div />);
};
expect(() => render(<ConfigCatProvider sdkKey={sdkKey}><TestComponent /></ConfigCatProvider>))
.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}</div>);
};
await render(<ConfigCatProvider sdkKey={sdkKey} id={providerId}><TestComponent /></ConfigCatProvider>);
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 (<div />);
};
expect(() => render(<ConfigCatProvider sdkKey={sdkKey}><TestComponent /></ConfigCatProvider>))
.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}</div>);
};
await render(<ConfigCatProvider sdkKey={sdkKey} id={providerId}><TestComponent /></ConfigCatProvider>);
await screen.findByText("Feature flag value: Cat", void 0, { timeout: 2000 });
});

0 comments on commit d6ba427

Please sign in to comment.