const {RuleTester} = require('eslint') const avoidUnwrappedText = require('../avoid-unwrapped-text') const ruleTester = new RuleTester({ parser: require.resolve('@typescript-eslint/parser'), parserOptions: { ecmaFeatures: { jsx: true, }, ecmaVersion: 6, sourceType: 'module', }, }) describe('avoid-unwrapped-text', () => { const tests = { valid: [ { code: ` foo `, }, { code: ` foo `, }, { code: ` <> foo `, }, { code: ` {foo && foo} `, }, { code: ` {foo ? foo : bar} `, }, { code: ` foo `, }, { code: ` {foo && foo} `, }, { code: ` {foo ? foo : bar} `, }, { code: ` foo `, }, { code: ` foo `, }, { code: ` {bar} `, }, { code: ` {bar} `, }, { code: ` foo {bar} `, }, { code: ` foo `, }, { code: ` {bar} `, }, { code: ` foo {bar} `, }, { code: ` foo `, }, { code: ` foo }> `, }, { code: ` foo }> `, }, { code: ` foo : bar }> `, }, { code: ` foo }> `, }, { code: ` foo }> `, }, { code: ` foo }> `, }, { code: ` foo }> `, }, { code: ` foo : bar }> `, }, { code: ` function Stuff() { return foo } `, }, { code: ` function Stuff({ foo }) { return {foo} } `, }, { code: ` function MyText() { return foo } `, }, { code: ` function MyText({ foo }) { if (foo) { return foo } return foo } `, }, { code: ` {'foo'} `, }, { code: ` {foo + 'foo'} `, }, { code: ` {'foo'} `, }, { code: ` {foo['bar'] && } `, }, { code: ` {(foo === 'bar') && } `, }, { code: ` {(foo !== 'bar') && } `, }, { code: ` {\`foo\`} `, }, { code: ` {\`foo\`} `, }, { code: ` {_(msg\`foo\`)} `, }, { code: ` {_(msg\`foo\`)} `, }, { code: ` `, }, { code: ` stuff('foo')}> `, }, { code: ` {renderItem('foo')} `, }, { code: ` {foo === 'foo' && } `, }, { code: ` {foo['foo'] && } `, }, { code: ` {check('foo') && } `, }, { code: ` {foo.bar && } `, }, { code: ` {renderItem('foo')} `, }, { code: ` {null} `, }, { code: ` {null} `, }, ], invalid: [ { code: ` `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` <> foo `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` {foo && foo} `, errors: 1, }, { code: ` {foo ? foo : bar} `, errors: 2, }, { code: ` foo `, errors: 1, }, { code: ` foo {bar} `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` foo }> `, errors: 1, }, { code: ` foo }> `, errors: 1, }, { code: ` foo : bar }> `, errors: 2, }, { code: ` foo }> `, errors: 1, }, { code: ` function MyText() { return } `, errors: 1, }, { code: ` function MyText({ foo }) { return {foo} } `, errors: 1, }, { code: ` function MyText({ foo }) { if (foo) { return {foo} } return foo } `, errors: 1, }, { code: ` {'foo'} `, errors: 1, }, { code: ` {foo && 'foo'} `, errors: 1, }, { code: ` {'foo'} `, errors: 1, }, { code: ` {foo && {'foo'}} `, errors: 1, }, { code: ` {10} `, errors: 1, }, { code: ` {10} `, errors: 1, }, { code: ` {foo + 10} `, errors: 1, }, { code: ` {\`foo\`} `, errors: 1, }, { code: ` {\`foo\`} `, errors: 1, }, { code: ` {foo + \`foo\`} `, errors: 1, }, { code: ` {_(msg\`foo\`)} `, errors: 1, }, { code: ` {foo + _(msg\`foo\`)} `, errors: 1, }, { code: ` {_(msg\`foo\`)} `, errors: 1, }, { code: ` {foo + _(msg\`foo\`)} `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` foo `, errors: 1, }, { code: ` {foo} `, errors: 1, }, { code: ` {'foo'} `, errors: 1, }, { code: ` foo }> `, errors: 1, }, ], } // For easier local testing if (!process.env.CI) { let only = [] let skipped = [] ;[...tests.valid, ...tests.invalid].forEach(t => { if (t.skip) { delete t.skip skipped.push(t) } if (t.only) { delete t.only only.push(t) } }) const predicate = t => { if (only.length > 0) { return only.indexOf(t) !== -1 } if (skipped.length > 0) { return skipped.indexOf(t) === -1 } return true } tests.valid = tests.valid.filter(predicate) tests.invalid = tests.invalid.filter(predicate) } ruleTester.run('avoid-unwrapped-text', avoidUnwrappedText, tests) })