|
| 1 | +import test, { TestContext } from 'ava'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +const path = require('path'); |
| 4 | +import * as postcss from 'postcss'; |
| 5 | + |
| 6 | +import { PostcssTypescriptCss } from '../namespace/PostcssTypescriptCss'; |
| 7 | +import * as plugin from '../postcss-typescript-css'; |
| 8 | + |
| 9 | +function run(t: TestContext, input: {css: string, from: string}, opts?: PostcssTypescriptCss.Options) { |
| 10 | + return postcss([ plugin(opts) ]).process(input.css, { from: input.from }) |
| 11 | + .then((result) => { |
| 12 | + let fakeComponentTS; |
| 13 | + if (opts) { |
| 14 | + fakeComponentTS = readFileSync(path.join(__dirname, 'postcss/fakeComponentModules.ts'), 'utf8'); |
| 15 | + t.true(fakeComponentTS.includes('fakeComponentModulesStyle')); |
| 16 | + t.true(fakeComponentTS.includes('fakeComponentModules:')); |
| 17 | + t.true(fakeComponentTS.includes('fakeComponentModulesDescendentName:')); |
| 18 | + t.true(fakeComponentTS.includes('fakeComponentModulesModifierName:')); |
| 19 | + } else { |
| 20 | + fakeComponentTS = readFileSync(path.join(__dirname, 'postcss/fakeComponent.ts'), 'utf8'); |
| 21 | + t.true(fakeComponentTS.includes('fakeComponentStyle')); |
| 22 | + t.true(fakeComponentTS.includes('fakeComponent:')); |
| 23 | + t.true(fakeComponentTS.includes('fakeComponentDescendentName:')); |
| 24 | + t.true(fakeComponentTS.includes('fakeComponentModifierName:')); |
| 25 | + } |
| 26 | + }) |
| 27 | + .catch((err: string) => { |
| 28 | + if (opts) { |
| 29 | + if (!opts.cssFileName) { |
| 30 | + t.is(err, 'The property cssFileName can not be null'); |
| 31 | + } |
| 32 | + if (!opts.content) { |
| 33 | + t.is(err, 'The property content can not be null'); |
| 34 | + } |
| 35 | + } else { |
| 36 | + t.true(err.includes('TypeError: Path must be a string')); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +test('should create a ts file', t => { |
| 42 | + const cssFile = path.join(__dirname, 'postcss/fakeComponent.postcss'); |
| 43 | + const cssContent = readFileSync(cssFile, 'utf8'); |
| 44 | + return run(t, { css: cssContent, from: cssFile }); |
| 45 | +}); |
| 46 | + |
| 47 | +test('should create a ts file with postcss-modules configuration', t => { |
| 48 | + const cssFileName = path.join(__dirname, 'postcss/fakeComponentModules.postcss'); |
| 49 | + const content = JSON.parse(readFileSync(path.join(__dirname, 'postcss/fakeComponentModules.json'), 'utf8')); |
| 50 | + return run(t, { css: '', from: '' }, { cssFileName, content }); |
| 51 | +}); |
| 52 | + |
| 53 | +test('throws if cssFileName is null', t => { |
| 54 | + return run(t, { css: '', from: '' }, { cssFileName: null, content: ['.class'] }); |
| 55 | +}); |
| 56 | + |
| 57 | +test('throws if content is null', t => { |
| 58 | + const cssFileName = path.join(__dirname, 'postcss/fakeComponentModules.postcss'); |
| 59 | + return run(t, { css: '', from: '' }, { cssFileName, content: null }); |
| 60 | +}); |
0 commit comments