-
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.
- Loading branch information
1 parent
26a55c6
commit 6421bc2
Showing
6 changed files
with
2,133 additions
and
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Storybook Tests | ||
on: deployment_status | ||
jobs: | ||
test: | ||
timeout-minutes: 60 | ||
runs-on: ubuntu-latest | ||
if: github.event.deployment_status.state == 'success' | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: "18.x" | ||
- run: yarn | ||
- run: yarn build | ||
- run: yarn test | ||
env: | ||
TARGET_URL: "${{ github.event.deployment_status.target_url }}" |
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,19 +1,71 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import type { Meta } from "@storybook/react"; | ||
import { within, fireEvent } from "@storybook/testing-library"; | ||
import { expect } from "@storybook/jest"; | ||
import Button from "../components/Button"; | ||
import { ContextualStoryDecorator } from "./StoryWrapper"; | ||
import { createSpyableActions } from "./common"; | ||
|
||
const { actions, actionsMockReset } = createSpyableActions(); | ||
|
||
export default { | ||
title: "Contextual API/Button", | ||
component: Button, | ||
decorators: [ | ||
ContextualStoryDecorator({ | ||
onDone: action("done"), | ||
onAbort: action("abort"), | ||
onError: action("error"), | ||
onPending: action("pending"), | ||
}), | ||
], | ||
decorators: [ContextualStoryDecorator(actions)], | ||
} as Meta; | ||
|
||
export const Primary = { args: { children: "Click me", persistDone: false } }; | ||
export const Primary: Meta = { | ||
args: { children: "Click me", persistDone: false }, | ||
}; | ||
|
||
export const InteractionTestNotPersisted: Meta = { | ||
args: { children: "Click me", persistDone: false }, | ||
play: async ({ canvasElement }) => { | ||
await Promise.resolve(); | ||
|
||
actionsMockReset(); | ||
|
||
const canvas = within(canvasElement); | ||
const button = canvas.getByText("Click me", { selector: "button" }); | ||
fireEvent.click(button); | ||
const text = canvas.getByTestId("root-state"); | ||
expect(text.innerText).toBe("pending"); | ||
await Promise.resolve(); | ||
// it is resolved in 1 microtask | ||
expect(text.innerText).toBe("done"); | ||
const refreshButton = canvas.getByTestId("new-root-done-tracker"); | ||
fireEvent.click(refreshButton); | ||
await Promise.resolve(); | ||
expect(text.innerText).toBe("pending"); | ||
fireEvent.click(button); | ||
expect(text.innerText).toBe("pending"); | ||
await Promise.resolve(); | ||
expect(text.innerText).toBe("done"); | ||
}, | ||
}; | ||
|
||
export const InteractionTestPersisted: Meta = { | ||
args: { children: "Click me", persistDone: true }, | ||
play: async ({ canvasElement }) => { | ||
await Promise.resolve(); | ||
|
||
actionsMockReset(); | ||
|
||
const canvas = within(canvasElement); | ||
const text = await canvas.findByTestId("root-state"); | ||
const refreshButton = await canvas.findByTestId("new-root-done-tracker"); | ||
|
||
const button = canvas.getByText("Click me", { selector: "button" }); | ||
fireEvent.click(button); | ||
expect(text.innerText).toBe("pending"); | ||
await Promise.resolve(); | ||
// it is resolved in 1 microtask | ||
expect(text.innerText).toBe("done"); | ||
expect(actions.onDone).toBeCalledTimes(1); | ||
actions.onDone.mockReset(); | ||
fireEvent.click(refreshButton); | ||
await Promise.resolve(); | ||
expect(text.innerText).toBe("done"); | ||
expect(actions.onPending).not.toBeCalled(); | ||
expect(actions.onDone).toBeCalledTimes(1); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import { jest } from "@storybook/jest"; | ||
|
||
export const actions = { | ||
onDone: action("done"), | ||
onAbort: action("abort"), | ||
onError: action("error"), | ||
onPending: action("pending"), | ||
}; | ||
|
||
export function createSpyableActions() { | ||
const spyableActions: Record< | ||
keyof typeof actions, | ||
jest.Mock<void, []> | ||
> = Object.fromEntries( | ||
Object.entries(actions).map(([k, v]) => { | ||
const fn = jest.fn(v); | ||
// 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()); | ||
}; | ||
|
||
return { actions: spyableActions, actionsMockReset }; | ||
} |
Oops, something went wrong.
6421bc2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-done-tracker – ./
react-done-tracker.vercel.app
react-done-tracker-git-main-bubblydoo.vercel.app
react-done-tracker-bubblydoo.vercel.app