Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Ensure files are written to the correct directory
Browse files Browse the repository at this point in the history
  • Loading branch information
khelif96 committed Jan 10, 2024
1 parent 5a165ab commit 1aafebe
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/storybook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { composeStories } from "@storybook/react";
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, testing-vue3)
import * as glob from "glob";
import "jest-specific-snapshot";
import MatchMediaMock from "jest-matchmedia-mock";
import path from "path";
import { renderWithRouterMatch as render } from "test_utils";
import { render } from "test_utils";
import { CustomMeta, CustomStoryObj } from "test_utils/types";
import * as projectAnnotations from "../.storybook/preview";

let matchMedia;

type StoryFile = {
default: CustomMeta<unknown>;
Expand All @@ -17,7 +21,7 @@ const compose = (
entry: StoryFile
): ReturnType<typeof composeStories<StoryFile>> => {
try {
return composeStories(entry);
return composeStories(entry, projectAnnotations);
} catch (e) {
throw new Error(
`There was an issue composing stories for the module: ${JSON.stringify(
Expand Down Expand Up @@ -47,11 +51,41 @@ const options = {
snapshotExtension: ".storyshot",
};

describe(`Snapshot Tests: ${options.suite}`, () => {
describe(`${options.suite}`, () => {
beforeAll(() => {
matchMedia = new MatchMediaMock();
});
beforeEach(() => {
const mockIntersectionObserver = jest.fn((callback) => {
callback([
{
isIntersecting: true,
},
]);
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});

// @ts-expect-error
window.IntersectionObserver = mockIntersectionObserver;
});

afterAll(() => {
matchMedia.clear();
jest.restoreAllMocks();
});
getAllStoryFiles().forEach((params) => {
const { filePath, storyFile } = params;
const meta = storyFile.default;
const { title } = meta;
const storyBookFileBaseName = path
.basename(filePath)
.replace(/\.[^/.]+$/, "");
// storyName is either the title of the story or the name of the file without the extension
const storyName = title || storyBookFileBaseName;
if (
options.storyKindRegex.test(title) ||
meta.parameters?.storyshots?.disable
Expand All @@ -60,7 +94,7 @@ describe(`Snapshot Tests: ${options.suite}`, () => {
return;
}

describe(`${title}`, () => {
describe(`${storyName}`, () => {
const stories = Object.entries(compose(storyFile))
.map(([name, story]) => ({ name, story }))
.filter(
Expand All @@ -78,10 +112,7 @@ describe(`Snapshot Tests: ${options.suite}`, () => {

stories.forEach(({ name, story }) => {
it(`${name}`, async () => {
const { container } = render(story(), {
path: story.parameters?.reactRouter?.path,
route: story.parameters?.reactRouter?.route,
});
const { container } = render(story());
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot.
await new Promise((resolve) => {
setTimeout(resolve, 1);
Expand All @@ -90,9 +121,8 @@ describe(`Snapshot Tests: ${options.suite}`, () => {
const snapshotPath = path.join(
storyDirectory,
options.snapshotsDirName,
`${name}${options.snapshotExtension}`
`${storyBookFileBaseName}${options.snapshotExtension}`
);
console.log(snapshotPath);
expect(container).toMatchSpecificSnapshot(snapshotPath);
});
});
Expand Down

0 comments on commit 1aafebe

Please sign in to comment.