Skip to content

Commit

Permalink
docs: update story configs and add e2e story
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdyman committed Sep 11, 2022
1 parent 99bc53e commit 4ab6990
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:testing-library/recommended"
"plugin:@typescript-eslint/recommended"
],
"env": {
"browser": true,
Expand Down
16 changes: 11 additions & 5 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const path = require('path');

const codesandbox = require('remark-codesandbox');

module.exports = {
const storybookConfig = {
addons: [
'@storybook/addon-viewport',
'@storybook/addon-controls',
'@storybook/addon-links',
'@storybook/addon-console',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/jest',
'@storybook/addon-coverage',
'@storybook/addon-storysource',
{
name: '@storybook/addon-docs',
Expand All @@ -20,10 +25,9 @@ module.exports = {
},
},
},
'@storybook/addon-actions/register',
],
/** Files to load as stories */
stories: ['../docs/**/*.story.@(mdx|tsx)'],
stories: ['../docs/**/*.stories.@(mdx|tsx)'],
/** Customise webpack config */
webpackFinal: async (config) => {
// @HACK Horrific hack to shoehorn `remark-codesandbox` plugin into presets
Expand Down Expand Up @@ -66,3 +70,5 @@ module.exports = {
return config;
},
};

module.exports = storybookConfig;
6 changes: 6 additions & 0 deletions docs/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc",
"rules": {
"no-console": 0
}
}
2 changes: 1 addition & 1 deletion docs/00-intro.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Meta } from '@storybook/addon-docs/blocks';
import { Meta } from '@storybook/addon-docs';

<Meta title="Docs/Intro" />

Expand Down
2 changes: 1 addition & 1 deletion docs/01-api.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsTable, Description, Meta } from '@storybook/addon-docs/blocks';
import { ArgsTable, Description, Meta } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderHandle, ReactCompareSliderImage, styleFitContainer } from 'react-compare-slider';

Expand Down
2 changes: 1 addition & 1 deletion docs/02-images.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider';

Expand Down
2 changes: 1 addition & 1 deletion docs/03-handles.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Canvas, ArgsTable, Meta, Story } from '@storybook/addon-docs/blocks';
import { Canvas, ArgsTable, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSliderHandle } from 'react-compare-slider';

Expand Down
2 changes: 1 addition & 1 deletion docs/05-only-handle-draggable.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider';

Expand Down
2 changes: 1 addition & 1 deletion docs/06-bounds-padding.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider';

Expand Down
2 changes: 1 addition & 1 deletion docs/07-change-position-on-hover.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider';

Expand Down
101 changes: 101 additions & 0 deletions docs/stories/99-e2e-tests.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { jest, expect } from '@storybook/jest';
import type { Meta, Story } from '@storybook/react';
import { userEvent, waitFor, within } from '@storybook/testing-library';
import React from 'react';

import {
ReactCompareSlider,
ReactCompareSliderHandle,
ReactCompareSliderImage as BaseReactCompareSliderImage,
ReactCompareSliderDetailedProps,
styleFitContainer,
} from '../../src';

export default {
title: 'Tests/E2E',
} as Meta;

const getArgs = (args: Partial<ReactCompareSliderDetailedProps> = {}) => ({
'data-testid': 'rcs-root',
onPositionChange: jest.fn(console.log),
style: { width: '100%', height: '100vh' },
itemOne: (
<BaseReactCompareSliderImage
src="https://raw.githubusercontent.com/nerdyman/stuff/main/libs/react-compare-slider/demo-images/e2e-test-1.png"
alt="one"
/>
),
itemTwo: (
<BaseReactCompareSliderImage
src="https://raw.githubusercontent.com/nerdyman/stuff/main/libs/react-compare-slider/demo-images/e2e-test-2.png"
alt="two"
/>
),
...args,
});

const Template: Story<ReactCompareSliderDetailedProps> = (args) => (
<ReactCompareSlider {...args} />
);

/** Test default props. */
export const Default = Template.bind({});
Default.args = getArgs();

Default.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);
const rootComponent = canvas.queryByTestId(Default.args['data-testid']) as Element;

// Should have elements on mount.
await new Promise((resolve) => setTimeout(resolve, 500));
await waitFor(() => expect(rootComponent).toBeInTheDocument());
await waitFor(() => expect(canvas.getByAltText('one')).toBeInTheDocument());
await waitFor(() => expect(canvas.getByAltText('two')).toBeInTheDocument());

// Should have initial position on mount.
await waitFor(() => expect(Default.args.onPositionChange).toHaveBeenLastCalledWith(50));

// On click.
userEvent.click(rootComponent, {
clientX: rootComponent.clientWidth * 0.25,
clientY: rootComponent.clientHeight * 0.25,
});

await waitFor(() => expect(Default.args.onPositionChange).toHaveBeenLastCalledWith(25));
};

/** Test `handle`. */
export const CustomHandle = Template.bind({});

CustomHandle.args = getArgs({
handle: <ReactCompareSliderHandle data-testid="handlearoo" />,
});

CustomHandle.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);

waitFor(() => expect(canvas.queryByTestId('handlearoo')).toBeInTheDocument());
};

/** Default image. */
export const ReactCompareSliderImage = (args) => (
<BaseReactCompareSliderImage {...args} />
);

ReactCompareSliderImage.args = {
alt: 'testaroo',
src: 'https://raw.githubusercontent.com/nerdyman/stuff/main/libs/react-compare-slider/demo-images/e2e-test-1.png',
};

ReactCompareSliderImage.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);

await waitFor(() =>
expect(canvas.getByAltText(ReactCompareSliderImage.args.alt)).toBeInTheDocument()
);

// Ensure default styles have been applied to `ReactCompareSliderImage`.
expect(canvas.getByAltText(ReactCompareSliderImage.args.alt).style).toMatchObject(
styleFitContainer() as Record<string, unknown>
);
};

0 comments on commit 4ab6990

Please sign in to comment.