-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 #3745 from storybooks/storyshots-refactoring
Storyshots addon refactoring
- Loading branch information
Showing
30 changed files
with
312 additions
and
162 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
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
Oops, something went wrong.