This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
forked from jest-community/jest-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17b064d
commit f91bd4f
Showing
7 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/matchers/toIncludeRepeated/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\"</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |