Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Add E2E tests for the module mocking stories #27046

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions code/e2e-tests/module-mocking.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { test, expect } from '@playwright/test';
import process from 'process';
import { SbPage } from './util';

const storybookUrl = process.env.STORYBOOK_URL || 'http://localhost:8001';

test.describe('module-mocking', () => {
test.beforeEach(async ({ page }) => {
await page.goto(storybookUrl);

await new SbPage(page).waitUntilLoaded();
});

test('should assert story lifecycle order', async ({ page }) => {
const sbPage = new SbPage(page);

await sbPage.navigateToStory('lib/test/before-each', 'before-each-order');

await sbPage.viewAddonPanel('Actions');
const logItem = await page.locator('#storybook-panel-root #panel-tab-content');
await expect(logItem).toBeVisible();

const expectedTexts = [
'1 - [from loaders]',
'2 - [from meta beforeEach]',
'3 - [from story beforeEach]',
'4 - [from decorator]',
'5 - [from onClick]',
];

// Assert that each LI text content contains the expected text in order
for (let i = 0; i < expectedTexts.length; i++) {
const nthText = await logItem.locator(`li >> nth=${i}`).innerText();
expect(nthText).toMatch(expectedTexts[i]);
}
});

test('should assert that utils import is mocked', async ({ page }) => {
const sbPage = new SbPage(page);

await sbPage.navigateToStory('lib/test/module-mocking', 'basic');

await sbPage.viewAddonPanel('Actions');
const logItem = await page.locator('#storybook-panel-root #panel-tab-content', {
hasText: 'foo: []',
});
await expect(logItem).toBeVisible();
});
});
25 changes: 19 additions & 6 deletions code/lib/test/template/stories/before-each.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { expect, mocked, getByRole, spyOn, userEvent } from '@storybook/test';

const meta = {
component: globalThis.Components.Button,
beforeEach() {
loaders() {
spyOn(console, 'log').mockName('console.log');
console.log('first');
console.log('1 - [from loaders]');
},
beforeEach() {
console.log('2 - [from meta beforeEach]');
},
};

Expand All @@ -15,17 +18,27 @@ export const BeforeEachOrder = {
chromatic: { disable: true },
},
beforeEach() {
console.log('second');
console.log('3 - [from story beforeEach]');
},
decorators: (StoryFn: any) => {
console.log('4 - [from decorator]');
return StoryFn();
},
args: {
label: 'Button',
onClick: () => {
console.log('third');
console.log('5 - [from onClick]');
},
},
async play({ canvasElement }) {
async play({ canvasElement }: any) {
await userEvent.click(getByRole(canvasElement, 'button'));

await expect(mocked(console.log).mock.calls).toEqual([['first'], ['second'], ['third']]);
await expect(mocked(console.log).mock.calls).toEqual([
['1 - [from loaders]'],
['2 - [from meta beforeEach]'],
['3 - [from story beforeEach]'],
['4 - [from decorator]'],
['5 - [from onClick]'],
]);
},
};
2 changes: 1 addition & 1 deletion code/lib/test/template/stories/utils.mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fn } from '@storybook/test';
import * as utils from './utils';

export const foo = fn(utils.foo);
export const foo = fn(utils.foo).mockName('foo');
8 changes: 8 additions & 0 deletions code/presets/create-react-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ const webpack = async (
...getModulePath(CWD),
],
plugins: [PnpWebpackPlugin as any],
// manual copy from builder-webpack because defaults are disabled in this CRA preset
conditionNames: [
...(webpackConfig.resolve?.conditionNames ?? []),
'storybook',
'stories',
'test',
'...',
],
},
resolveLoader,
} as Configuration;
Expand Down