|
1 | 1 | import dedent from 'ts-dedent';
|
| 2 | +import path from 'path'; |
| 3 | +import * as coreCommon from '@storybook/core-common'; |
| 4 | + |
2 | 5 | import { transformPlaywright } from './transformPlaywright';
|
3 | 6 |
|
| 7 | +jest.mock('@storybook/core-common'); |
| 8 | + |
4 | 9 | expect.addSnapshotSerializer({
|
5 | 10 | print: (val: any) => val.trim(),
|
6 | 11 | test: (val: any) => true,
|
7 | 12 | });
|
8 | 13 |
|
9 | 14 | describe('Playwright', () => {
|
| 15 | + beforeEach(() => { |
| 16 | + const relativeSpy = jest.spyOn(path, 'relative'); |
| 17 | + relativeSpy.mockReturnValueOnce('stories/basic/Header.stories.js'); |
| 18 | + jest.spyOn(coreCommon, 'serverRequire').mockImplementation(() => ({ |
| 19 | + stories: [ |
| 20 | + { |
| 21 | + directory: '../stories/basic', |
| 22 | + titlePrefix: 'Example', |
| 23 | + }, |
| 24 | + ], |
| 25 | + })); |
| 26 | + jest.spyOn(coreCommon, 'normalizeStories').mockImplementation(() => [ |
| 27 | + { |
| 28 | + titlePrefix: 'Example', |
| 29 | + files: '**/*.stories.@(mdx|tsx|ts|jsx|js)', |
| 30 | + directory: './stories/basic', |
| 31 | + importPathMatcher: |
| 32 | + /^\.[\\/](?:stories\/basic(?:\/(?!\.)(?:(?:(?!(?:^|\/)\.).)*?)\/|\/|$)(?!\.)(?=.)[^/]*?\.stories\.(mdx|tsx|ts|jsx|js))$/, |
| 33 | + }, |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + const filename = './stories/basic/Header.stories.js'; |
10 | 38 | it('should generate a play test when the story has a play function', () => {
|
11 | 39 | expect(
|
12 |
| - transformPlaywright(dedent` |
13 |
| - export default { title: 'foo/bar' }; |
| 40 | + transformPlaywright( |
| 41 | + dedent` |
| 42 | + export default { title: 'foo/bar', component: Button }; |
14 | 43 | export const A = () => {};
|
15 | 44 | A.play = () => {};
|
16 |
| - `) |
| 45 | + `, |
| 46 | + filename |
| 47 | + ) |
17 | 48 | ).toMatchInlineSnapshot(`
|
18 | 49 | if (!require.main) {
|
19 | 50 | describe("foo/bar", () => {
|
@@ -43,10 +74,13 @@ describe('Playwright', () => {
|
43 | 74 | });
|
44 | 75 | it('should generate a smoke test when story does not have a play function', () => {
|
45 | 76 | expect(
|
46 |
| - transformPlaywright(dedent` |
| 77 | + transformPlaywright( |
| 78 | + dedent` |
47 | 79 | export default { title: 'foo/bar' };
|
48 | 80 | export const A = () => {};
|
49 |
| - `) |
| 81 | + `, |
| 82 | + filename |
| 83 | + ) |
50 | 84 | ).toMatchInlineSnapshot(`
|
51 | 85 | if (!require.main) {
|
52 | 86 | describe("foo/bar", () => {
|
@@ -74,4 +108,41 @@ describe('Playwright', () => {
|
74 | 108 | }
|
75 | 109 | `);
|
76 | 110 | });
|
| 111 | + |
| 112 | + it('should generate a smoke test with auto title', () => { |
| 113 | + expect( |
| 114 | + transformPlaywright( |
| 115 | + dedent` |
| 116 | + export default { component: Button }; |
| 117 | + export const A = () => {}; |
| 118 | + `, |
| 119 | + filename |
| 120 | + ) |
| 121 | + ).toMatchInlineSnapshot(` |
| 122 | + if (!require.main) { |
| 123 | + describe("Example/Header", () => { |
| 124 | + describe("A", () => { |
| 125 | + it("smoke-test", async () => { |
| 126 | + page.on('pageerror', err => { |
| 127 | + page.evaluate(({ |
| 128 | + id, |
| 129 | + err |
| 130 | + }) => __throwError(id, err), { |
| 131 | + id: "example-header--a", |
| 132 | + err: err.message |
| 133 | + }); |
| 134 | + }); |
| 135 | + return page.evaluate(({ |
| 136 | + id, |
| 137 | + hasPlayFn |
| 138 | + }) => __test(id, hasPlayFn), { |
| 139 | + id: "example-header--a", |
| 140 | + hasPlayFn: false |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + `); |
| 147 | + }); |
77 | 148 | });
|
0 commit comments