Skip to content

Commit dbd5e5d

Browse files
committed
Add tests for CSS Modules with Preact
1 parent b212b15 commit dbd5e5d

File tree

11 files changed

+127
-4
lines changed

11 files changed

+127
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { h } from 'preact';
2+
import './styles.css';
3+
import './styles.less';
4+
import './styles.scss';
5+
import './styles.stylus';
6+
import stylesCss from './styles.module.css?module';
7+
import stylesLess from './styles.module.less?module';
8+
import stylesScss from './styles.module.scss?module';
9+
import stylesStylus from './styles.module.stylus?module';
10+
11+
export default function App() {
12+
return <div className={`red large justified lowercase ${stylesCss.italic} ${stylesLess.underline} ${stylesScss.bold} ${stylesStylus.rtl}`}></div>
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.red {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.justified {
2+
text-align: justify;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.italic {
2+
font-style: italic;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.underline {
2+
text-decoration: underline;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bold {
2+
font-weight: bold;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.rtl
2+
direction: rtl;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.large {
2+
font-size: 50px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.lowercase
2+
text-transform: lowercase

fixtures/preact-css-modules/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { h, render } from 'preact';
2+
3+
import App from './components/App';
4+
5+
render(<App />, document.getElementById('app'));

test/functional.js

+87-4
Original file line numberDiff line numberDiff line change
@@ -1684,13 +1684,13 @@ module.exports = {
16841684
expectClassDeclaration('large'); // Standard SCSS
16851685
expectClassDeclaration('justified'); // Standard Less
16861686
expectClassDeclaration('lowercase'); // Standard Stylus
1687-
expectClassDeclaration('block'); // Standard Postcss
1687+
expectClassDeclaration('block'); // Standard PostCSS
16881688

16891689
expectClassDeclaration('italic_foo'); // CSS Module
16901690
expectClassDeclaration('bold_foo'); // SCSS Module
16911691
expectClassDeclaration('underline_foo'); // Less Module
16921692
expectClassDeclaration('rtl_foo'); // Stylus Module
1693-
expectClassDeclaration('hidden_foo'); // Stylus Module
1693+
expectClassDeclaration('hidden_foo'); // PostCSS Module
16941694

16951695
testSetup.requestTestPage(
16961696
browser,
@@ -1706,20 +1706,103 @@ module.exports = {
17061706
expect(divClassArray.includes('large')).to.be.true; // Standard SCSS
17071707
expect(divClassArray.includes('justified')).to.be.true; // Standard Less
17081708
expect(divClassArray.includes('lowercase')).to.be.true; // Standard Stylus
1709-
expect(divClassArray.includes('block')).to.be.true; // Standard Stylus
1709+
expect(divClassArray.includes('block')).to.be.true; // Standard PostCSS
17101710

17111711
expect(divClassArray.includes('italic_foo')).to.be.true; // CSS module
17121712
expect(divClassArray.includes('bold_foo')).to.be.true; // SCSS module
17131713
expect(divClassArray.includes('underline_foo')).to.be.true; // Less module
17141714
expect(divClassArray.includes('rtl_foo')).to.be.true; // Stylus module
1715-
expect(divClassArray.includes('hidden_foo')).to.be.true; // Stylus module
1715+
expect(divClassArray.includes('hidden_foo')).to.be.true; // PostCSS module
17161716

17171717
done();
17181718
}
17191719
);
17201720
});
17211721
});
17221722

1723+
it('Preact supports CSS/Sass/Less/Stylus modules', (done) => {
1724+
const appDir = testSetup.createTestAppDir();
1725+
const config = testSetup.createWebpackConfig(appDir, 'www/build', 'dev');
1726+
config.enableSingleRuntimeChunk();
1727+
config.setPublicPath('/build');
1728+
config.addEntry('main', './preact-css-modules/main.js');
1729+
config.enablePreactPreset();
1730+
config.enableSassLoader();
1731+
config.enableLessLoader();
1732+
config.enableStylusLoader();
1733+
config.configureCssLoader(options => {
1734+
// Remove hashes from local ident names
1735+
// since they are not always the same.
1736+
if (options.modules) {
1737+
options.modules.localIdentName = '[local]_foo';
1738+
}
1739+
});
1740+
1741+
// Enable the PostCSS loader so we can use `lang="postcss"`
1742+
config.enablePostCssLoader();
1743+
fs.writeFileSync(
1744+
path.join(appDir, 'postcss.config.js'),
1745+
`
1746+
module.exports = {
1747+
plugins: [
1748+
require('autoprefixer')()
1749+
]
1750+
} `
1751+
);
1752+
1753+
testSetup.runWebpack(config, (webpackAssert) => {
1754+
expect(config.outputPath).to.be.a.directory().with.deep.files([
1755+
'main.js',
1756+
'main.css',
1757+
'manifest.json',
1758+
'entrypoints.json',
1759+
'runtime.js',
1760+
]);
1761+
1762+
const expectClassDeclaration = (className) => {
1763+
webpackAssert.assertOutputFileContains(
1764+
'main.css',
1765+
`.${className} {`
1766+
);
1767+
};
1768+
1769+
expectClassDeclaration('red'); // Standard CSS
1770+
expectClassDeclaration('large'); // Standard SCSS
1771+
expectClassDeclaration('justified'); // Standard Less
1772+
expectClassDeclaration('lowercase'); // Standard Stylus
1773+
1774+
expectClassDeclaration('italic_foo'); // CSS Module
1775+
expectClassDeclaration('bold_foo'); // SCSS Module
1776+
expectClassDeclaration('underline_foo'); // Less Module
1777+
expectClassDeclaration('rtl_foo'); // Stylus Module
1778+
1779+
testSetup.requestTestPage(
1780+
browser,
1781+
path.join(config.getContext(), 'www'),
1782+
[
1783+
'build/runtime.js',
1784+
'build/main.js'
1785+
],
1786+
async({ page }) => {
1787+
const divClassArray = await page.evaluate(() => Array.from(document.body.querySelector('#app > div').classList.values()));
1788+
1789+
expect(divClassArray.includes('red')).to.be.true; // Standard CSS
1790+
expect(divClassArray.includes('large')).to.be.true; // Standard SCSS
1791+
expect(divClassArray.includes('justified')).to.be.true; // Standard Less
1792+
expect(divClassArray.includes('lowercase')).to.be.true; // Standard Stylus
1793+
1794+
expect(divClassArray.includes('italic_foo')).to.be.true; // CSS module
1795+
expect(divClassArray.includes('bold_foo')).to.be.true; // SCSS module
1796+
expect(divClassArray.includes('underline_foo')).to.be.true; // Less module
1797+
expect(divClassArray.includes('rtl_foo')).to.be.true; // Stylus module
1798+
1799+
done();
1800+
}
1801+
);
1802+
});
1803+
});
1804+
1805+
17231806
it('Vue.js error when using non-activated loaders', (done) => {
17241807
const config = createWebpackConfig('www/build', 'dev');
17251808
config.setPublicPath('/build');

0 commit comments

Comments
 (0)