|
| 1 | +import path from 'path'; |
| 2 | +import expect from 'expect'; |
| 3 | +import posthtml from 'posthtml'; |
| 4 | +import cssModules from '..'; |
| 5 | + |
| 6 | +const classesPath = path.join(__dirname, 'classes.json'); |
| 7 | +const classesDir = path.dirname(classesPath); |
| 8 | + |
| 9 | + |
| 10 | +describe('posthtml-css-modules', () => { |
| 11 | + it('should inline CSS module from the file (flat config)', () => { |
| 12 | + return init( |
| 13 | + '<div class="foob" css-module="title"></div>', |
| 14 | + '<div class="foob __title __heading"></div>', |
| 15 | + classesPath |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + |
| 20 | + it('should inline CSS module from the file (deep config)', () => { |
| 21 | + return init( |
| 22 | + '<div css-module="user.profile.photo"></div>', |
| 23 | + '<div class="__user__profile__photo"></div>', |
| 24 | + classesPath |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + |
| 29 | + it('should inline CSS module from the directory', () => { |
| 30 | + return init( |
| 31 | + '<div css-module="classes.title"></div>', |
| 32 | + '<div class="__title __heading"></div>', |
| 33 | + classesDir |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + |
| 38 | + it('should throw an error if the file with the CSS modules is not found', () => { |
| 39 | + return init( |
| 40 | + '<div></div>', |
| 41 | + '<div></div>', |
| 42 | + classesPath |
| 43 | + ).catch(error => { |
| 44 | + expect(error.message) |
| 45 | + .toInclude('Cannot find module') |
| 46 | + .toInclude('config/notExists.json'); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + |
| 51 | + it('should throw an error if the CSS module is not found in the file', () => { |
| 52 | + return init( |
| 53 | + '<div css-module="notExists"></div>', |
| 54 | + '<div css-module="notExists"></div>', |
| 55 | + classesPath |
| 56 | + ).catch(error => { |
| 57 | + expect(error.message) |
| 58 | + .toInclude('[posthtml-css-modules] CSS module "notExists" is not found'); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + it('should throw an error if the file with the CSS module is not found in the directory', () => { |
| 64 | + return init( |
| 65 | + '<div css-module="notExists"></div>', |
| 66 | + '<div css-module="notExists"></div>', |
| 67 | + classesDir |
| 68 | + ).catch(error => { |
| 69 | + expect(error.message) |
| 70 | + .toInclude('Cannot find module') |
| 71 | + .toInclude('test/notExists'); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +function init(html, expectedHtml, options) { |
| 78 | + return posthtml([cssModules(options)]).process(html).then(result => { |
| 79 | + expect(result.html).toBe(expectedHtml); |
| 80 | + }); |
| 81 | +} |
0 commit comments