Skip to content

Commit

Permalink
improve readability of StoryRender.test flow
Browse files Browse the repository at this point in the history
  • Loading branch information
JReinhold committed Apr 5, 2024
1 parent c39272a commit 6f6fe5f
Showing 1 changed file with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const createGate = (): [Promise<void>, () => void] => {
});
return [gate, openGate];
};
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));

window.location = { reload: vi.fn() } as any;

Expand Down Expand Up @@ -127,18 +128,19 @@ describe('StoryRender', () => {
});

it('reloads the page when tearing down during loading', async () => {
// Arrange - setup StoryRender and async gate blocking applyLoaders
const [loaderGate, openLoaderGate] = createGate();
const story = {
id: 'id',
title: 'title',
name: 'name',
tags: [],
applyLoaders: vi.fn(),
applyLoaders: vi.fn(() => loaderGate),
unboundStoryFn: vi.fn(),
playFunction: vi.fn(),
prepareContext: vi.fn(),
};
const store = { getStoryContext: () => ({}), cleanupStory: vi.fn() };

const render = new StoryRender(
new Channel({}),
store as any,
Expand All @@ -150,16 +152,24 @@ describe('StoryRender', () => {
story as any
);

story.applyLoaders.mockImplementation(() => teardownAndWaitForReload(render));

await render.renderToElement({} as any);

// Act - render, blocked by loaders, teardown
// ... Assert - window is reloaded
const renderPromise = render.renderToElement({} as any);
expect(story.applyLoaders).toHaveBeenCalledOnce();
await teardownAndWaitForReload(render);

// Assert - everything is actually cleaned up, just in case
expect(store.cleanupStory).toHaveBeenCalledOnce();
expect(window.location.reload).toHaveBeenCalledOnce();

// clear dangling promise
openLoaderGate();
await renderPromise;
});

it('reloads the page when tearing down during rendering', async () => {
// Arrange - setup StoryRender and async gate blocking renderToScreen
const [renderGate, openRenderGate] = createGate();
const story = {
id: 'id',
title: 'title',
Expand All @@ -171,7 +181,7 @@ describe('StoryRender', () => {
prepareContext: vi.fn(),
};
const store = { getStoryContext: () => ({}), cleanupStory: vi.fn() };
const renderToScreen = vi.fn();
const renderToScreen = vi.fn(() => renderGate);

const render = new StoryRender(
new Channel({}),
Expand All @@ -184,24 +194,33 @@ describe('StoryRender', () => {
story as any
);

renderToScreen.mockImplementation(() => teardownAndWaitForReload(render));

await render.renderToElement({} as any);

// Act - render, blocked by renderToScreen, teardown
// ... Assert - window is reloaded
const renderPromise = render.renderToElement({} as any);
await tick(); // go from 'loading' to 'rendering' phase
expect(renderToScreen).toHaveBeenCalledOnce();
await teardownAndWaitForReload(render);

// Assert - everything is actually cleaned up, just in case
expect(store.cleanupStory).toHaveBeenCalledOnce();
expect(window.location.reload).toHaveBeenCalledOnce();

// clear dangling promise
openRenderGate();
await renderPromise;
});

it('reloads the page when tearing down during playing', async () => {
// Arrange - setup StoryRender and async gate blocking playing
const [playGate, openPlayGate] = createGate();
const story = {
id: 'id',
title: 'title',
name: 'name',
tags: [],
applyLoaders: vi.fn(),
unboundStoryFn: vi.fn(),
playFunction: vi.fn(),
playFunction: vi.fn(() => playGate),
prepareContext: vi.fn(),
};
const store = { getStoryContext: () => ({}), cleanupStory: vi.fn() };
Expand All @@ -217,13 +236,20 @@ describe('StoryRender', () => {
story as any
);

story.playFunction.mockImplementation(() => teardownAndWaitForReload(render));

await render.renderToElement({} as any);

// Act - render, blocked by renderToScreen, teardown
// ... Assert - window is reloaded
const renderPromise = render.renderToElement({} as any);
await tick(); // go from 'loading' to 'playing' phase
expect(story.playFunction).toHaveBeenCalledOnce();
await teardownAndWaitForReload(render);

// Assert - everything is actually cleaned up, just in case
expect(store.cleanupStory).toHaveBeenCalledOnce();
expect(window.location.reload).toHaveBeenCalledOnce();

// clear dangling promise
openPlayGate();
await renderPromise;
});
});
});

0 comments on commit 6f6fe5f

Please sign in to comment.