Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature toBeHexadecimal Matcher #152

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@
"contributions": [
"code"
]
},
{
"login": "mvpowers",
"name": "Mikey Powers",
"avatar_url": "https://avatars1.githubusercontent.com/u/13043635?v=4",
"profile": "https://github.com/mvpowers",
"contributions": [
"code",
"doc",
"test"
]
}
]
}
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [~~Promise~~](#promise)
- [String](#string)
- [.toBeString()](#tobestring)
- [.toBeHexadecimal(string)](#tobehexadecimal)
- [.toEqualCaseInsensitive(string)](#toequalcaseinsensitivestring)
- [.toStartWith(prefix)](#tostartwithprefix)
- [.toEndWith(suffix)](#toendwithsuffix)
Expand Down Expand Up @@ -770,6 +771,19 @@ test('passes when value is a string', () => {
});
```

#### .toBeHexadecimal(string)

Use `.toBeHexadecimal` when checking if a value is a valid HTML hexadecimal color.

```js
test('passes when value is a valid hexadecimal', () => {
expect('#abc123').toBeHexadecimal();
expect('#FFF').toBeHexadecimal();
expect('#000000').toBeHexadecimal();
expect('#123ffg').not.toBeHexadecimal();
});
```

#### .toEqualCaseInsensitive(string)

Use `.toEqualCaseInsensitive` when checking if a string is equal (===) to another ignoring the casing of both strings.
Expand Down Expand Up @@ -848,7 +862,7 @@ test('passes when value includes all substrings', () => {
| [<img src="https://avatars2.githubusercontent.com/u/33098064?v=4" width="100px;"/><br /><sub>mjmiles</sub>](https://github.com/mjmiles)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/13333582?v=4" width="100px;"/><br /><sub>Gary Meehan</sub>](https://github.com/garmeeh)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [<img src="https://avatars2.githubusercontent.com/u/3191489?v=4" width="100px;"/><br /><sub>Fredrik M盲kil盲</sub>](https://github.com/GitHug)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [<img src="https://avatars2.githubusercontent.com/u/9046616?v=4" width="100px;"/><br /><sub>Daniel Reinoso</sub>](http://kloc.io/)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/4359781?v=4" width="100px;"/><br /><sub>Chris Hut</sub>](https://github.com/tophernuts)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1513183?v=4" width="100px;"/><br /><sub>Kelvin Ly</sub>](https://github.com/cactorium)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [<img src="https://avatars0.githubusercontent.com/u/11182826?v=4" width="100px;"/><br /><sub>Francis Ngo</sub>](https://github.com/francisngo)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/10330923?v=4" width="100px;"/><br /><sub>Amish Shah</sub>](https://hydrabolt.me/)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [<img src="https://avatars3.githubusercontent.com/u/2045206?v=4" width="100px;"/><br /><sub>Dave Cooper</sub>](http://davecooper.org)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3630495?v=4" width="100px;"/><br /><sub>Swann Polydor</sub>](https://github.com/soueuls)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2027003?v=4" width="100px;"/><br /><sub>vikneshwar</sub>](https://github.com/vikneshwar)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1243921?v=4" width="100px;"/><br /><sub>Budi Irawan</sub>](http://budiirawan.com)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [<img src="https://avatars2.githubusercontent.com/u/980783?v=4" width="100px;"/><br /><sub>Tejas Bubane</sub>](http://foss-geek.blogspot.com/)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/13134653?v=4" width="100px;"/><br /><sub>Subinoy Ghosh</sub>](https://github.com/subinoy7)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/1404810?v=4" width="100px;"/><br /><sub>Simen Bekkhus</sub>](https://github.com/SimenB)<br />[馃摉](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/49038?v=4" width="100px;"/><br /><sub>Orta</sub>](http://orta.io)<br />[馃摉](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/17221813?v=4" width="100px;"/><br /><sub>Tom</sub>](https://jsdevtom.com)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [馃挕](#example-jsdevtom "Examples") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [<img src="https://avatars0.githubusercontent.com/u/15064535?v=4" width="100px;"/><br /><sub>Lucian Buzzo</sub>](https://github.com/LucianBuzzo)<br /> | [<img src="https://avatars3.githubusercontent.com/u/2997844?v=4" width="100px;"/><br /><sub>Thiago Delgado Pinto</sub>](https://github.com/thiagodp)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [馃挕](#example-thiagodp "Examples") [馃](#ideas-thiagodp "Ideas, Planning, & Feedback") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [<img src="https://avatars0.githubusercontent.com/u/3042904?v=4" width="100px;"/><br /><sub>Ragnar Laud</sub>](https://github.com/xprn)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3047126?v=4" width="100px;"/><br /><sub>Luiz Am茅rico</sub>](https://github.com/blikblum)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [馃](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/868844?v=4" width="100px;"/><br /><sub>Gilles De Mey</sub>](https://demey.io)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [<img src="https://avatars0.githubusercontent.com/u/50928?v=4" width="100px;"/><br /><sub>Deniz Dogan</sub>](https://github.com/denizdogan)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [馃](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/868844?v=4" width="100px;"/><br /><sub>Gilles De Mey</sub>](https://demey.io)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [<img src="https://avatars0.githubusercontent.com/u/50928?v=4" width="100px;"/><br /><sub>Deniz Dogan</sub>](https://github.com/denizdogan)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") | [<img src="https://avatars1.githubusercontent.com/u/13043635?v=4" width="100px;"/><br /><sub>Mikey Powers</sub>](https://github.com/mvpowers)<br />[馃捇](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Code") [馃摉](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Documentation") [鈿狅笍](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Tests") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

## LICENSE
Expand Down
22 changes: 22 additions & 0 deletions src/matchers/toBeHexadecimal/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeHexadecimal fails when given valid hexadecimal 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeHexadecimal(</><dim>)</>

Expected value to not be a hexadecimal, received:
<red>\\"#eeffee\\"</>"
`;

exports[`.toBeHexadecimal fails when given invalid hexadecimal 1`] = `
"<dim>expect(</><red>received</><dim>).toBeHexadecimal(</><dim>)</>

Expected value to be a hexadecimal, received:
<red>\\"#eeggee\\"</>"
`;

exports[`.toBeHexadecimal fails when given non-string 1`] = `
"<dim>expect(</><red>received</><dim>).toBeHexadecimal(</><dim>)</>

Expected value to be a hexadecimal, received:
<red>true</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeHexadecimal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeHexadecimal', 'received', '') +
'\n\n' +
'Expected value to not be a hexadecimal, received:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeHexadecimal', 'received', '') +
'\n\n' +
'Expected value to be a hexadecimal, received:\n' +
` ${printReceived(received)}`;

export default {
toBeHexadecimal: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
}
};
31 changes: 31 additions & 0 deletions src/matchers/toBeHexadecimal/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import matcher from './';

expect.extend(matcher);

describe('.toBeHexadecimal', () => {
test('passes when given valid 6 digit hexadecimal', () => {
expect('#ECECEC').toBeHexadecimal();
});

test('passes when given valid 3 digit hexadecimal', () => {
expect('#000').toBeHexadecimal();
});

test('fails when given invalid hexadecimal', () => {
expect(() => expect('#eeggee').toBeHexadecimal()).toThrowErrorMatchingSnapshot();
});

test('fails when given non-string', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth having a test for a string that isn't hexadecimal as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This boundary is already covered in the predicate test.

expect(() => expect(true).toBeHexadecimal()).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeHexadecimal', () => {
test('passes when given positive number', () => {
expect(1).not.toBeHexadecimal();
});

test('fails when given valid hexadecimal', () => {
expect(() => expect('#eeffee').not.toBeHexadecimal()).toThrowErrorMatchingSnapshot();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeHexadecimal/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default expected => {
const re1 = RegExp(/^#\b[a-f0-9]{6}\b/gi);
const re2 = RegExp(/^#\b[a-f0-9]{3}\b/gi);
return re1.test(expected) || re2.test(expected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth naming these a bit more explicitly, e.g.
shortRegex
longRedex
I only ask because i read the regex 3 times before i spotted the 6 and 3 馃寽

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This was lazy on my part.

};
30 changes: 30 additions & 0 deletions src/matchers/toBeHexadecimal/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import predicate from './predicate';

describe('toBeHexadecimal Predicate', () => {
describe('returns true', () => {
test('When valid hex is passed', () => {
expect(predicate('#123abc')).toBe(true);
expect(predicate('#ABC123')).toBe(true);
expect(predicate('#FFF')).toBe(true);
expect(predicate('#ecECec')).toBe(true);
expect(predicate('#00f')).toBe(true);
});
});

describe('return false', () => {
test('When incomplete hex is passed', () => {
expect(predicate('#ecec')).toBe(false);
expect(predicate('#00')).toBe(false);
});

test('When octothorp is not passed', () => {
expect(predicate('000')).toBe(false);
expect(predicate('ececec')).toBe(false);
});

test('When invalid character is passed', () => {
expect(predicate('#123ffg')).toBe(false);
expect(predicate('#GGG')).toBe(false);
});
});
});
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ declare namespace jest {
*/
toBeFunction(): R;

/**
* Use `.toBeHexadecimal` when checking if a value is a valid HTML hex color.
*
* @param {String} string
*/
toBeHexadecimal(string: string): R;

/**
* Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before another `Mock`.
*
Expand Down