Skip to content

Commit d2f945e

Browse files
committed
Add interaction tests for InboxScreen
1 parent 2c8173d commit d2f945e

File tree

6 files changed

+951
-69
lines changed

6 files changed

+951
-69
lines changed

.storybook/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ module.exports = {
99
'@storybook/addon-links',
1010
'@storybook/addon-essentials',
1111
'@storybook/preset-create-react-app',
12+
'@storybook/addon-interactions',
1213
],
1314
core: {
1415
builder: {
1516
name: 'webpack5',
1617
},
1718
},
19+
features: {
20+
interactionsDebugger: true,
21+
},
1822
webpackFinal: async (config) => {
1923
return {
2024
...config,

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
"scripts": {
2020
"start": "SKIP_PREFLIGHT_CHECK=true react-scripts start",
2121
"build": "SKIP_PREFLIGHT_CHECK=true react-scripts build",
22-
"test": "react-scripts test",
22+
"test": "SKIP_PREFLIGHT_CHECK=true react-scripts test",
2323
"eject": "react-scripts eject",
2424
"storybook": "start-storybook -p 6006",
25-
"build-storybook": "build-storybook"
25+
"build-storybook": "build-storybook",
26+
"test-storybook": "test-storybook"
2627
},
2728
"eslintConfig": {
2829
"extends": [
@@ -55,12 +56,16 @@
5556
"devDependencies": {
5657
"@storybook/addon-actions": "^6.5.3",
5758
"@storybook/addon-essentials": "^6.5.3",
59+
"@storybook/addon-interactions": "^6.5.3",
5860
"@storybook/addon-links": "^6.5.3",
5961
"@storybook/builder-webpack5": "^6.5.3",
62+
"@storybook/jest": "^0.0.10",
6063
"@storybook/manager-webpack5": "^6.5.3",
6164
"@storybook/node-logger": "^6.5.3",
6265
"@storybook/preset-create-react-app": "^4.1.0",
6366
"@storybook/react": "^6.5.3",
67+
"@storybook/test-runner": "^0.1.0",
68+
"@storybook/testing-library": "^0.0.11",
6469
"msw": "^0.40.0",
6570
"msw-storybook-addon": "^1.6.3"
6671
},

src/InboxScreen.stories.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22
import { rest } from 'msw';
3+
import { within, userEvent, findByRole } from '@storybook/testing-library';
4+
import { expect } from '@storybook/jest';
35
import { InboxScreen } from './InboxScreen';
46
import { Default as TaskListDefault } from './components/TaskList.stories';
57

@@ -34,3 +36,51 @@ Error.parameters = {
3436
],
3537
},
3638
};
39+
40+
export const PinTask = Template.bind({});
41+
PinTask.parameters = Default.parameters;
42+
PinTask.play = async ({ canvasElement }) => {
43+
const canvas = within(canvasElement);
44+
const getTask = (name) => canvas.findByRole('listitem', { name });
45+
46+
// Find the task to pin
47+
const itemToPin = await getTask('Export logo');
48+
49+
// Find the pin button
50+
const pinButton = await findByRole(itemToPin, 'button', { name: 'pin' });
51+
52+
// Click the pin button
53+
await userEvent.click(pinButton);
54+
55+
// Check that the pin button is now a unpin button
56+
const unpinButton = within(itemToPin).getByRole('button', { name: 'unpin' });
57+
await expect(unpinButton).toBeInTheDocument();
58+
};
59+
60+
export const ArchiveTask = Template.bind({});
61+
ArchiveTask.parameters = Default.parameters;
62+
ArchiveTask.play = async ({ canvasElement }) => {
63+
const canvas = within(canvasElement);
64+
const getTask = (name) => canvas.findByRole('listitem', { name });
65+
66+
const itemToArchive = await getTask('QA dropdown');
67+
const archiveCheckbox = await findByRole(itemToArchive, 'checkbox');
68+
await userEvent.click(archiveCheckbox);
69+
70+
await expect(archiveCheckbox.checked).toBe(true);
71+
};
72+
73+
export const EditTask = Template.bind({});
74+
EditTask.parameters = Default.parameters;
75+
EditTask.play = async ({ canvasElement }) => {
76+
const canvas = within(canvasElement);
77+
const getTask = (name) => canvas.findByRole('listitem', { name });
78+
79+
const itemToEdit = await getTask('Fix bug in input error state');
80+
const taskInput = await findByRole(itemToEdit, 'textbox');
81+
82+
userEvent.type(taskInput, ' and disabled state');
83+
await expect(taskInput.value).toBe(
84+
'Fix bug in input error state and disabled state'
85+
);
86+
};

src/InboxScreen.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React from 'react';
2+
import '@testing-library/jest-dom/extend-expect';
3+
import {
4+
render,
5+
waitFor,
6+
cleanup,
7+
within,
8+
fireEvent,
9+
} from '@testing-library/react';
10+
import { composeStories } from '@storybook/testing-react';
11+
import { getWorker } from 'msw-storybook-addon';
12+
import * as stories from './InboxScreen.stories';
13+
14+
describe('InboxScreen', () => {
15+
afterEach(() => {
16+
cleanup();
17+
});
18+
19+
// Clean up after all tests are done, preventing this
20+
// interception layer from affecting irrelevant tests
21+
afterAll(() => getWorker().close());
22+
23+
const { Default } = composeStories(stories);
24+
25+
it('should pin a task', async () => {
26+
const { queryByText, getByRole } = render(<Default />);
27+
28+
await waitFor(() => {
29+
expect(queryByText('You have no tasks')).not.toBeInTheDocument();
30+
});
31+
32+
const getTask = () => getByRole('listitem', { name: 'Export logo' });
33+
34+
const pinButton = within(getTask()).getByRole('button', { name: 'pin' });
35+
36+
fireEvent.click(pinButton);
37+
38+
const unpinButton = within(getTask()).getByRole('button', {
39+
name: 'unpin',
40+
});
41+
42+
expect(unpinButton).toBeInTheDocument();
43+
});
44+
45+
it('should archive a task', async () => {
46+
const { queryByText, getByRole } = render(<Default />);
47+
48+
await waitFor(() => {
49+
expect(queryByText('You have no tasks')).not.toBeInTheDocument();
50+
});
51+
52+
const task = getByRole('listitem', { name: 'QA dropdown' });
53+
const archiveCheckbox = within(task).getByRole('checkbox');
54+
expect(archiveCheckbox.checked).toBe(false);
55+
56+
fireEvent.click(archiveCheckbox);
57+
expect(archiveCheckbox.checked).toBe(true);
58+
});
59+
60+
it('should edit a task', async () => {
61+
const { queryByText, getByRole } = render(<Default />);
62+
63+
await waitFor(() => {
64+
expect(queryByText('You have no tasks')).not.toBeInTheDocument();
65+
});
66+
67+
const task = getByRole('listitem', {
68+
name: 'Fix bug in input error state',
69+
});
70+
const taskInput = within(task).getByRole('textbox');
71+
72+
const updatedTaskName = 'Fix bug in the textarea error state';
73+
74+
fireEvent.change(taskInput, {
75+
target: { value: 'Fix bug in the textarea error state' },
76+
});
77+
expect(taskInput.value).toBe(updatedTaskName);
78+
});
79+
});

src/setupTests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
// expect(element).toHaveTextContent(/react/i)
44
// learn more: https://github.com/testing-library/jest-dom
55
import '@testing-library/jest-dom';
6+
7+
import { setGlobalConfig } from '@storybook/testing-react';
8+
import * as globalStorybookConfig from '../.storybook/preview';
9+
10+
setGlobalConfig(globalStorybookConfig);

0 commit comments

Comments
 (0)