|
1 | 1 | const test = require('ava') |
2 | | -const path = require('path') |
3 | | -const { writeFileSync, readFileSync } = require('fs') |
4 | | -const { spawnSync } = require('child_process') |
5 | | - |
6 | | -const inputFile = path.join(__dirname, 'fixtures', 'default', 'input.html') |
7 | | -const outputFile = path.join(__dirname, 'fixtures', 'default', 'output.html') |
8 | | -const inputData = readFileSync(inputFile) |
9 | | -const cliPath = path.join(__dirname, '..', 'cli', 'index.js') |
10 | | - |
11 | | -test('Should format with default settings', t => { |
12 | | - writeFileSync( |
13 | | - inputFile, |
14 | | - '<form #heroForm (ngSubmit)="onSubmit(heroForm)"><input type="text" [(onChange)]="dede" name="test"><button [style.color]="isSpecial ? \'red\' : \'green\'"></button></form>' |
| 2 | +const { Readable } = require('stream') |
| 3 | +const { configTransform } = require('../cli/processor') |
| 4 | +const { getDefaultSettings } = require('../cli') |
| 5 | +const spy = require('../test-helpers/util') |
| 6 | +const args = require('../cli/args') |
| 7 | +const engine = require('unified-engine') |
| 8 | +const unified = require('unified') |
| 9 | + |
| 10 | +test.cb('Should format with default settings', t => { |
| 11 | + const stream = new Readable() |
| 12 | + stream._read = () => {} |
| 13 | + stream.push( |
| 14 | + `<form #heroForm (ngSubmit)="onSubmit(heroForm)"><input type="text" [(onChange)]="dede" name="test" /><button [style.color]="isSpecial ? 'red' : 'green'"></button></form>` |
| 15 | + ) |
| 16 | + stream.push(null) |
| 17 | + |
| 18 | + const stderr = spy() |
| 19 | + const stdout = spy() |
| 20 | + |
| 21 | + const prettierConfig = {} |
| 22 | + const cli = args(prettierConfig) |
| 23 | + const settings = getDefaultSettings({ prettierConfig, cli }) |
| 24 | + settings.configTransform = configTransform |
| 25 | + settings.defaultConfig = configTransform({ prettierConfig, cli }) |
| 26 | + settings.processor = unified() |
| 27 | + settings.streamIn = stream |
| 28 | + settings.streamError = stderr.stream |
| 29 | + settings.streamOut = stdout.stream |
| 30 | + |
| 31 | + engine( |
| 32 | + { |
| 33 | + ...settings, |
| 34 | + streamIn: stream, |
| 35 | + streamError: stderr.stream |
| 36 | + }, |
| 37 | + (err, code, result) => { |
| 38 | + t.falsy(err) |
| 39 | + t.deepEqual([stderr(), code], ['<stdin>: no issues found\n', 0]) |
| 40 | + t.snapshot(result.files[0]) |
| 41 | + t.end() |
| 42 | + } |
15 | 43 | ) |
16 | | - const argv = [cliPath, inputFile] |
17 | | - const result = spawnSync('node', argv) |
| 44 | +}) |
| 45 | + |
| 46 | +test('Should use correct default settings when no prettier settings was provided', t => { |
| 47 | + const prettierConfig = {} |
| 48 | + const cli = args(prettierConfig) |
| 49 | + t.deepEqual(cli.flags, { |
| 50 | + printWidth: 80, |
| 51 | + quiet: false, |
| 52 | + silent: false, |
| 53 | + singleQuote: false, |
| 54 | + sortAttributes: false, |
| 55 | + stdin: false, |
| 56 | + tabWidth: 2, |
| 57 | + usePrettier: true, |
| 58 | + useTabs: false, |
| 59 | + wrapAttributes: false |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +test('Should use correct settings when prettier settings was provided', t => { |
| 64 | + const prettierConfig = { |
| 65 | + printWidth: 120, |
| 66 | + tabWidth: 4, |
| 67 | + singleQuote: true, |
| 68 | + useTabs: true |
| 69 | + } |
| 70 | + const cli = args(prettierConfig) |
| 71 | + t.deepEqual(cli.flags, { |
| 72 | + printWidth: 120, |
| 73 | + quiet: false, |
| 74 | + silent: false, |
| 75 | + // Dont let it override by prettier settings because `"` is best practice in HTML |
| 76 | + singleQuote: false, |
| 77 | + sortAttributes: false, |
| 78 | + stdin: false, |
| 79 | + tabWidth: 4, |
| 80 | + usePrettier: true, |
| 81 | + useTabs: true, |
| 82 | + wrapAttributes: false |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +test('Should transform config in unified pipes', t => { |
| 87 | + const prettierConfig = {} |
| 88 | + const cli = args(prettierConfig) |
| 89 | + const config = configTransform({ prettierConfig, cli }) |
| 90 | + t.is(config.plugins.length, 3) |
| 91 | +}) |
18 | 92 |
|
19 | | - t.is(result.status, 0) |
20 | | - t.is(readFileSync(inputFile, 'utf8'), readFileSync(outputFile, 'utf8')) |
| 93 | +test('Should add sortAttributes plugin to the config', t => { |
| 94 | + const prettierConfig = { sortAttributes: true } |
| 95 | + const cli = args(prettierConfig) |
| 96 | + const config = configTransform({ prettierConfig, cli }) |
| 97 | + t.is(config.plugins.length, 4) |
21 | 98 | }) |
22 | 99 |
|
23 | | -test.after.always('restore file', t => { |
24 | | - writeFileSync(inputFile, inputData) |
| 100 | +test('Should add sortAttributes plugin to the config when it was passed as a flag', t => { |
| 101 | + const prettierConfig = {} |
| 102 | + const cli = args(prettierConfig) |
| 103 | + cli.flags.sortAttributes = true |
| 104 | + const config = configTransform({ prettierConfig, cli }) |
| 105 | + t.is(config.plugins.length, 4) |
25 | 106 | }) |
0 commit comments