Skip to content

Commit

Permalink
feat(matchers): support asymmetric matchers (#37)
Browse files Browse the repository at this point in the history
* chore(contributor): add Ailrun as a contributor

* feat(matchers): support asymmetric matchers

* Update .all-contributorsrc
  • Loading branch information
Ailrun authored and Kent C. Dodds committed Apr 23, 2018
1 parent 21fdeff commit a2bec88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"projectName": "jest-glamor-react",
"projectOwner": "kentcdodds",
"repoType": "github",
"files": [
"README.md"
],
Expand Down Expand Up @@ -98,6 +99,16 @@
"contributions": [
"doc"
]
},
{
"login": "Ailrun",
"name": "Junyoung Clare Jang",
"avatar_url": "https://avatars3.githubusercontent.com/u/12473268?v=4",
"profile": "http://ailrun.github.io/",
"contributions": [
"code",
"test"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Jest utilities for Glamor and React
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -380,7 +380,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- prettier-ignore -->
| [<img src="https://avatars1.githubusercontent.com/u/1308971?v=3" width="100px;"/><br /><sub><b>Michele Bertoli</b></sub>](http://michele.berto.li)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Documentation") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=MicheleBertoli "Tests") | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=kentcdodds "Tests") | [<img src="https://avatars2.githubusercontent.com/u/11481355?v=3" width="100px;"/><br /><sub><b>Mitchell Hamilton</b></sub>](https://hamil.town)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Code") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Documentation") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=mitchellhamilton "Tests") | [<img src="https://avatars2.githubusercontent.com/u/11878516?v=3" width="100px;"/><br /><sub><b>jhurley23</b></sub>](https://github.com/jhurley23)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=jhurley23 "Code") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=jhurley23 "Tests") [📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=jhurley23 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/27758243?v=4" width="100px;"/><br /><sub><b>Gaurav Talwar</b></sub>](https://github.com/megaurav2002)<br /> | [<img src="https://avatars3.githubusercontent.com/u/6494737?v=4" width="100px;"/><br /><sub><b>Henry Lewis</b></sub>](http://hjylewis.com/)<br />[🐛](https://github.com/kentcdodds/jest-glamor-react/issues?q=author%3Ahjylewis "Bug reports") [💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=hjylewis "Code") | [<img src="https://avatars2.githubusercontent.com/u/8881674?v=4" width="100px;"/><br /><sub><b>Alexey Svetliakov</b></sub>](https://github.com/asvetliakov)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=asvetliakov "Code") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=asvetliakov "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars2.githubusercontent.com/u/794161?v=4" width="100px;"/><br /><sub><b>James W Lane</b></sub>](http://jameswlane.com)<br />[🐛](https://github.com/kentcdodds/jest-glamor-react/issues?q=author%3Ajameswlane "Bug reports") [💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=jameswlane "Code") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=jameswlane "Tests") | [<img src="https://avatars1.githubusercontent.com/u/202773?v=4" width="100px;"/><br /><sub><b>Brent Ertz</b></sub>](https://github.com/brentertz)<br />[📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=brentertz "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/794161?v=4" width="100px;"/><br /><sub><b>James W Lane</b></sub>](http://jameswlane.com)<br />[🐛](https://github.com/kentcdodds/jest-glamor-react/issues?q=author%3Ajameswlane "Bug reports") [💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=jameswlane "Code") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=jameswlane "Tests") | [<img src="https://avatars1.githubusercontent.com/u/202773?v=4" width="100px;"/><br /><sub><b>Brent Ertz</b></sub>](https://github.com/brentertz)<br />[📖](https://github.com/kentcdodds/jest-glamor-react/commits?author=brentertz "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/12473268?v=4" width="100px;"/><br /><sub><b>Junyoung Clare Jang</b></sub>](http://ailrun.github.io/)<br />[💻](https://github.com/kentcdodds/jest-glamor-react/commits?author=Ailrun "Code") [⚠️](https://github.com/kentcdodds/jest-glamor-react/commits?author=Ailrun "Tests") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
13 changes: 12 additions & 1 deletion src/__tests__/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ test('react-test-renderer', () => {

expect(wrapper).toHaveStyleRule('background', 'papayawhip')
expect(wrapper).not.toHaveStyleRule('color', 'palevioletred')
expect(wrapper).toHaveStyleRule('padding', expect.anything())

const title = wrapper.children[0]
expect(title).not.toHaveStyleRule('background', 'papayawhip')
expect(title).toHaveStyleRule('color', 'palevioletred')
expect(title).toHaveStyleRule('text-align', expect.stringMatching(/cen.*$/))
})

test('enzyme', () => {
Expand All @@ -55,10 +57,12 @@ test('enzyme', () => {
const wrapper = enzyme[method](ui)
expect(wrapper).toHaveStyleRule('background', 'papayawhip')
expect(wrapper).not.toHaveStyleRule('color', 'palevioletred')
expect(wrapper).toHaveStyleRule('padding', expect.anything())

const title = method === 'render' ? wrapper.find('h1') : wrapper.find(Title)
expect(title).not.toHaveStyleRule('background', 'papayawhip')
expect(title).toHaveStyleRule('color', 'palevioletred')
expect(title).toHaveStyleRule('text-align', expect.stringMatching(/cen.*$/))
})
})

Expand Down Expand Up @@ -97,10 +101,17 @@ describe('toHaveStyleRule', () => {
).toContain('Expected background not to match:')
})

it('can recieve string or regex values', () => {
it('can recieve string, regex values, or asymmetricMatchers (like expect.anything())', () => {
const wrapper = renderer.create(<Wrapper />).toJSON()

expect(toHaveStyleRule(wrapper, 'background', /papayawhip/).pass).toBe(true)
expect(toHaveStyleRule(wrapper, 'background', 'papayawhip').pass).toBe(true)
expect(toHaveStyleRule(wrapper, 'background', expect.anything()).pass).toBe(
true,
)
expect(
toHaveStyleRule(wrapper, 'background', expect.stringContaining('paya'))
.pass,
).toBe(true)
})
})
28 changes: 25 additions & 3 deletions src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@ const {getAST, getClassNames} = require('./utils')

const getGlamorStyleSheet = () => require('glamor').styleSheet

/*
* Taken from
* https://github.com/facebook/jest/blob/be4bec387d90ac8d6a7596be88bf8e4994bc3ed9/packages/expect/src/jasmine_utils.js#L234
*/
function isA(typeName, value) {
return Object.prototype.toString.apply(value) === `[object ${typeName}]`
}

/*
* Taken from
* https://github.com/facebook/jest/blob/be4bec387d90ac8d6a7596be88bf8e4994bc3ed9/packages/expect/src/jasmine_utils.js#L36
*/
function isAsymmetric(obj) {
return obj && isA('Function', obj.asymmetricMatch)
}

function valueMatches(declaration, value) {
return value instanceof RegExp
? value.test(declaration.value)
: value === declaration.value
if (value instanceof RegExp) {
return value.test(declaration.value)
}

if (isAsymmetric(value)) {
return value.asymmetricMatch(declaration.value)
}

return value === declaration.value
}

function toHaveStyleRule(received, property, value) {
Expand Down

0 comments on commit a2bec88

Please sign in to comment.