Skip to content

Commit

Permalink
feat: add storybook test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Feb 24, 2022
1 parent b0a845d commit 0ef0fb2
Show file tree
Hide file tree
Showing 4 changed files with 2,459 additions and 3,329 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@
"@storybook/addon-interactions": "^6.4.19",
"@storybook/addon-storyshots": "^6.4.19",
"@storybook/builder-webpack5": "^6.4.19",
"@storybook/jest": "^0.0.6",
"@storybook/jest": "^0.0.9",
"@storybook/manager-webpack5": "^6.4.19",
"@storybook/preset-create-react-app": "^4.0.1",
"@storybook/react": "^6.4.19",
"@storybook/test-runner": "^0.0.4",
"@storybook/testing-library": "^0.0.9",
"@storybook/testing-react": "^1.2.3",
"@testing-library/jest-dom": "^5.16.1",
Expand Down
2 changes: 1 addition & 1 deletion public/mockServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* tslint:disable */

/**
* Mock Service Worker (0.36.0).
* Mock Service Worker (0.36.8).
* @see https://github.com/mswjs/msw
* - Please do NOT modify this file.
* - Please do NOT serve this file on production.
Expand Down
102 changes: 56 additions & 46 deletions src/pages/RestaurantDetailPage/RestaurantDetailPage.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ComponentStory, ComponentMeta } from '@storybook/react'
import { expect } from '@storybook/jest'
import { within, userEvent, waitForElementToBeRemoved } from '@storybook/testing-library'
import { within, userEvent } from '@storybook/testing-library'
import { rest } from 'msw'

import { BASE_URL } from '../../api'
import { animatedUserEventClick } from '../../../.storybook/interaction'
import { restaurants } from '../../stub/restaurants'
import { cartItems } from '../../stub/cart-items'

Expand Down Expand Up @@ -42,6 +41,17 @@ Success.parameters = {
],
}

export const WithModalOpen = Template.bind({})
WithModalOpen.play = async ({ canvasElement }) => {
const canvas = within(canvasElement)
const item = await canvas.findByText(/Cheeseburger/i)
await userEvent.click(item)
await expect(canvas.getByTestId('modal')).toBeInTheDocument()
}
WithModalOpen.parameters = {
...Success.parameters,
}

export const WithItemsInTheCart = Template.bind({})
WithItemsInTheCart.parameters = {
...Success.parameters,
Expand Down Expand Up @@ -95,47 +105,47 @@ Error.parameters = {
},
}

export const SelectingAndUpdatingItems = Template.bind({})
SelectingAndUpdatingItems.parameters = {
msw: {
handlers: [rest.get(BASE_URL, (req, res, ctx) => res(ctx.json(restaurants[0])))],
},
}
SelectingAndUpdatingItems.args = {
demoMode: false,
}
SelectingAndUpdatingItems.argTypes = {
demoMode: {
control: { type: 'boolean' },
},
}
SelectingAndUpdatingItems.play = async ({ canvasElement, args }) => {
// @ts-ignore
const clickEvent = args.demoMode === true ? animatedUserEventClick : userEvent.click
const canvas = within(canvasElement)

await waitForElementToBeRemoved(await canvas.findByText('Looking for some food...'))

const foodItem = await canvas.findByText(/Cheeseburger/i)
await clickEvent(foodItem)

const modalButton = await canvas.findByLabelText('increase quantity by one')
await clickEvent(modalButton)
await clickEvent(modalButton)
await clickEvent(canvas.getByLabelText('confirm'))

const cheeseburgerItem = within(foodItem.parentElement!)

await expect(cheeseburgerItem.getByLabelText('food quantity').textContent).toEqual('3')

await clickEvent(canvas.getByLabelText('food cart'))
const sidebar = await within(canvasElement).findByTestId('sidebar')

const foodItemSelector: HTMLSelectElement = within(sidebar).getByRole('combobox')
await expect(foodItemSelector.value).toEqual('3')
await userEvent.selectOptions(foodItemSelector, '2')

await clickEvent(canvas.getByLabelText('close sidebar'))
await expect(cheeseburgerItem.getByLabelText('food quantity').textContent).toEqual('2')
}
SelectingAndUpdatingItems.storyName = '▶️ Selecting and updating items'
// export const SelectingAndUpdatingItems = Template.bind({})
// SelectingAndUpdatingItems.parameters = {
// msw: {
// handlers: [rest.get(BASE_URL, (req, res, ctx) => res(ctx.json(restaurants[0])))],
// },
// }
// SelectingAndUpdatingItems.args = {
// demoMode: false,
// }
// SelectingAndUpdatingItems.argTypes = {
// demoMode: {
// control: { type: 'boolean' },
// },
// }
// SelectingAndUpdatingItems.play = async ({ canvasElement, args }) => {
// // @ts-ignore
// const clickEvent = args.demoMode === true ? animatedUserEventClick : userEvent.click
// const canvas = within(canvasElement)

// await waitForElementToBeRemoved(await canvas.findByText('Looking for some food...'))

// const foodItem = await canvas.findByText(/Cheeseburger/i)
// await clickEvent(foodItem)

// const modalButton = await canvas.findByLabelText('increase quantity by one')
// await clickEvent(modalButton)
// await clickEvent(modalButton)
// await clickEvent(canvas.getByLabelText('confirm'))

// const cheeseburgerItem = within(foodItem.parentElement!)

// await expect(cheeseburgerItem.getByLabelText('food quantity').textContent).toEqual('3')

// await clickEvent(canvas.getByLabelText('food cart'))
// const sidebar = await within(canvasElement).findByTestId('sidebar')

// const foodItemSelector: HTMLSelectElement = within(sidebar).getByRole('combobox')
// await expect(foodItemSelector.value).toEqual('3')
// await userEvent.selectOptions(foodItemSelector, '2')

// await clickEvent(canvas.getByLabelText('close sidebar'))
// await expect(cheeseburgerItem.getByLabelText('food quantity').textContent).toEqual('2')
// }
// SelectingAndUpdatingItems.storyName = '▶️ Selecting and updating items'
Loading

1 comment on commit 0ef0fb2

@vercel
Copy link

@vercel vercel bot commented on 0ef0fb2 Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.