Skip to content

Commit

Permalink
Add toIncludeRepeated matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Nov 29, 2017
1 parent 17b064d commit f91bd4f
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,6 @@ test('passes when value includes substring', () => {

#### .toIncludeRepeated(substring, times)

_Note: Currently unimplemented_

Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times.

```js
Expand Down
4 changes: 3 additions & 1 deletion src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import toBeNil from './toBeNil';
import toSatisfyAll from './toSatisfyAll';
import toBeEmpty from './toBeEmpty';
import toBeSealed from './toBeSealed';
import toIncludeRepeated from './toIncludeRepeated';

export default [
toBeEven,
Expand Down Expand Up @@ -83,5 +84,6 @@ export default [
toSatisfyAll,
toBeEmpty,
toBeSealed,
toSatisfy
toSatisfy,
toIncludeRepeated
].reduce((acc, matcher) => ({ ...acc, ...matcher }), {});
37 changes: 37 additions & 0 deletions src/matchers/toIncludeRepeated/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toIncludeRepeated fails when given string includes given substring 1 time 1`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeRepeated(</><green>expected</><dim>)</>
Expected string to not include repeated 1 times:
<green>\\"ell\\"</>
Received:
<red>\\"hello world\\"</>"
`;
exports[`.not.toIncludeRepeated fails when given string includes given substring to the given occurrences 1`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeRepeated(</><green>expected</><dim>)</>
Expected string to not include repeated 3 times:
<green>\\"l\\"</>
Received:
<red>\\"hello world\\"</>"
`;
exports[`.toIncludeRepeated fails when given string does not have a given substring the correct number of times 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeRepeated(</><green>expected</><dim>)</>
Expected string to include repeated 2 times:
<green>\\"world\\"</>
Received:
<red>\\"hello world\\"</>"
`;
exports[`.toIncludeRepeated fails when given string does not include given substring 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeRepeated(</><green>expected</><dim>)</>
Expected string to include repeated 1 times:
<green>\\"bob\\"</>
Received:
<red>\\"hello world\\"</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toIncludeRepeated/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (actual, expected, occurrences) => () =>
matcherHint('.not.toIncludeRepeated') +
'\n\n' +
`Expected string to not include repeated ${occurrences} times:\n` +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const failMessage = (actual, expected, occurrences) => () =>
matcherHint('.toIncludeRepeated') +
'\n\n' +
`Expected string to include repeated ${occurrences} times:\n` +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

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

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

expect.extend(matcher);

const string = 'hello world';

describe('.toIncludeRepeated', () => {
test('passes when a string includes a given substring 1 time', () => {
expect(string).toIncludeRepeated('ell', 1);
});

test('passes when a string includes a given substring 3 times', () => {
expect(string).toIncludeRepeated('l', 3);
});

test('fails when given string does not include given substring', () => {
expect(() => expect(string).toIncludeRepeated('bob', 1)).toThrowErrorMatchingSnapshot();
});

test('fails when given string does not have a given substring the correct number of times', () => {
expect(() => expect(string).toIncludeRepeated('world', 2)).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toIncludeRepeated', () => {
test('fails when given string includes given substring 1 time', () => {
expect(() => expect(string).not.toIncludeRepeated('ell', 1)).toThrowErrorMatchingSnapshot();
});

test('fails when given string includes given substring to the given occurrences', () => {
expect(() => expect(string).not.toIncludeRepeated('l', 3)).toThrowErrorMatchingSnapshot();
});

test('passes when given string does not include given substring', () => {
expect(string).not.toIncludeRepeated('bob', 1);
});

test('passes when given string does not have a given substring the correct number of times', () => {
expect(string).not.toIncludeRepeated('world', 2);
});
});
2 changes: 2 additions & 0 deletions src/matchers/toIncludeRepeated/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (string, substring, occurrences) =>
(string.match(new RegExp(substring, 'g')) || []).length === occurrences;
25 changes: 25 additions & 0 deletions src/matchers/toIncludeRepeated/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import predicate from './predicate';

const string = 'hello world';
const multi = `
hello world
hello world
`;

describe('Predicate > .toIncludeRepeated', () => {
test('passes when string contains substring', () => {
expect(predicate(string, 'l', 3)).toBe(true);
});

test('passes when given a multiline string includes given substring hello 2 times', () => {
expect(predicate(multi, 'hello', 2)).toBe(true);
});

test('fails when string does not contain substring', () => {
expect(predicate(string, 'bob', 1)).toBe(false);
});

test('fails when string does not contain substring for the given occurrences', () => {
expect(predicate(string, 'l', 10)).toBe(false);
});
});

0 comments on commit f91bd4f

Please sign in to comment.