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

Controls: Fix reset button broken for !undefined URL values #18231

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/preview-web/src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ export class Preview<TFramework extends AnyFramework> {
const render = this.storyRenders.find((r) => r.id === storyId);
const story = render?.story || (await this.storyStore.loadStory({ storyId }));

const argNamesToReset = argNames || Object.keys(this.storyStore.args.get(storyId));
const argNamesToReset = argNames || [
...new Set([
...Object.keys(story.initialArgs),
...Object.keys(this.storyStore.args.get(storyId)),
]),
];

const updatedArgs = argNamesToReset.reduce((acc, argName) => {
acc[argName] = story.initialArgs[argName];
return acc;
Expand Down
98 changes: 96 additions & 2 deletions lib/preview-web/src/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,8 @@ describe('PreviewWeb', () => {

it('resets a single arg', async () => {
document.location.search = '?id=component-one--a';
await createAndRenderPreview();
const preview = await createAndRenderPreview();
const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added expects for the call toonUpdateArgs. To me, it made it easier to understand what was going inside of onResetArgs. Please let me know if this is appropriate and what you think of the testing in general 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I just check the STORY_ARGS_UPDATED event as basically the same thing as checking onUpdateArgs, as that is the only place it is emitted. Probably the spy is a little more coupled to the implementation than strictly necessary but I'm happy to leave it.


mockChannel.emit.mockClear();
emitter.emit(Events.UPDATE_STORY_ARGS, {
Expand Down Expand Up @@ -1100,11 +1101,58 @@ describe('PreviewWeb', () => {
storyId: 'component-one--a',
args: { foo: 'a', new: 'value' },
});

expect(onUpdateArgsSpy).toHaveBeenCalledWith({
storyId: 'component-one--a',
updatedArgs: { foo: 'a' },
});
});

it('resets all args after one is updated', async () => {
document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs');

emitter.emit(Events.UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
updatedArgs: { foo: 'new' },
});
await waitForEvents([Events.STORY_ARGS_UPDATED]);

mockChannel.emit.mockClear();
emitter.emit(Events.RESET_STORY_ARGS, {
storyId: 'component-one--a',
});

await waitForRender();

expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith(
expect.objectContaining({
forceRemount: false,
storyContext: expect.objectContaining({
initialArgs: { foo: 'a' },
args: { foo: 'a' },
}),
}),
undefined // this is coming from view.prepareForStory, not super important
);

await waitForEvents([Events.STORY_ARGS_UPDATED]);
expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, {
storyId: 'component-one--a',
args: { foo: 'a' },
});

expect(onUpdateArgsSpy).toHaveBeenCalledWith({
storyId: 'component-one--a',
updatedArgs: { foo: 'a' },
});
});

it('resets all args', async () => {
document.location.search = '?id=component-one--a';
await createAndRenderPreview();
const preview = await createAndRenderPreview();
const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs');

emitter.emit(Events.UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
Expand Down Expand Up @@ -1135,6 +1183,52 @@ describe('PreviewWeb', () => {
storyId: 'component-one--a',
args: { foo: 'a' },
});

expect(onUpdateArgsSpy).toHaveBeenCalledWith({
storyId: 'component-one--a',
updatedArgs: { foo: 'a', new: undefined },
});
});

it('resets all args when one arg is undefined', async () => {
document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
const onUpdateArgsSpy = jest.spyOn(preview, 'onUpdateArgs');

emitter.emit(Events.UPDATE_STORY_ARGS, {
storyId: 'component-one--a',
updatedArgs: { foo: undefined },
});
await waitForEvents([Events.STORY_ARGS_UPDATED]);

mockChannel.emit.mockClear();
emitter.emit(Events.RESET_STORY_ARGS, {
storyId: 'component-one--a',
});

await waitForRender();

expect(projectAnnotations.renderToDOM).toHaveBeenCalledWith(
expect.objectContaining({
forceRemount: false,
storyContext: expect.objectContaining({
initialArgs: { foo: 'a' },
args: { foo: 'a' },
}),
}),
undefined // this is coming from view.prepareForStory, not super important
);

await waitForEvents([Events.STORY_ARGS_UPDATED]);
expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, {
storyId: 'component-one--a',
args: { foo: 'a' },
});

expect(onUpdateArgsSpy).toHaveBeenCalledWith({
storyId: 'component-one--a',
updatedArgs: { foo: 'a' },
});
});
});

Expand Down