-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Storyshots addon refactoring #3745
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e46dada
Create and expose Stories2SnapsConverter to support customization of …
igor-dv 19b269e
WIP restructuring
igor-dv 120af7c
Few minor cleanups
igor-dv 36d6a9c
Change "export * from x" to manually importing and exporting named ex…
igor-dv 52ef4c4
Import framework loaders dynamically
igor-dv 0fbb935
Move appOptions imports to the load methods
igor-dv fce815a
Add docs for the stories2snapsConverters
igor-dv 6644743
Fix README.md
igor-dv e43a733
Mention about imageSnapshot in the MIGRATION.md
igor-dv f45a3cc
Merge remote-tracking branch 'origin/master' into storyshots-refactoring
igor-dv 3063a77
MIGRATION.md and README.md improvements
igor-dv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 50 additions & 0 deletions
50
addons/storyshots/storyshots-core/src/Stories2SnapsConverter.js
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,50 @@ | ||
import path from 'path'; | ||
|
||
const defaultOptions = { | ||
snapshotsDirName: '__snapshots__', | ||
snapshotExtension: '.storyshot', | ||
storiesExtensions: ['.js', '.jsx', '.ts', '.tsx'], | ||
}; | ||
|
||
class DefaultStories2SnapsConverter { | ||
constructor(options = {}) { | ||
this.options = { | ||
...defaultOptions, | ||
...options, | ||
}; | ||
} | ||
|
||
getSnapshotExtension = () => this.options.snapshotExtension; | ||
|
||
getStoryshotFile(fileName) { | ||
const { dir, name } = path.parse(fileName); | ||
const { snapshotsDirName, snapshotExtension } = this.options; | ||
|
||
return path.format({ dir: path.join(dir, snapshotsDirName), name, ext: snapshotExtension }); | ||
} | ||
|
||
getSnapshotFileName(context) { | ||
const { fileName } = context; | ||
|
||
if (!fileName) { | ||
return null; | ||
} | ||
|
||
return this.getStoryshotFile(fileName); | ||
} | ||
|
||
getPossibleStoriesFiles(storyshotFile) { | ||
const { dir, name } = path.parse(storyshotFile); | ||
const { storiesExtensions } = this.options; | ||
|
||
return storiesExtensions.map(ext => | ||
path.format({ | ||
dir: path.dirname(dir), | ||
name, | ||
ext, | ||
}) | ||
); | ||
} | ||
} | ||
|
||
export default DefaultStories2SnapsConverter; |
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
File renamed without changes.
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,90 @@ | ||
import global, { describe } from 'global'; | ||
import addons, { mockChannel } from '@storybook/addons'; | ||
import snapshotsTests from './snapshotsTestsTemplate'; | ||
import integrityTest from './integrityTestTemplate'; | ||
import getIntegrityOptions from './getIntegrityOptions'; | ||
import loadFramework from '../frameworks/frameworkLoader'; | ||
import Stories2SnapsConverter from '../Stories2SnapsConverter'; | ||
import { snapshotWithOptions } from '../test-bodies'; | ||
|
||
global.STORYBOOK_REACT_CLASSES = global.STORYBOOK_REACT_CLASSES || {}; | ||
|
||
const defaultStories2SnapsConverter = new Stories2SnapsConverter(); | ||
const methods = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll']; | ||
|
||
function ensureOptionsDefaults(options) { | ||
const { | ||
suite = 'Storyshots', | ||
storyNameRegex, | ||
storyKindRegex, | ||
renderer, | ||
serializer, | ||
stories2snapsConverter = defaultStories2SnapsConverter, | ||
test: testMethod = snapshotWithOptions({ renderer, serializer }), | ||
} = options; | ||
|
||
const integrityOptions = getIntegrityOptions(options); | ||
|
||
return { | ||
suite, | ||
storyNameRegex, | ||
storyKindRegex, | ||
stories2snapsConverter, | ||
testMethod, | ||
integrityOptions, | ||
}; | ||
} | ||
|
||
function callTestMethodGlobals(testMethod) { | ||
methods.forEach(method => { | ||
if (typeof testMethod[method] === 'function') { | ||
global[method](testMethod[method]); | ||
} | ||
}); | ||
} | ||
|
||
function testStorySnapshots(options = {}) { | ||
if (typeof describe !== 'function') { | ||
throw new Error('testStorySnapshots is intended only to be used inside jest'); | ||
} | ||
|
||
addons.setChannel(mockChannel()); | ||
|
||
const { storybook, framework, renderTree, renderShallowTree } = loadFramework(options); | ||
const storiesGroups = storybook.getStorybook(); | ||
|
||
if (storiesGroups.length === 0) { | ||
throw new Error('storyshots found 0 stories'); | ||
} | ||
|
||
const { | ||
suite, | ||
storyNameRegex, | ||
storyKindRegex, | ||
stories2snapsConverter, | ||
testMethod, | ||
integrityOptions, | ||
} = ensureOptionsDefaults(options); | ||
|
||
const testMethodParams = { | ||
renderTree, | ||
renderShallowTree, | ||
stories2snapsConverter, | ||
}; | ||
|
||
callTestMethodGlobals(testMethod); | ||
|
||
snapshotsTests({ | ||
groups: storiesGroups, | ||
suite, | ||
framework, | ||
storyKindRegex, | ||
storyNameRegex, | ||
testMethod, | ||
testMethodParams, | ||
}); | ||
|
||
integrityTest(integrityOptions, stories2snapsConverter); | ||
} | ||
|
||
export default testStorySnapshots; |
24 changes: 24 additions & 0 deletions
24
addons/storyshots/storyshots-core/src/api/integrityTestTemplate.js
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,24 @@ | ||
import fs from 'fs'; | ||
import glob from 'glob'; | ||
import { describe, it } from 'global'; | ||
|
||
function integrityTest(integrityOptions, stories2snapsConverter) { | ||
if (integrityOptions === false) { | ||
return; | ||
} | ||
|
||
describe('Storyshots Integrity', () => { | ||
it('Abandoned Storyshots', () => { | ||
const snapshotExtension = stories2snapsConverter.getSnapshotExtension(); | ||
const storyshots = glob.sync(`**/*${snapshotExtension}`, integrityOptions); | ||
|
||
const abandonedStoryshots = storyshots.filter(fileName => { | ||
const possibleStoriesFiles = stories2snapsConverter.getPossibleStoriesFiles(fileName); | ||
return !possibleStoriesFiles.some(fs.existsSync); | ||
}); | ||
expect(abandonedStoryshots).toHaveLength(0); | ||
}); | ||
}); | ||
} | ||
|
||
export default integrityTest; |
47 changes: 47 additions & 0 deletions
47
addons/storyshots/storyshots-core/src/api/snapshotsTestsTemplate.js
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,47 @@ | ||
import { describe, it } from 'global'; | ||
|
||
function snapshotTest({ story, kind, fileName, framework, testMethod, testMethodParams }) { | ||
const { name } = story; | ||
|
||
it(name, () => { | ||
const context = { fileName, kind, story: name, framework }; | ||
|
||
return testMethod({ | ||
story, | ||
context, | ||
...testMethodParams, | ||
}); | ||
}); | ||
} | ||
|
||
function snapshotTestSuite({ kind, stories, suite, storyNameRegex, ...restParams }) { | ||
describe(suite, () => { | ||
describe(kind, () => { | ||
// eslint-disable-next-line | ||
for (const story of stories) { | ||
if (storyNameRegex && !story.name.match(storyNameRegex)) { | ||
// eslint-disable-next-line | ||
continue; | ||
} | ||
|
||
snapshotTest({ story, kind, ...restParams }); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function snapshotsTests({ groups, storyKindRegex, ...restParams }) { | ||
// eslint-disable-next-line | ||
for (const group of groups) { | ||
const { fileName, kind, stories } = group; | ||
|
||
if (storyKindRegex && !kind.match(storyKindRegex)) { | ||
// eslint-disable-next-line | ||
continue; | ||
} | ||
|
||
snapshotTestSuite({ stories, kind, fileName, ...restParams }); | ||
} | ||
} | ||
|
||
export default snapshotsTests; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
31 changes: 31 additions & 0 deletions
31
addons/storyshots/storyshots-core/src/frameworks/frameworkLoader.js
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,31 @@ | ||
/* eslint-disable global-require,import/no-dynamic-require */ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const loaderScriptName = 'loader.js'; | ||
|
||
const isDirectory = source => fs.lstatSync(source).isDirectory(); | ||
|
||
function getLoaders() { | ||
return fs | ||
.readdirSync(__dirname) | ||
.map(name => path.join(__dirname, name)) | ||
.filter(isDirectory) | ||
.map(framework => path.join(framework, loaderScriptName)) | ||
.filter(fs.existsSync) | ||
.map(loader => require(loader).default); | ||
} | ||
|
||
function loadFramework(options) { | ||
const loaders = getLoaders(); | ||
|
||
const loader = loaders.find(frameworkLoader => frameworkLoader.test(options)); | ||
|
||
if (!loader) { | ||
throw new Error('storyshots is intended only to be used with storybook'); | ||
} | ||
|
||
return loader.load(options); | ||
} | ||
|
||
export default loadFramework; |
File renamed without changes.
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
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably worth mentioning in
MIGRATION.md
, as well as puppeteer moving to other package