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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Ensure that simultaneous onStoriesChanged don't clobber each other #26882

Merged
merged 5 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions code/lib/preview-api/src/modules/preview-web/PreviewWeb.test.ts
Expand Up @@ -2735,6 +2735,60 @@ describe('PreviewWeb', () => {
});
});

describe('if called twice simultaneously', () => {
it('does not get renders confused', async () => {
const [importGate, openImportGate] = createGate();
const [importedGate, openImportedGate] = createGate();
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
const newImportFn = vi.fn(async (path) => {
openImportedGate();
await importGate;
return importFn(path);
});

document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
mockChannel.emit.mockClear();

preview.onStoriesChanged({ importFn: newImportFn });
await importedGate;
preview.onStoriesChanged({ importFn });

openImportGate();
await waitForRender();

expect(preview.storyRenders.length).toEqual(1);
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
});

it('renders the second importFn', async () => {
const [importGate, openImportGate] = createGate();
const [importedGate, openImportedGate] = createGate();
const secondImportFn = vi.fn(async (path) => {
openImportedGate();
await importGate;
return importFn(path);
});

const thirdImportFn = vi.fn(async (path) => {
openImportedGate();
await importGate;
return importFn(path);
});

document.location.search = '?id=component-one--a';
const preview = await createAndRenderPreview();
mockChannel.emit.mockClear();

preview.onStoriesChanged({ importFn: secondImportFn });
await importedGate;
preview.onStoriesChanged({ importFn: thirdImportFn });

openImportGate();
await waitForRender();

expect(thirdImportFn).toHaveBeenCalled();
});
});

describe('when the current story changes', () => {
const newComponentOneExports = merge({}, componentOneExports, {
a: { args: { foo: 'edited' } },
Expand Down
Expand Up @@ -352,12 +352,8 @@ export class PreviewWithSelection<TRenderer extends Renderer> extends Preview<TR
try {
await render.prepare();
} catch (err) {
if (err !== PREPARE_ABORTED) {
// We are about to render an error so make sure the previous story is
// no longer rendered.
if (lastRender) await this.teardownRender(lastRender);
Comment on lines -356 to -358
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'm really unsure why the logic wouldn't have applied in the aborted cased too (the lastRender is out of date no matter what!). The tests from the PR that added this code #17599 still pass so I think it's good.

this.renderStoryLoadingException(storyId, err as Error);
}
if (lastRender) await this.teardownRender(lastRender);
if (err !== PREPARE_ABORTED) this.renderStoryLoadingException(storyId, err as Error);
return;
}

Expand Down