diff --git a/README.md b/README.md index 3ca64db..5ca9134 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Pass an object of the following properties | `localIdentName` | `{String}` | `"[local]-[hash:base64:6]"` | A rule using any available token from [webpack interpolateName](https://github.com/webpack/loader-utils#interpolatename) | | `includePaths` | `{Array}` | `[]` (Any) | An array of paths to be processed | | `getLocalIdent` | `Function` | `undefined` | Generate the classname by specifying a function instead of using the built-in interpolation | +| `strict` | `Boolean` | `false` | When true, an exception is raised when a class is used while not being defined in `\nRed'; -const sourceShorthand = '\nRed'; +const style = ''; +const source = style + '\nRed'; +const sourceShorthand = style + '\nRed'; test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => { const output = await compiler({ @@ -55,3 +56,188 @@ test('[Shorthand] Avoid generated class to end with a hyphen', async () => { }); expect(output).toBe('\nRed'); }); + +describe('combining multiple classes', () => { + const style = ''; + const source = style + '\nRed'; + + const expectedStyle = + ''; + const expectedOutput = expectedStyle + '\nRed'; + + test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => { + const output = await compiler( + { + source, + }, + { + localIdentName: '[local]-123456', + } + ); + + expect(output).toBe(expectedOutput); + }); +}); + +describe('Classname is part of a selector', () => { + + test('CSS Modules class targetting children', async () => { + const source = + '\n' + + '
Red*
'; + + const expectedOutput = + '\n' + + '
Red*
'; + + const output = await compiler( + { + source, + }, + { + localIdentName: '[local]-123', + } + ); + + expect(output).toBe(expectedOutput); + }); + + test('CSS Modules class has a parent', async () => { + const source = + '\n' + + '
Red
'; + + const expectedOutput = + '\n' + + '
Red
'; + + const output = await compiler( + { + source, + }, + { + localIdentName: '[local]-123', + } + ); + + expect(output).toBe(expectedOutput); + }); + + test('CSS Modules class has a global parent', async () => { + const source = + '\n' + + '
Red
'; + + const expectedOutput = + '\n' + + '
Red
'; + + const output = await compiler( + { + source, + }, + { + localIdentName: '[local]-123', + } + ); + + expect(output).toBe(expectedOutput); + }); + + test('CSS Modules class is used within a media query', async () => { + const source = + '\n' + + '
Red
'; + + const expectedOutput = + '\n' + + '
Red
'; + + const output = await compiler( + { + source, + }, + { + localIdentName: '[local]-123', + } + ); + + expect(output).toBe(expectedOutput); + }); +}); + +describe('using dynamic classes', () => { + describe('when matched class is empty', () => { + // The parser will identify a class named '' + const source = + '' + 'Red'; + + test('throws an exception', async () => { + await expect(compiler({ source })).rejects.toThrow( + 'Invalid class name in file src/App.svelte.\nThis usually happens when using dynamic classes with svelte-preprocess-cssmodules.' + ); + }); + }); + + describe('when matched class could be a valid class but does not match any style definition', () => { + // The parser will identify a class named 'color' + const source = + '' + + 'Red'; + + it('in strict mode, it throw an exception', async () => { + await expect(compiler({ source }, { strict: true })).rejects.toThrow( + 'Classname "color" was not found in declared src/App.svelte Red' + ); + }); + }); + + describe('when matched class is an invalid class', () => { + // The parser will identify a class named 'color-' + const source = + '' + + 'Red'; + + it('throws an exception when resulting class is invalid', async () => { + await expect(compiler({ source })).rejects.toThrow('Classname "color-" in file src/App.svelte is not valid'); + }); + }); +});