Skip to content

Commit

Permalink
Merge pull request #5 from greggb/custom-attributes
Browse files Browse the repository at this point in the history
Allow custom attributes 🚀
  • Loading branch information
kutyel committed Oct 5, 2017
2 parents 3ca3730 + 039b67e commit 015da34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ $ npm install babel-plugin-remove-test-ids --save
]
}
```
... you can also configure it with your attributes!
```json
{
"plugins": [
[
"remove-test-ids",
{
"attributes": ["data-test", "data-custom-test-attr"]
}
]
]
}
```

## Motivation

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = () => ({
visitor: {
JSXAttribute: path =>
path.node.name.name === 'data-test-id' && path.remove()
JSXAttribute: (path, { opts: { attributes = ['data-test-id'] }}) =>
attributes.includes(path.node.name.name) && path.remove()
}
})
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const { transform } = require('babel-core')

const conf = { plugins: ['syntax-jsx', './index.js'] }
const confWithOptions = {
plugins: [
'syntax-jsx',
['./index.js', { attributes: ['data-custom-test-attr'] }]
]
}

describe('babel-plugin-remove-test-ids', () => {
it('should remove the [data-test-id] attribute if present', () => {
Expand All @@ -10,6 +16,13 @@ describe('babel-plugin-remove-test-ids', () => {
expect(code).toEqual(expected)
})

it('should remove a custom test attribute', () => {
const jsx = '<Component data-custom-test-attr="test">Hello!</Component>;'
const expected = '<Component>Hello!</Component>;'
const { code } = transform(jsx, confWithOptions)
expect(code).toEqual(expected)
})

it('should do nothing if [data-test-id] attribute is not present', () => {
const jsx = '<Component content="something">Hello again!</Component>;'
const { code } = transform(jsx, conf)
Expand Down

0 comments on commit 015da34

Please sign in to comment.