Skip to content

Commit e0ffb59

Browse files
committed
fix: added test
1 parent fa6003d commit e0ffb59

File tree

1 file changed

+110
-4
lines changed

1 file changed

+110
-4
lines changed

packages/runtime/src/runtime/hooks/__tests__/useInvokeApi.test.tsx

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ const BrowserRouterWrapper = ({ children }: BrowserRouterProps) => (
3737
</QueryClientProvider>
3838
);
3939

40+
const logSpy = jest.spyOn(console, "log").mockImplementation(jest.fn());
41+
jest.spyOn(console, "error").mockImplementation(jest.fn());
42+
4043
beforeEach(() => {
4144
jest.useFakeTimers();
4245
});
4346

4447
afterEach(() => {
48+
logSpy.mockClear();
4549
jest.clearAllMocks();
4650
jest.useRealTimers();
4751
queryClient.clear();
4852
});
4953

5054
test("fetch API cache response for cache expiry", async () => {
51-
const logSpy = jest.spyOn(console, "log");
52-
5355
fetchMock.mockResolvedValue({ body: { data: "foobar" } });
5456

5557
render(
@@ -162,8 +164,6 @@ test("fetch API cache response for unique inputs while cache expiry", async () =
162164
});
163165

164166
test("fetch API without cache", async () => {
165-
const logSpy = jest.spyOn(console, "log");
166-
167167
fetchMock.mockResolvedValue({ body: { data: "foobar" } });
168168

169169
render(
@@ -337,3 +337,109 @@ test("fetch API with force cache clear", async () => {
337337
expect(fetchMock).toHaveBeenCalledTimes(2);
338338
});
339339
});
340+
341+
test("after API fetching using toggle check states", async () => {
342+
fetchMock.mockResolvedValueOnce({ body: { data: "foo" }, isLoading: false });
343+
fetchMock.mockResolvedValueOnce({ body: { data: "bar" }, isLoading: false });
344+
345+
render(
346+
<EnsembleScreen
347+
screen={{
348+
name: "test",
349+
id: "test",
350+
body: {
351+
name: "Column",
352+
properties: {
353+
children: [
354+
{
355+
name: "ToggleButton",
356+
properties: {
357+
id: "toggleButton",
358+
value: "foo",
359+
items: [
360+
{ label: "Foo", value: "foo" },
361+
{ label: "Bar", value: "bar" },
362+
],
363+
onChange: {
364+
executeConditionalAction: {
365+
conditions: [
366+
{
367+
if: `\${value === 'bar'}`,
368+
action: { invokeAPI: { name: "fetchBar" } },
369+
},
370+
],
371+
},
372+
},
373+
},
374+
},
375+
{
376+
name: "Conditional",
377+
properties: {
378+
conditions: [
379+
{
380+
if: `\${toggleButton.value === "foo"}`,
381+
Column: {
382+
children: [
383+
{
384+
LoadingContainer: {
385+
isLoading: `\${fetchFoo.isLoading !== false}`,
386+
widget: {
387+
Text: { text: `\${fetchFoo.body.data}` },
388+
},
389+
},
390+
},
391+
],
392+
},
393+
},
394+
{
395+
elseif: `\${toggleButton.value === "bar"}`,
396+
Column: {
397+
children: [{ Text: { text: `\${fetchFoo.data}` } }],
398+
},
399+
},
400+
],
401+
},
402+
},
403+
{
404+
name: "Button",
405+
properties: {
406+
label: "Verify States",
407+
onTap: { executeCode: "console.log(fetchFoo.isLoading)" },
408+
},
409+
},
410+
],
411+
},
412+
},
413+
apis: [
414+
{ name: "fetchFoo", method: "GET" },
415+
{ name: "fetchBar", method: "GET" },
416+
],
417+
onLoad: { invokeAPI: { name: "fetchFoo" } },
418+
}}
419+
/>,
420+
{
421+
wrapper: BrowserRouterWrapper,
422+
},
423+
);
424+
425+
await waitFor(() => {
426+
expect(fetchMock).toHaveBeenCalledTimes(1);
427+
expect(screen.getByText("Foo")).toBeInTheDocument();
428+
expect(screen.getByText("Bar")).toBeInTheDocument();
429+
});
430+
431+
await waitFor(() => {
432+
fireEvent.click(screen.getByText("Bar"));
433+
expect(fetchMock).toHaveBeenCalledTimes(2);
434+
});
435+
436+
await waitFor(() => {
437+
fireEvent.click(screen.getByText("Foo"));
438+
expect(fetchMock).toHaveBeenCalledTimes(2);
439+
});
440+
441+
await waitFor(() => {
442+
fireEvent.click(screen.getByText("Verify States"));
443+
expect(logSpy).toHaveBeenCalledWith(false);
444+
});
445+
});

0 commit comments

Comments
 (0)