From 1e82f5405bf17001aaf138faa974827c0ec551cd Mon Sep 17 00:00:00 2001 From: patricklafrance Date: Sun, 15 Dec 2024 16:03:38 -0500 Subject: [PATCH] More stuff --- .../rsbuild-configs/tests/storybook.test.ts | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 packages/rsbuild-configs/tests/storybook.test.ts diff --git a/packages/rsbuild-configs/tests/storybook.test.ts b/packages/rsbuild-configs/tests/storybook.test.ts new file mode 100644 index 00000000..d14baa70 --- /dev/null +++ b/packages/rsbuild-configs/tests/storybook.test.ts @@ -0,0 +1,170 @@ +import type { RsbuildConfig, RsbuildPlugin, SourceMap } from "@rsbuild/core"; +import type { RsbuildConfigTransformer } from "../src/applyTransformers.ts"; +import { defineStorybookConfig } from "../src/storybook.ts"; + +test("when additional plugins are provided, append the provided plugins at the end of the plugins array", () => { + const plugin1: RsbuildPlugin = { + name: "plugin-1", + setup: () => {} + }; + + const plugin2: RsbuildPlugin = { + name: "plugin-2", + setup: () => {} + }; + + const result = defineStorybookConfig({ + plugins: [ + plugin1, + plugin2 + ] + }); + + const pluginsCount = result.plugins!.length; + + expect(result.plugins![pluginsCount - 2]).toBe(plugin1); + expect(result.plugins![pluginsCount - 1]).toBe(plugin2); +}); + +test("when sourceMap is false, the output.sourceMap option is false", () => { + const result = defineStorybookConfig({ + sourceMap: false + }); + + expect(result.output?.sourceMap).toBeFalsy(); +}); + +test("when sourceMap is an object, the output.sourceMap option is the object", () => { + const sourceMap: SourceMap = { + js: false, + css: false + }; + + const result = defineStorybookConfig({ + sourceMap + }); + + expect(result.output?.sourceMap).toBe(sourceMap); +}); + +test("when react is false, the react plugin is not included", () => { + const result = defineStorybookConfig({ + react: false + }); + + const plugin = result.plugins?.find(x => (x as RsbuildPlugin).name === "rsbuild:react"); + + expect(plugin).toBeUndefined(); +}); + +test("when react is a function, the function is executed", () => { + const fct = jest.fn(); + + defineStorybookConfig({ + react: fct + }); + + expect(fct).toHaveBeenCalledTimes(1); +}); + +test("when svgr is false, the svgr plugin is not included", () => { + const result = defineStorybookConfig({ + svgr: false + }); + + const plugin = result.plugins?.find(x => (x as RsbuildPlugin).name === "rsbuild:svgr"); + + expect(plugin).toBeUndefined(); +}); + +test("when svgr is a function, the function is executed", () => { + const fct = jest.fn(); + + defineStorybookConfig({ + svgr: fct + }); + + expect(fct).toHaveBeenCalledTimes(1); +}); + +test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the Rsbuild config", () => { + const entryTransformer: RsbuildConfigTransformer = (config: RsbuildConfig) => { + config.source = config.source ?? {}; + config.source.entry = { + index: "a-custom-value-in-a-transformer" + }; + + return config; + }; + + const result = defineStorybookConfig({ + transformers: [entryTransformer] + }); + + expect(result.source!.entry!.index).toBe("a-custom-value-in-a-transformer"); +}); + +test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the Rsbuild config", () => { + const entryTransformer: RsbuildConfigTransformer = () => { + return { + source: { + entry: { + index: "a-custom-value-in-a-transformer" + } + } + }; + }; + + const result = defineStorybookConfig({ + transformers: [entryTransformer] + }); + + expect(result.source!.entry!.index).toBe("a-custom-value-in-a-transformer"); +}); + +test("when multiple transformers are provided, all the transformers are applied on the webpack config", () => { + const entryTransformer: RsbuildConfigTransformer = (config: RsbuildConfig) => { + config.source = config.source ?? {}; + config.source.entry = { + index: "a-custom-value-in-a-transformer" + }; + + return config; + }; + + const distPathTransformer: RsbuildConfigTransformer = (config: RsbuildConfig) => { + config.output = config.output ?? {}; + config.output.distPath = config.output.distPath ?? {}; + config.output.distPath.js = "a-custom-dist-path-in-a-tranformer"; + + return config; + }; + + const result = defineStorybookConfig({ + transformers: [entryTransformer, distPathTransformer] + }); + + expect(result.source!.entry!.index).toBe("a-custom-value-in-a-transformer"); + expect(result.output!.distPath!.js).toBe("a-custom-dist-path-in-a-tranformer"); +}); + +test("transformers context environment is \"dev\"", () => { + const mockTransformer = jest.fn(); + + defineStorybookConfig({ + transformers: [mockTransformer] + }); + + expect(mockTransformer).toHaveBeenCalledWith(expect.anything(), { environment: "storybook", verbose: false }); +}); + +test("when the verbose option is true, the transformers context verbose value is \"true\"", () => { + const mockTransformer = jest.fn(); + + defineStorybookConfig({ + verbose: true, + transformers: [mockTransformer] + }); + + expect(mockTransformer).toHaveBeenCalledWith(expect.anything(), { environment: "storybook", verbose: true }); +});