-
-
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.
- Loading branch information
1 parent
726a80a
commit 412b525
Showing
2 changed files
with
40 additions
and
44 deletions.
There are no files selected for viewing
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
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/src'; | ||
|
||
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')); | ||
}); |