Skip to content

Commit

Permalink
Move test
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Apr 9, 2024
1 parent 726a80a commit 412b525
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
42 changes: 0 additions & 42 deletions code/addons/interactions/src/preview.test.ts

This file was deleted.

42 changes: 40 additions & 2 deletions code/lib/test/src/index.test.ts
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'));
});

0 comments on commit 412b525

Please sign in to comment.