-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve interaction test handling, test utils
- Loading branch information
1 parent
6421bc2
commit 8850a55
Showing
3 changed files
with
101 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { action } from "@storybook/addon-actions"; | ||
import type { Meta } from "@storybook/react"; | ||
import { waitFor, within } from "@storybook/testing-library"; | ||
import { expect } from "@storybook/jest"; | ||
import Image from "../components/Image"; | ||
import { createSpyableActions, delay, doneTrackerUtils } from "./common"; | ||
import { ContextualStoryDecorator } from "./StoryWrapper"; | ||
import { action } from "@storybook/addon-actions"; | ||
|
||
const { actions, actionsMockClear } = createSpyableActions({ | ||
onDone: action("done"), | ||
onAbort: action("abort"), | ||
onError: action("error"), | ||
onPending: action("pending"), | ||
}); | ||
|
||
export default { | ||
title: "Contextual API/Image", | ||
component: Image, | ||
decorators: [ | ||
ContextualStoryDecorator({ | ||
onDone: action("done"), | ||
onAbort: action("abort"), | ||
onError: action("error"), | ||
onPending: action("pending"), | ||
}), | ||
ContextualStoryDecorator(actions), | ||
], | ||
} as Meta; | ||
|
||
export const Primary = { | ||
export const Primary: Meta = { | ||
args: { | ||
src: "https://picsum.photos/200/300", | ||
} | ||
}; | ||
|
||
export const InteractionTest: Meta = { | ||
args: { | ||
src: "https://picsum.photos/200/300", | ||
}, | ||
play: async ({ canvasElement }) => { | ||
actionsMockClear(); | ||
|
||
await delay(500); | ||
|
||
const canvas = within(canvasElement); | ||
const { status, refresh } = await doneTrackerUtils(canvas); | ||
|
||
await waitFor(() => expect(status()).toBe("done"), { timeout: 1000 }); | ||
await delay(100); | ||
expect(actions.onDone).toBeCalledTimes(1); | ||
refresh(); | ||
await delay(100); | ||
expect(status()).toBe("done"); | ||
expect(actions.onDone).toBeCalledTimes(2); | ||
} | ||
}; | ||
|
||
export const Error: Meta = { | ||
args: { | ||
src: "https://example.qwkeinasc", | ||
} | ||
}; | ||
|
||
export const Error = { | ||
export const InteractionTestError: Meta = { | ||
args: { | ||
src: "https://example.qwkeinasc", | ||
}, | ||
play: async ({ canvasElement }) => { | ||
await delay(500); | ||
|
||
const canvas = within(canvasElement); | ||
const { status, refresh } = await doneTrackerUtils(canvas); | ||
console.log('b'); | ||
actionsMockClear(); | ||
|
||
await waitFor(() => expect(status()).toBe("error"), { timeout: 1000 }); | ||
refresh(); | ||
await delay(100); | ||
expect(status()).toBe("error"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,36 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import { action as actionFn } from "@storybook/addon-actions"; | ||
import { jest } from "@storybook/jest"; | ||
import { fireEvent } from "@storybook/testing-library"; | ||
|
||
export const actions = { | ||
onDone: action("done"), | ||
onAbort: action("abort"), | ||
onError: action("error"), | ||
onPending: action("pending"), | ||
}; | ||
|
||
export function createSpyableActions() { | ||
export function createSpyableActions< | ||
A extends Record<string, ReturnType<typeof actionFn>> | ||
>(actions: A) { | ||
const spyableActions: Record< | ||
keyof typeof actions, | ||
keyof A, | ||
jest.Mock<void, []> | ||
> = Object.fromEntries( | ||
Object.entries(actions).map(([k, v]) => { | ||
const fn = jest.fn(v); | ||
Object.entries(actions).map(([k, actionFn]) => { | ||
const fn = jest.fn(actionFn); | ||
// set function name for storybook interaction view | ||
Object.defineProperty(fn, "name", { value: k }); | ||
return [k as any, fn] as const; | ||
}) | ||
); | ||
|
||
const actionsMockReset = () => { | ||
Object.values(spyableActions).forEach((action) => action.mockReset()); | ||
const actionsMockClear = () => { | ||
Object.values(spyableActions).forEach((action) => action.mockClear()); | ||
}; | ||
|
||
return { actions: spyableActions, actionsMockReset }; | ||
return { actions: spyableActions, actionsMockClear }; | ||
} | ||
|
||
export async function doneTrackerUtils(canvas: any) { | ||
const stateText = await canvas.findByTestId("root-state"); | ||
const refreshButton = await canvas.findByTestId("new-root-done-tracker"); | ||
return { | ||
status: () => stateText.innerHTML, | ||
refresh: () => fireEvent.click(refreshButton), | ||
}; | ||
} | ||
|
||
export const delay = (n: number) => new Promise((res) => setTimeout(res, n)); |