Skip to content

Commit 81ee554

Browse files
committed
test: add unit tests for css modules and insert
1 parent 2b4f583 commit 81ee554

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

src/utils/processRenderResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const processRenderResponse = (
7979
* by rollup.
8080
*/
8181
imports.push(`import ${INSERT_STYLE_ID} from '${INSERT_STYLE_ID}';`);
82-
cssCode = `${INSERT_STYLE_ID}(${cssCode});`;
82+
cssCode = `${INSERT_STYLE_ID}(${cssCode})`;
8383
defaultExport = cssCode;
8484
} else if (!rollupOptions.output) {
8585
defaultExport = cssCode;

test/index.test.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -760,55 +760,55 @@ const createApiOptionTestCaseTitle: TitleFn<[RollupPluginSassOptions]> = (
760760
test(title, macro, TEST_PLUGIN_OPTIONS_DEFAULT_MODERN);
761761
}
762762

763-
// {
764-
// const title =
765-
// 'should produces CSS modules alongside `insertStyle` if `cssModules` is returned from processor';
766-
767-
// const macro = test.macro<[RollupPluginSassOptions]>({
768-
// async exec(t, pluginOptions) {
769-
// const outputBundle = await rollup({
770-
// input: 'test/fixtures/css-modules/index.js',
771-
// plugins: [
772-
// sass({
773-
// ...pluginOptions,
774-
// insert: true,
775-
// processor: postcssModulesProcessor,
776-
// }),
777-
// ],
778-
// });
779-
780-
// const { output } = await outputBundle.generate(TEST_GENERATE_OPTIONS);
781-
782-
// t.is(
783-
// output.length,
784-
// 1,
785-
// 'has 1 chunk (we are bundling all in one single file)',
786-
// );
787-
788-
// const [{ moduleIds, modules }] = output;
789-
790-
// t.is(
791-
// moduleIds.filter((it) => it.endsWith('insertStyle')).length,
792-
// 1,
793-
// 'include insertStyle one time',
794-
// );
795-
796-
// const actualAModuleID = moduleIds.find((it) =>
797-
// it.endsWith('actual_a.scss'),
798-
// ) as string;
799-
// const actualAModule = modules[actualAModuleID];
800-
// t.truthy(actualAModule);
801-
// t.snapshot(
802-
// actualAModule.code,
803-
// 'actual_a content is compiled with insertStyle',
804-
// );
805-
// },
806-
// title: createApiOptionTestCaseTitle,
807-
// });
808-
809-
// test.only(title, macro, TEST_PLUGIN_OPTIONS_DEFAULT_LEGACY);
810-
// test(title, macro, TEST_PLUGIN_OPTIONS_DEFAULT_MODERN);
811-
// }
763+
{
764+
const title =
765+
'should produces CSS modules alongside `insertStyle` if `cssModules` is returned from processor';
766+
767+
const macro = test.macro<[RollupPluginSassOptions]>({
768+
async exec(t, pluginOptions) {
769+
const outputBundle = await rollup({
770+
input: 'test/fixtures/css-modules/index.js',
771+
plugins: [
772+
sass({
773+
...pluginOptions,
774+
insert: true,
775+
processor: postcssModulesProcessor,
776+
}),
777+
],
778+
});
779+
780+
const { output } = await outputBundle.generate(TEST_GENERATE_OPTIONS);
781+
782+
t.is(
783+
output.length,
784+
1,
785+
'has 1 chunk (we are bundling all in one single file)',
786+
);
787+
788+
const [{ moduleIds, modules }] = output;
789+
790+
t.is(
791+
moduleIds.filter((it) => it.endsWith('insertStyle')).length,
792+
1,
793+
'include insertStyle one time',
794+
);
795+
796+
const styleModuleID = moduleIds.find((it) =>
797+
it.endsWith('style.scss'),
798+
) as string;
799+
const styleModule = modules[styleModuleID];
800+
t.truthy(styleModule);
801+
t.snapshot(
802+
styleModule.code,
803+
'style content is compiled with insertStyle',
804+
);
805+
},
806+
title: createApiOptionTestCaseTitle,
807+
});
808+
809+
test(title, macro, TEST_PLUGIN_OPTIONS_DEFAULT_LEGACY);
810+
test(title, macro, TEST_PLUGIN_OPTIONS_DEFAULT_MODERN);
811+
}
812812

813813
{
814814
const title = 'should throw error when CSS modules is not an object';

test/snapshots/test/index.test.ts.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ Generated by [AVA](https://avajs.dev).
268268
export { style as default };␊
269269
`
270270

271+
## should produces CSS modules alongside `insertStyle` if `cssModules` is returned from processor using 'api' = 'legacy' (implicit)
272+
273+
> style content is compiled with insertStyle
274+
275+
`insertStyle("._something_ngy8h_1{color:red}");␊
276+
var style = {"something":"_something_ngy8h_1"};`
277+
278+
## should produces CSS modules alongside `insertStyle` if `cssModules` is returned from processor using 'api' = 'modern'
279+
280+
> style content is compiled with insertStyle
281+
282+
`insertStyle("._something_ngy8h_1{color:red}");␊
283+
var style = {"something":"_something_ngy8h_1"};`
284+
271285
## should resolve ~ as node_modules using 'api' = 'legacy' (implicit)
272286

273287
> Snapshot 1
52 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)