-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from pixiv/feat/add-storybook-snapshot
add storybook snapshot
- Loading branch information
Showing
42 changed files
with
388 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { config } from '../jest.config.mjs' | ||
|
||
export default { | ||
...config(), | ||
moduleNameMapper: { | ||
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': | ||
'<rootDir>/../__mocks__/file.ts', | ||
'\\.(styl|css|less|scss)$': '<rootDir>/../__mocks__/style.ts', | ||
'\\.(mdx)$': '<rootDir>/../__mocks__/mdx.ts', | ||
}, | ||
setupFilesAfterEnv: ['../jest.setup.ts', './jest.setup.ts'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { setProjectAnnotations } from '@storybook/react' | ||
|
||
import * as projectAnnotations from './preview' | ||
|
||
setProjectAnnotations(projectAnnotations) | ||
|
||
globalThis.location = { | ||
...globalThis.location, | ||
search: '', | ||
} | ||
window.location = { | ||
...window.location, | ||
search: '', | ||
} | ||
|
||
global.IntersectionObserver = jest.fn().mockImplementation(() => ({ | ||
observe() { | ||
return null | ||
}, | ||
disconnect() { | ||
return null | ||
}, | ||
})) | ||
|
||
global.matchMedia = jest.fn().mockImplementation(() => ({ | ||
matches: true, | ||
media: '(max-width: 600px)', | ||
addEventListener() { | ||
// Do Nothing | ||
}, | ||
removeEventListener() { | ||
// Do Nothing | ||
}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import path from 'node:path' | ||
import * as glob from 'glob' | ||
|
||
import { composeStories } from '@storybook/react' | ||
import renderer from 'react-test-renderer' | ||
import { addSerializer } from 'jest-specific-snapshot' | ||
import { styleSheetSerializer } from 'jest-styled-components' | ||
|
||
import type { Meta, StoryFn } from '@storybook/react' | ||
|
||
addSerializer(styleSheetSerializer) | ||
|
||
type StoryFile = { | ||
default: Meta | ||
[name: string]: StoryFn | Meta | ||
} | ||
|
||
const compose = ( | ||
entry: StoryFile | ||
): ReturnType<typeof composeStories<StoryFile>> => { | ||
try { | ||
return composeStories(entry) | ||
} catch (e) { | ||
throw new Error( | ||
`There was an issue composing stories for the module: ${JSON.stringify( | ||
entry | ||
)}, ${e}` | ||
) | ||
} | ||
} | ||
|
||
function getAllStoryFiles() { | ||
// Place the glob you want to match your stories files | ||
const storyFiles = glob.sync( | ||
path.join(__dirname, '../packages/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}') | ||
) | ||
|
||
return storyFiles.map((filePath) => { | ||
const storyFile = require(filePath) | ||
return { filePath, storyFile } | ||
}) | ||
} | ||
|
||
// Recreate similar options to Storyshots. Place your configuration below | ||
const options = { | ||
suite: 'Storybook Tests', | ||
storyKindRegex: /^.*?DontTest$/, | ||
storyNameRegex: /UNSET/, | ||
snapshotsDirName: '__snapshots__', | ||
snapshotExtension: 'storyshot', | ||
} | ||
|
||
describe(options.suite, () => { | ||
getAllStoryFiles().forEach(({ storyFile, filePath }) => { | ||
const meta = storyFile.default | ||
const title = meta.title || filePath | ||
const dir = path.join(path.dirname(filePath), options.snapshotsDirName) | ||
const filename = [ | ||
path.basename(filePath, '.tsx'), | ||
options.snapshotExtension, | ||
].join('.') | ||
const snapshotPath = path.join(dir, filename) | ||
|
||
if ( | ||
options.storyKindRegex.test(title) || | ||
meta.parameters?.storyshots?.disable | ||
) { | ||
return | ||
} | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)) | ||
.map(([name, story]) => ({ name, story })) | ||
.filter(({ name, story }) => { | ||
return ( | ||
!options.storyNameRegex.test(name) && | ||
!story.parameters.storyshots?.disable | ||
) | ||
}) | ||
|
||
if (stories.length <= 0) { | ||
throw new Error( | ||
`No stories found for this module: ${title}. Make sure there is at least one valid story for this module, without a disable parameter, or add parameters.storyshots.disable in the default export of this file.` | ||
) | ||
} | ||
|
||
for (const { name, story } of stories) { | ||
const testFn = story.parameters.storyshots?.skip ? test.skip : test | ||
|
||
testFn(name, async () => { | ||
const mounted = renderer.create(story()).toJSON() | ||
expect(mounted).toMatchSpecificSnapshot(snapshotPath) | ||
}) | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-sandbox/src/components/Carousel/__snapshots__/index.story.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-sandbox/src/components/HintText/__snapshots__/index.story.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.