From 2a0cd505e9ac45a8d3dd5f9523ea447bebebe8cf Mon Sep 17 00:00:00 2001 From: Malash Date: Wed, 29 May 2024 13:59:13 +0800 Subject: [PATCH] add tests --- .../postcssLinariaPreprocessor.test.ts.snap | 42 ++++++++++++++ .../postcssTransformUnit.test.ts.snap | 21 +++++++ .../postcssLinariaPreprocessor.test.ts | 57 +++++++++++++++++++ .../__tests__/postcssTransformUnit.test.ts | 49 ++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 packages/cli/src/config/__tests__/__snapshots__/postcssLinariaPreprocessor.test.ts.snap create mode 100644 packages/cli/src/config/__tests__/__snapshots__/postcssTransformUnit.test.ts.snap create mode 100644 packages/cli/src/config/__tests__/postcssLinariaPreprocessor.test.ts create mode 100644 packages/cli/src/config/__tests__/postcssTransformUnit.test.ts diff --git a/packages/cli/src/config/__tests__/__snapshots__/postcssLinariaPreprocessor.test.ts.snap b/packages/cli/src/config/__tests__/__snapshots__/postcssLinariaPreprocessor.test.ts.snap new file mode 100644 index 0000000..cddeba3 --- /dev/null +++ b/packages/cli/src/config/__tests__/__snapshots__/postcssLinariaPreprocessor.test.ts.snap @@ -0,0 +1,42 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`postcss-linaria-preprocessor :global() selector 1`] = ` +" + .a { + color: red + } + page { + width: 50vw; + } + " +`; + +exports[`postcss-linaria-preprocessor escape breaking control characters 1`] = ` +" + .a { + content: '\\\\feff'; + } + " +`; + +exports[`postcss-linaria-preprocessor keyframes rename 1`] = ` +" + .a { + animation: 1s ease 1 backwards normal zoomIn-a; + @keyframes zoomIn-a { + from { + opacity: 0; + transform: scale(0.94); + } + + 50% { + opacity: var(--opacity); + } + + to { + transform: scale(var(--scale)); + } + } + } + " +`; diff --git a/packages/cli/src/config/__tests__/__snapshots__/postcssTransformUnit.test.ts.snap b/packages/cli/src/config/__tests__/__snapshots__/postcssTransformUnit.test.ts.snap new file mode 100644 index 0000000..e6fb047 --- /dev/null +++ b/packages/cli/src/config/__tests__/__snapshots__/postcssTransformUnit.test.ts.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`postcss-transform-unit px to rpx 1`] = ` +" + .a { + width: 20rpx; + height: 20rpx; + font-size: 30px; + } + " +`; + +exports[`postcss-transform-unit rpx to px 1`] = ` +" + .a { + width: 10px; + height: 10px; + font-size: 30rpx; + } + " +`; diff --git a/packages/cli/src/config/__tests__/postcssLinariaPreprocessor.test.ts b/packages/cli/src/config/__tests__/postcssLinariaPreprocessor.test.ts new file mode 100644 index 0000000..9a8f694 --- /dev/null +++ b/packages/cli/src/config/__tests__/postcssLinariaPreprocessor.test.ts @@ -0,0 +1,57 @@ +import postcss from 'postcss'; + +const transform = async (css: string) => { + // eslint-disable-next-line global-require + const result = await postcss([require('../postcssLinariaPreprocessor')()]).process(css, { + from: '/path/to/file.css', + }); + return result.css; +}; + +describe('postcss-linaria-preprocessor', () => { + test(':global() selector', () => { + const css = ` + .a { + color: red; + :global() { + page { + width: 50vw; + } + } + } + `; + expect(transform(css)).resolves.toMatchSnapshot(); + }); + + test('keyframes rename', () => { + const css = ` + .a { + animation: 1s ease 1 backwards normal zoomIn; + @keyframes zoomIn { + from { + opacity: 0; + transform: scale(0.94); + } + + 50% { + opacity: var(--opacity); + } + + to { + transform: scale(var(--scale)); + } + } + } + `; + expect(transform(css)).resolves.toMatchSnapshot(); + }); + + test('escape breaking control characters', () => { + const css = ` + .a { + content: '\feff'; + } + `; + expect(transform(css)).resolves.toMatchSnapshot(); + }); +}); diff --git a/packages/cli/src/config/__tests__/postcssTransformUnit.test.ts b/packages/cli/src/config/__tests__/postcssTransformUnit.test.ts new file mode 100644 index 0000000..e47b35a --- /dev/null +++ b/packages/cli/src/config/__tests__/postcssTransformUnit.test.ts @@ -0,0 +1,49 @@ +import postcss from 'postcss'; + +describe('postcss-transform-unit', () => { + test('px to rpx', async () => { + const css = ` + .a { + width: 10px; + height: 20rpx; + font-size: 30px; /* no */ + } + `; + + const result = await postcss([ + // eslint-disable-next-line global-require + require('../postcssTransformUnit')({ + divisor: 1, + multiple: 2, + sourceUnit: 'px', + targetUnit: 'rpx', + }), + ]).process(css, { + from: '/path/to/file.css', + }); + expect(result.css).toMatchSnapshot(); + }); + + test('rpx to px', async () => { + const css = ` + .a { + width: 10px; + height: 20rpx; + font-size: 30rpx; /* no */ + } + `; + + const result = await postcss([ + // eslint-disable-next-line global-require + require('../postcssTransformUnit')({ + divisor: 2, + multiple: 1, + sourceUnit: 'rpx', + targetUnit: 'px', + }), + ]).process(css, { + from: '/path/to/file.css', + }); + expect(result.css).toMatchSnapshot(); + }); +});