-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
240 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { CarbonSvelte } from "../src/constants"; | ||
import OptimizeCssPlugin from "../src/plugins/OptimizeCssPlugin"; | ||
|
||
// Mock webpack compiler and related types | ||
const createMockCompiler = (options: any = {}) => { | ||
const { assets = {}, fileDependencies = [] } = options; | ||
|
||
const compilation = { | ||
hooks: { | ||
processAssets: { | ||
tap: jest.fn((_, callback) => { | ||
callback(assets); | ||
}), | ||
}, | ||
}, | ||
updateAsset: jest.fn(), | ||
}; | ||
|
||
const normalModuleHooks = { | ||
beforeSnapshot: { | ||
tap: jest.fn((_, callback) => { | ||
callback({ buildInfo: { fileDependencies } }); | ||
}), | ||
}, | ||
}; | ||
|
||
return { | ||
hooks: { | ||
thisCompilation: { | ||
tap: jest.fn((_, callback) => callback(compilation)), | ||
}, | ||
}, | ||
webpack: { | ||
Compilation: { | ||
PROCESS_ASSETS_STAGE_DERIVED: "PROCESS_ASSETS_STAGE_DERIVED", | ||
}, | ||
NormalModule: { | ||
getCompilationHooks: () => normalModuleHooks, | ||
}, | ||
sources: { | ||
RawSource: jest.fn((content) => ({ source: () => content })), | ||
}, | ||
}, | ||
compilation, | ||
normalModuleHooks, | ||
}; | ||
}; | ||
|
||
describe("OptimizeCssPlugin", () => { | ||
test("constructor sets default options correctly", () => { | ||
const plugin = new OptimizeCssPlugin(); | ||
expect((plugin as any).options).toEqual({ | ||
verbose: true, | ||
preserveAllIBMFonts: false, | ||
}); | ||
}); | ||
|
||
test("constructor respects provided options", () => { | ||
const plugin = new OptimizeCssPlugin({ | ||
verbose: false, | ||
preserveAllIBMFonts: true, | ||
}); | ||
expect((plugin as any).options).toEqual({ | ||
verbose: false, | ||
preserveAllIBMFonts: true, | ||
}); | ||
}); | ||
|
||
test("skips processing if no Carbon Svelte imports are found", () => { | ||
const plugin = new OptimizeCssPlugin(); | ||
const mockCompiler = createMockCompiler({ | ||
assets: { "styles.css": { source: () => "body { color: red; }" } }, | ||
fileDependencies: ["regular-component.svelte"], | ||
}); | ||
|
||
plugin.apply(mockCompiler as any); | ||
|
||
expect(mockCompiler.compilation.updateAsset).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("processes CSS files when Carbon Svelte imports are found", () => { | ||
const plugin = new OptimizeCssPlugin(); | ||
const carbonComponent = `node_modules/${CarbonSvelte.Components}/Button.svelte`; | ||
const cssContent = ".bx--btn { color: blue; }"; | ||
|
||
const mockCompiler = createMockCompiler({ | ||
assets: { | ||
"styles.css": { source: () => cssContent }, | ||
}, | ||
fileDependencies: [carbonComponent], | ||
}); | ||
|
||
plugin.apply(mockCompiler as any); | ||
|
||
expect(mockCompiler.compilation.updateAsset).toHaveBeenCalledWith( | ||
"styles.css", | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
test("handles Buffer input correctly", () => { | ||
const plugin = new OptimizeCssPlugin(); | ||
const carbonComponent = `node_modules/${CarbonSvelte.Components}/Button.svelte`; | ||
const cssContent = Buffer.from(".bx--btn { color: blue; }"); | ||
|
||
const mockCompiler = createMockCompiler({ | ||
assets: { | ||
"styles.css": { source: () => cssContent }, | ||
}, | ||
fileDependencies: [carbonComponent], | ||
}); | ||
|
||
plugin.apply(mockCompiler as any); | ||
|
||
expect(mockCompiler.compilation.updateAsset).toHaveBeenCalledWith( | ||
"styles.css", | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
test("respects verbose option for printing diff", () => { | ||
const consoleSpy = jest.spyOn(console, "log").mockImplementation(); | ||
const plugin = new OptimizeCssPlugin({ verbose: true }); | ||
const carbonComponent = `node_modules/${CarbonSvelte.Components}/Button.svelte`; | ||
|
||
const mockCompiler = createMockCompiler({ | ||
assets: { | ||
"styles.css": { source: () => ".bx--btn { color: blue; }" }, | ||
}, | ||
fileDependencies: [carbonComponent], | ||
}); | ||
|
||
plugin.apply(mockCompiler as any); | ||
|
||
expect(consoleSpy).toHaveBeenCalled(); | ||
consoleSpy.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { CarbonSvelte } from "../src/constants"; | ||
import { isCarbonSvelteImport, isCssFile, isSvelteFile } from "../src/utils"; | ||
|
||
describe("isSvelteFile", () => { | ||
test("returns true for .svelte files", () => { | ||
expect(isSvelteFile("Component.svelte")).toBe(true); | ||
expect(isSvelteFile("path/to/Component.svelte")).toBe(true); | ||
expect(isSvelteFile("./Component.svelte")).toBe(true); | ||
}); | ||
|
||
test("returns false for non-svelte files", () => { | ||
expect(isSvelteFile("Component.js")).toBe(false); | ||
expect(isSvelteFile("Component.css")).toBe(false); | ||
expect(isSvelteFile("Component")).toBe(false); | ||
expect(isSvelteFile("Component.svelte.js")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("isCssFile", () => { | ||
test("returns true for .css files", () => { | ||
expect(isCssFile("styles.css")).toBe(true); | ||
expect(isCssFile("path/to/styles.css")).toBe(true); | ||
expect(isCssFile("./styles.css")).toBe(true); | ||
}); | ||
|
||
test("returns false for non-css files", () => { | ||
expect(isCssFile("styles.scss")).toBe(false); | ||
expect(isCssFile("styles.less")).toBe(false); | ||
expect(isCssFile("styles")).toBe(false); | ||
expect(isCssFile("styles.css.js")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("isCarbonSvelteImport", () => { | ||
test("returns true for Carbon Svelte component imports", () => { | ||
expect( | ||
isCarbonSvelteImport( | ||
`node_modules/${CarbonSvelte.Components}/Button.svelte`, | ||
), | ||
).toBe(true); | ||
expect( | ||
isCarbonSvelteImport(`${CarbonSvelte.Components}/Button.svelte`), | ||
).toBe(true); | ||
}); | ||
|
||
test("returns false for non-Carbon Svelte imports", () => { | ||
expect(isCarbonSvelteImport("Button.svelte")).toBe(false); | ||
expect(isCarbonSvelteImport(`${CarbonSvelte.Icons}/Button.svelte`)).toBe( | ||
false, | ||
); | ||
expect(isCarbonSvelteImport(`${CarbonSvelte.Components}/Button.js`)).toBe( | ||
false, | ||
); | ||
expect(isCarbonSvelteImport("other-lib/Button.svelte")).toBe(false); | ||
}); | ||
}); |