├── .gitignore ├── .storybook ├── main.js └── preview.js ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── App.test.js ├── components │ ├── Button │ │ ├── Button.stories.jsx │ │ ├── Button.test.jsx │ │ ├── button.css │ │ └── index.jsx │ └── Carousel │ │ ├── Carousel.stories.jsx │ │ ├── Carousel.test.jsx │ │ ├── __snapshots__ │ │ └── Carousel.test.jsx.snap │ │ ├── carousel.css │ │ └── index.jsx ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js └── setupTests.js ├── testing-with-stories-thumbnail.jpg └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /.storybook/main.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "stories": [ 3 | "../src/**/*.stories.mdx", 4 | "../src/**/*.stories.@(js|jsx|ts|tsx)" 5 | ], 6 | "addons": [ 7 | "@storybook/addon-links", 8 | "@storybook/addon-essentials", 9 | "@storybook/preset-create-react-app" 10 | ] 11 | } -------------------------------------------------------------------------------- /.storybook/preview.js: -------------------------------------------------------------------------------- 1 | 2 | export const parameters = { 3 | actions: { argTypesRegex: "^on[A-Z].*" }, 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Storybook stories as tests in React 2 | 3 | This repo contains the code used in a tutorial video. Click the image below or this link for [the youtube video](https://youtu.be/k6NG96awIJ) 4 | 5 | [![Testing with Stories graphic](./testing-with-stories-thumbnail.jpg)](https://youtu.be/k6NG96awIJ0) 6 | 7 | 8 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 9 | 10 | ## Available Scripts 11 | 12 | In the project directory, you can run: 13 | 14 | ### `yarn storybook` 15 | 16 | Runs Storybook and opens it in the browser. Useful to see the components in action! 17 | 18 | The page will reload if you make edits. 19 | 20 | ### `yarn test` 21 | 22 | Launches the test runner in the interactive watch mode.\ 23 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 24 | 25 | ### `yarn test:coverage` 26 | Shows the test coverage of files. Additional configuration has been added to the `package.json` under the `jest` property. This was shown during the video. 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "storybook-tests-react", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.4", 7 | "@testing-library/react": "^11.1.0", 8 | "@testing-library/user-event": "^12.1.10", 9 | "react": "^17.0.1", 10 | "react-dom": "^17.0.1", 11 | "react-scripts": "4.0.3", 12 | "web-vitals": "^1.0.1" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "test:coverage": "react-scripts test --watchAll=false --coverage", 19 | "eject": "react-scripts eject", 20 | "storybook": "start-storybook -p 6006 -s public", 21 | "build-storybook": "build-storybook -s public" 22 | }, 23 | "jest": { 24 | "collectCoverageFrom": [ 25 | "/src/components/**/*.{js,jsx}", 26 | "!**/node_modules/**", 27 | "!**/*.stories.{js,jsx}" 28 | ] 29 | }, 30 | "eslintConfig": { 31 | "extends": [ 32 | "react-app", 33 | "react-app/jest" 34 | ] 35 | }, 36 | "browserslist": { 37 | "production": [ 38 | ">0.2%", 39 | "not dead", 40 | "not op_mini all" 41 | ], 42 | "development": [ 43 | "last 1 chrome version", 44 | "last 1 firefox version", 45 | "last 1 safari version" 46 | ] 47 | }, 48 | "devDependencies": { 49 | "@storybook/addon-actions": "^6.1.21", 50 | "@storybook/addon-essentials": "^6.1.21", 51 | "@storybook/addon-links": "^6.1.21", 52 | "@storybook/node-logger": "^6.1.21", 53 | "@storybook/preset-create-react-app": "^3.1.6", 54 | "@storybook/react": "^6.1.21" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jimmydalecleveland/storybook-tests-react/201e2b435cc9c7da99b7b1025f0e9bc8a5caa578/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jimmydalecleveland/storybook-tests-react/201e2b435cc9c7da99b7b1025f0e9bc8a5caa578/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jimmydalecleveland/storybook-tests-react/201e2b435cc9c7da99b7b1025f0e9bc8a5caa578/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import logo from './logo.svg'; 2 | import './App.css'; 3 | 4 | function App() { 5 | return ( 6 |
7 |
8 | logo 9 |

10 | Edit src/App.js and save to reload. 11 |

12 | 18 | Learn React 19 | 20 |
21 |
22 | ); 23 | } 24 | 25 | export default App; 26 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/components/Button/Button.stories.jsx: -------------------------------------------------------------------------------- 1 | import { Button } from "."; 2 | 3 | const Story = { 4 | title: "Example/Button", 5 | component: Button, 6 | argTypes: { 7 | backgroundColor: { control: "color" }, 8 | }, 9 | }; 10 | 11 | const Template = (args) => 23 | ); 24 | }; 25 | 26 | Button.propTypes = { 27 | /** 28 | * Is this the principal call to action on the page? 29 | */ 30 | primary: PropTypes.bool, 31 | /** 32 | * What background color to use 33 | */ 34 | backgroundColor: PropTypes.string, 35 | /** 36 | * How large should the button be? 37 | */ 38 | size: PropTypes.oneOf(["small", "medium", "large"]), 39 | /** 40 | * Button contents 41 | */ 42 | label: PropTypes.string.isRequired, 43 | /** 44 | * Optional click handler 45 | */ 46 | onClick: PropTypes.func, 47 | }; 48 | 49 | Button.defaultProps = { 50 | backgroundColor: null, 51 | primary: false, 52 | size: "medium", 53 | onClick: undefined, 54 | }; 55 | -------------------------------------------------------------------------------- /src/components/Carousel/Carousel.stories.jsx: -------------------------------------------------------------------------------- 1 | import Carousel from "."; 2 | 3 | const Story = { 4 | title: "Example/Carousel", 5 | component: Carousel, 6 | }; 7 | 8 | const Template = (args) => ; 9 | 10 | export const Default = Template.bind({}); 11 | Default.args = { 12 | slides: ["Swashbuckling", "with", "Code"], 13 | }; 14 | 15 | export default Story; 16 | -------------------------------------------------------------------------------- /src/components/Carousel/Carousel.test.jsx: -------------------------------------------------------------------------------- 1 | import { Default } from "./Carousel.stories"; 2 | import { render, screen } from "@testing-library/react"; 3 | import userEvent from "@testing-library/user-event"; 4 | 5 | test("should render", () => { 6 | const { container } = render(); 7 | expect(container.firstChild).toMatchSnapshot(); 8 | }); 9 | 10 | test("should switch slides when clicked", () => { 11 | render(); 12 | const carouselContainer = screen.getByTestId("carousel-container"); 13 | 14 | // Grab all slides, as they are always in the DOM 15 | const firstSlide = screen.getByText(/swashbuckling/i); 16 | const secondSlide = screen.getByText(/with/i); 17 | const thirdSlide = screen.getByText(/code/i); 18 | 19 | // First slide is visible by default 20 | expect(firstSlide).toBeVisible(); 21 | expect(secondSlide).not.toBeVisible(); 22 | expect(thirdSlide).not.toBeVisible(); 23 | 24 | // After clicking once, only the Second slide is visible 25 | userEvent.click(carouselContainer); 26 | expect(firstSlide).not.toBeVisible(); 27 | expect(secondSlide).toBeVisible(); 28 | expect(thirdSlide).not.toBeVisible(); 29 | }); 30 | -------------------------------------------------------------------------------- /src/components/Carousel/__snapshots__/Carousel.test.jsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`should render 1`] = ` 4 | 26 | `; 27 | -------------------------------------------------------------------------------- /src/components/Carousel/carousel.css: -------------------------------------------------------------------------------- 1 | .carousel-container { 2 | height: 25vw; 3 | width: 45vw; 4 | display: grid; 5 | grid-template: 1 / 1; 6 | font-size: 6rem; 7 | } 8 | 9 | .carousel-slide { 10 | grid-area: 1 / 1; 11 | transition: all 0.3s 0.3s cubic-bezier(0.5, 0, 0.5, 1); 12 | transition-property: opacity, transform; 13 | 14 | display: block; 15 | display: flex; 16 | justify-content: center; 17 | align-items: center; 18 | border-radius: 0.5rem; 19 | box-shadow: 0 1rem 2rem rgba(black, 0.1); 20 | } 21 | 22 | .carousel-slide[hidden] { 23 | transition-delay: 0s; 24 | opacity: 0; 25 | transform: scale(0.8); 26 | } 27 | 28 | .carousel-slide:nth-child(1) { 29 | background: linear-gradient(45deg, #0e192c, #21375c); 30 | color: #fa9474; 31 | } 32 | 33 | .carousel-slide:nth-child(2) { 34 | background: linear-gradient(45deg, #0e192c, #21375c); 35 | color: #67ddff; 36 | } 37 | 38 | .carousel-slide:nth-child(3) { 39 | background: linear-gradient(45deg, #0e192c, #21375c); 40 | color: #40ffdc; 41 | } 42 | -------------------------------------------------------------------------------- /src/components/Carousel/index.jsx: -------------------------------------------------------------------------------- 1 | // Based on David Khourshid's codesandbox example: 2 | // https://codesandbox.io/s/funny-wind-p59ei?file=/src/index.js 3 | import { useState } from "react"; 4 | import PropTypes from "prop-types"; 5 | import "./carousel.css"; 6 | 7 | function Carousel({ slides }) { 8 | const [index, setIndex] = useState(0); 9 | 10 | return ( 11 |
setIndex((index + 1) % slides.length)} 15 | > 16 | {slides.map((screen, i) => ( 17 | 24 | ))} 25 |
26 | ); 27 | } 28 | 29 | Carousel.propTypes = { 30 | slides: PropTypes.array.isRequired, 31 | }; 32 | 33 | export default Carousel; 34 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /testing-with-stories-thumbnail.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jimmydalecleveland/storybook-tests-react/201e2b435cc9c7da99b7b1025f0e9bc8a5caa578/testing-with-stories-thumbnail.jpg --------------------------------------------------------------------------------