Skip to content

Commit

Permalink
Merge pull request #24451 from storybookjs/kasper/fix-attach-spy
Browse files Browse the repository at this point in the history
Action: Make sure that actions get attached spies across stories when defined in meta.
  • Loading branch information
kasperpeulen authored Oct 26, 2023
2 parents f505c63 + 8d51ad7 commit c6ffb6c
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions code/addons/interactions/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const fn = JestMock.fn.bind(JestMock);
// Aliasing `fn` to `action` here, so we get a more descriptive label in the UI.
const { action } = instrument({ action: fn }, { retain: true });
const channel = addons.getChannel();
const seen = new Set<any>();
const spies: any[] = [];

channel.on(FORCE_REMOUNT, () => spies.forEach((mock) => mock?.mockClear?.()));
Expand All @@ -28,8 +27,6 @@ channel.on(STORY_RENDER_PHASE_CHANGED, ({ newPhase }) => {
});

const addSpies = (id: string, val: any, key?: string): any => {
if (seen.has(val)) return val;
seen.add(val);
try {
if (Object.prototype.toString.call(val) === '[object Object]') {
// We have to mutate the original object for this to survive HMR.
Expand All @@ -40,7 +37,8 @@ const addSpies = (id: string, val: any, key?: string): any => {
if (Array.isArray(val)) {
return val.map((item, index) => addSpies(id, item, `${key}[${index}]`));
}
if (typeof val === 'function' && val.isAction) {
// eslint-disable-next-line no-underscore-dangle
if (typeof val === 'function' && val.isAction && !val._isMockFunction) {
Object.defineProperty(val, 'name', { value: key, writable: false });
Object.defineProperty(val, '__storyId__', { value: id, writable: false });
const spy = action(val);
Expand Down

0 comments on commit c6ffb6c

Please sign in to comment.