Skip to content

Commit

Permalink
add data-testid' as an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
smclmnt committed Jul 15, 2020
1 parent 387560d commit d7355cf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# svg-jest


This is a small library which transforms .SVG files for jest. It produces
and SVG in the stream which has two properties 'data-jest-file-name' which
includes the path and 'data-jest-name' which just the name portion.
and SVG in the stream.

The transformed item will have the following properties on it.
* data-jest-file-name: The name of the file (e.g. 'some-image.svg')
* data-jest-svg-name: Only the name portion of the file (e.g. 'some-image')
* data-testid: Same as data-jest-svg-name, but works with @testing-library/react getByTestId()

Works with both of these formats:

```js
import MySvg from '../images/an-image.svg';
```
```js

import { ReactComponent as MySvg} from '../images/an-image.svg';
```

Expand All @@ -35,12 +39,12 @@ The resulting HTML:

```html
<div>
<svg data-jest-file-name='an-image.svg' data-jest-name='an-image'/>
<svg data-jest-file-name='an-image.svg' data-jest-svg-name='an-image' data-testid='an-image'/>
</div>
```

Any properties passed to '<MySvg>' are passed along into both the snapshot
and the resulting HTML.
In additoin, any properties passed to '<MySvg>' are passed along into both the snapshot
and the resulting trees.

# usage
Configure jest:
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const ${functionName} = (props) =>
return React.createElement('svg', {
...props,
'data-jest-file-name': '${pathname}',
'data-jest-svg-name': '${filename}'
'data-jest-svg-name': '${filename}',
'data-testid': '${filename}'
});
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "A simple transformer for .SVG for Jest",
"main": "index.js",
"repository": "https://github.com/half-halt/svg-jest.git",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

0 comments on commit d7355cf

Please sign in to comment.