Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action: Attach spies on actions across stories when defined in meta #24451

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading