-
-
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 'next' into norbert/vta-added-on-upgrade
- Loading branch information
Showing
13 changed files
with
242 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 |
---|---|---|
@@ -1,8 +1,46 @@ | ||
import { it } from 'vitest'; | ||
import { expect, fn } from '@storybook/test'; | ||
import { describe, it, test } from 'vitest'; | ||
import { expect, fn, isMockFunction, traverseArgs } from '@storybook/test'; | ||
import { action } from '@storybook/addon-actions'; | ||
|
||
it('storybook expect and fn can be used in vitest test', () => { | ||
const spy = fn(); | ||
spy(1); | ||
expect(spy).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
describe('traverseArgs', () => { | ||
const args = { | ||
deep: { | ||
deeper: { | ||
fnKey: fn(), | ||
actionKey: action('name'), | ||
}, | ||
}, | ||
arg2: Object.freeze({ frozen: true }), | ||
}; | ||
|
||
expect(args.deep.deeper.fnKey.getMockName()).toEqual('spy'); | ||
|
||
const traversed = traverseArgs(args) as typeof args; | ||
|
||
test('The same structure is maintained', () => | ||
expect(traversed).toEqual({ | ||
deep: { | ||
deeper: { | ||
fnKey: args.deep.deeper.fnKey, | ||
actionKey: args.deep.deeper.actionKey, | ||
}, | ||
}, | ||
// We don't mutate frozen objects, but we do insert them back in the tree | ||
arg2: args.arg2, | ||
})); | ||
|
||
test('The mock name is mutated to be the arg key', () => | ||
expect(traversed.deep.deeper.fnKey.getMockName()).toEqual('fnKey')); | ||
|
||
const actionFn = traversed.deep.deeper.actionKey; | ||
|
||
test('Actions are wrapped in a spy', () => expect(isMockFunction(actionFn)).toBeTruthy()); | ||
test('The spy of the action is also matching the arg key ', () => | ||
expect(isMockFunction(actionFn) && actionFn.getMockName()).toEqual('actionKey')); | ||
}); |
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,15 @@ | ||
import { it, vi, expect, beforeEach } from 'vitest'; | ||
import { fn, onMockCall } from './spy'; | ||
|
||
const vitestSpy = vi.fn(); | ||
|
||
beforeEach(() => { | ||
const unsubscribe = onMockCall(vitestSpy); | ||
return () => unsubscribe(); | ||
}); | ||
|
||
it('mocks are reactive', () => { | ||
const storybookSpy = fn(); | ||
storybookSpy(1); | ||
expect(vitestSpy).toHaveBeenCalledWith(storybookSpy, [1]); | ||
}); |
Oops, something went wrong.