-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'storybookjs:next' into fix-deps-on-sharp-errors
- Loading branch information
Showing
16 changed files
with
360 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,26 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import type { LoaderFunction } from '@storybook/types'; | ||
import { global } from '@storybook/global'; | ||
import type { onMockCall as onMockCallType } from '@storybook/test'; | ||
import { action } from './runtime'; | ||
|
||
export const tinySpyInternalState = Symbol.for('tinyspy:spy'); | ||
let subscribed = false; | ||
|
||
const attachActionsToFunctionMocks: LoaderFunction = (context) => { | ||
const logActionsWhenMockCalled: LoaderFunction = (context) => { | ||
const { | ||
args, | ||
parameters: { actions }, | ||
} = context; | ||
if (actions?.disable) return; | ||
|
||
Object.entries(args) | ||
.filter( | ||
([, value]) => | ||
typeof value === 'function' && '_isMockFunction' in value && value._isMockFunction | ||
) | ||
.forEach(([key, value]) => { | ||
// See this discussion for context: | ||
// https://github.com/vitest-dev/vitest/pull/5352 | ||
const previous = | ||
value.getMockImplementation() ?? | ||
(tinySpyInternalState in value ? value[tinySpyInternalState]?.getOriginal() : undefined); | ||
if (previous?._actionAttached !== true && previous?.isAction !== true) { | ||
const implementation = (...params: unknown[]) => { | ||
action(key)(...params); | ||
return previous?.(...params); | ||
}; | ||
implementation._actionAttached = true; | ||
value.mockImplementation(implementation); | ||
} | ||
}); | ||
if ( | ||
!subscribed && | ||
'__STORYBOOK_TEST_ON_MOCK_CALL__' in global && | ||
typeof global.__STORYBOOK_TEST_ON_MOCK_CALL__ === 'function' | ||
) { | ||
const onMockCall = global.__STORYBOOK_TEST_ON_MOCK_CALL__ as typeof onMockCallType; | ||
onMockCall((mock, args) => action(mock.getMockName())(args)); | ||
subscribed = true; | ||
} | ||
}; | ||
|
||
export const loaders: LoaderFunction[] = [attachActionsToFunctionMocks]; | ||
export const loaders: LoaderFunction[] = [logActionsWhenMockCalled]; |
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 { global as globalThis } from '@storybook/global'; | ||
import { spyOn } from '@storybook/test'; | ||
|
||
export default { | ||
component: globalThis.Components.Button, | ||
loaders() { | ||
spyOn(console, 'log').mockName('console.log'); | ||
}, | ||
args: { | ||
label: 'Button', | ||
}, | ||
parameters: { | ||
chromatic: { disable: true }, | ||
}, | ||
}; | ||
|
||
export const ShowSpyOnInActions = { | ||
args: { | ||
onClick: () => { | ||
console.log('first'); | ||
console.log('second'); | ||
}, | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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,61 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import type { StorybookConfig } from '@storybook/types'; | ||
import { vta } from './vta'; | ||
|
||
const check = async ({ | ||
packageManager, | ||
main: mainConfig, | ||
storybookVersion = '7.0.0', | ||
}: { | ||
packageManager: any; | ||
main: Partial<StorybookConfig> & Record<string, unknown>; | ||
storybookVersion?: string; | ||
}) => { | ||
return vta.check({ | ||
packageManager, | ||
configDir: '', | ||
mainConfig: mainConfig as any, | ||
storybookVersion, | ||
}); | ||
}; | ||
|
||
describe('no-ops', () => { | ||
it('with addon setup', async () => { | ||
await expect( | ||
check({ | ||
packageManager: {}, | ||
main: { | ||
addons: ['@chromatic-com/storybook'], | ||
}, | ||
}) | ||
).resolves.toBeFalsy(); | ||
}); | ||
it('with addon setup with options', async () => { | ||
await expect( | ||
check({ | ||
packageManager: {}, | ||
main: { | ||
addons: [ | ||
{ | ||
name: '@chromatic-com/storybook', | ||
options: {}, | ||
}, | ||
], | ||
}, | ||
}) | ||
).resolves.toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('continue', () => { | ||
it('no addons', async () => { | ||
await expect( | ||
check({ | ||
packageManager: {}, | ||
main: { | ||
stories: ['**/*.stories.mdx'], | ||
}, | ||
}) | ||
).resolves.toBeTruthy(); | ||
}); | ||
}); |
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,55 @@ | ||
import { dedent } from 'ts-dedent'; | ||
import chalk from 'chalk'; | ||
import { getAddonNames, updateMainConfig } from '../helpers/mainConfigFile'; | ||
import type { Fix } from '../types'; | ||
|
||
const logger = console; | ||
|
||
interface Options {} | ||
|
||
/** | ||
*/ | ||
export const vta: Fix<Options> = { | ||
id: 'visual-tests-addon', | ||
|
||
versionRange: ['<8.0.7', '>=8.0.7'], | ||
|
||
async check({ mainConfig }) { | ||
const hadAddonInstalled = getAddonNames(mainConfig).some((addon) => | ||
addon.includes('@chromatic-com/storybook') | ||
); | ||
|
||
const skip = hadAddonInstalled; | ||
|
||
if (skip) { | ||
return null; | ||
} | ||
|
||
return {}; | ||
}, | ||
|
||
prompt() { | ||
return dedent` | ||
New to Storybook 8: Storybook's Visual Tests addon helps you catch unintentional changes/bugs in your stories. The addon is powered by Chromatic, a cloud-based testing tool developed by Storybook's core team. | ||
Learn more: ${chalk.yellow('storybook.js.org/docs/writing-tests/visual-testing')} | ||
Install Visual Tests addon in your project? | ||
`; | ||
}, | ||
|
||
async run({ packageManager, dryRun, mainConfigPath, skipInstall }) { | ||
if (!dryRun) { | ||
const packageJson = await packageManager.retrievePackageJson(); | ||
await packageManager.addDependencies( | ||
{ installAsDevDependencies: true, skipInstall, packageJson }, | ||
[`@chromatic-com/storybook@^1`] | ||
); | ||
|
||
await updateMainConfig({ mainConfigPath, dryRun: !!dryRun }, async (main) => { | ||
logger.info(`✅ Adding "@chromatic-com/storybook" addon`); | ||
main.appendValueToArray(['addons'], '@chromatic-dom/storybook'); | ||
}); | ||
} | ||
}, | ||
}; |
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.