Skip to content

Commit

Permalink
Alter for custom-written tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Aug 24, 2022
1 parent 11a80e0 commit 9c927ca
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ phpunit.xml
phpunit-watcher.yml
.tool-versions
test/storybook-playwright/test-results
test/storybook-playwright/index.spec.ts-snapshots
test/storybook-playwright/specs/__snapshots__
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
"storybook:build": "build-storybook -c ./storybook -o ./storybook/build",
"prestorybook:dev": "npm run build:packages",
"storybook:dev": "concurrently \"npm run dev:packages\" \"start-storybook -c ./storybook -p 50240\"",
"storybook:e2e:dev": "concurrently \"npm run dev:packages\" \"start-storybook -c test/storybook-playwright/storybook -p 50241\"",
"test": "npm-run-all lint test:unit",
"test:create-block": "bash ./bin/test-create-block.sh",
"test:e2e": "wp-scripts test-e2e --config packages/e2e-tests/jest.config.js",
Expand Down
25 changes: 25 additions & 0 deletions packages/components/src/popover/stories/e2e.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import Popover from '..';

export default {
title: 'Components/Popover',
component: Popover,
};

export const Default = () => {
const [ isVisible, setIsVisible ] = useState( false );

return (
<button onClick={ () => setIsVisible( ( state ) => ! state ) }>
Toggle Popover!
{ isVisible && <Popover>Popover is toggled!</Popover> }
</button>
);
};
1 change: 0 additions & 1 deletion storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = {
features: {
babelModeV7: true,
emotionAlias: false,
buildStoriesJson: true,
},
// Workaround:
// https://github.com/storybookjs/storybook/issues/12270
Expand Down
22 changes: 9 additions & 13 deletions test/storybook-playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ This is currently set up for testing visual regressions in the `components` pack

## How to run

First, prepare a static build of the Storybook. This is required to generate the `stories.json` file, which contains metadata for every story.
First, build and serve the E2E Storybook.

```sh
npm run storybook:build
```

Then serve the Storybook locally, using either the static build prepared in the previous step, or the dev server if you want to iterate in watch mode.

```sh
# Using the static build
npx http-server storybook/build -p 50240

# Using the dev server
npm run storybook:dev
storybook:e2e:dev
```

You are now ready to run the tests. The first run will generate the reference images, and subsequent runs will compare against them.
Expand All @@ -30,4 +20,10 @@ To update the reference images, pass the `--update-snapshots` flag.

```sh
npm run test:e2e:storybook -- --update-snapshots
```
```

## How to write tests

Any stories matching the ones listed in the [E2E Storybook config](./storybook/main.js) will be included in the special build. These are the fixtures for our tests.

The Playwright test files live in the [`specs`](./specs/) folder.
45 changes: 0 additions & 45 deletions test/storybook-playwright/index.spec.ts

This file was deleted.

19 changes: 19 additions & 0 deletions test/storybook-playwright/specs/popover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

/**
* Internal dependencies
*/
import { gotoStoryId } from '../utils';

test.describe( 'Popover', () => {
test( 'should render', async ( { page } ) => {
await gotoStoryId( page, 'components-popover--default' );

await page.click( 'role=button' );

expect( await page.screenshot() ).toMatchSnapshot();
} );
} );
14 changes: 14 additions & 0 deletions test/storybook-playwright/storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
const baseConfig = require( '../../../storybook/main' );

const config = {
...baseConfig,
addons: [],
stories: [
'../../../packages/components/src/**/e2e.stories.@(js|tsx|mdx)',
],
};

module.exports = config;
12 changes: 12 additions & 0 deletions test/storybook-playwright/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* External dependencies
*/
import type { Page } from '@playwright/test';

const STORYBOOK_PORT = '50241';

export const gotoStoryId = ( page: Page, storyId: string ) =>
page.goto(
`http://localhost:${ STORYBOOK_PORT }/iframe.html?id=${ storyId }`,
{ waitUntil: 'load' }
);

0 comments on commit 9c927ca

Please sign in to comment.