Skip to content

Commit 784bde8

Browse files
committed
fix(expect-puppeteer): use the same behaviour on toMatch and toMatchElement
1 parent 070dde9 commit 784bde8

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

packages/expect-puppeteer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ await expect(page).toFillForm('form[name="myForm"]', {
147147
Expect a text or a string RegExp to be present in the page or element.
148148

149149
* `instance` <[Page]|[ElementHandle]> Context
150-
* `matcher` <[string]> A text or a RegExp to match in page
150+
* `matcher` <[string]|[RegExp]> A text or a RegExp to match in page
151151
* `options` <[Object]> Optional parameters
152152
* `polling` <[string]|[number]> An interval at which the `pageFunction` is executed, defaults to `raf`. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. If `polling` is a string, then it can be one of the following values:
153153
* `raf` - to constantly execute `pageFunction` in `requestAnimationFrame` callback. This is the tightest polling mode which is suitable to observe styling changes.

packages/expect-puppeteer/src/matchers/toMatch.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1-
import { getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError, expandSearchExpr } from '../utils'
22
import { defaultOptions } from '../options'
33

44
async function toMatch(instance, matcher, options) {
55
options = defaultOptions(options)
66

77
const { page, handle } = await getContext(instance, () => document.body)
88

9+
const { text, regexp } = expandSearchExpr(matcher)
10+
911
try {
1012
await page.waitForFunction(
11-
(handle, matcher) => {
13+
(handle, text, regexp) => {
1214
if (!handle) return false
13-
return handle.textContent.match(new RegExp(matcher)) !== null
15+
if (regexp !== null) {
16+
const [, pattern, flags] = regexp.match(/\/(.*)\/(.*)?/)
17+
return (
18+
handle.textContent
19+
.replace(/\s+/g, ' ')
20+
.trim()
21+
.match(new RegExp(pattern, flags)) !== null
22+
)
23+
}
24+
if (text !== null) {
25+
return handle.textContent
26+
.replace(/\s+/g, ' ')
27+
.trim()
28+
.includes(text)
29+
}
30+
return false
1431
},
1532
options,
1633
handle,
17-
matcher,
34+
text,
35+
regexp,
1836
)
1937
} catch (error) {
2038
throw enhanceError(error, `Text not found "${matcher}"`)

packages/expect-puppeteer/src/matchers/toMatch.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ describe('toMatch', () => {
88
await expect(page).toMatch('This is home!')
99
})
1010

11+
it('should support RegExp', async () => {
12+
await expect(page).toMatch(/THIS.is.home/i)
13+
})
14+
1115
it('should return an error if text is not in the page', async () => {
1216
expect.assertions(3)
1317

@@ -26,6 +30,11 @@ describe('toMatch', () => {
2630
await expect(dialogBtn).toMatch('Open dialog')
2731
})
2832

33+
it('should support RegExp', async () => {
34+
const dialogBtn = await page.$('#dialog-btn')
35+
await expect(dialogBtn).toMatch(/OPEN/i)
36+
})
37+
2938
it('should return an error if text is not in the page', async () => {
3039
expect.assertions(3)
3140
const dialogBtn = await page.$('#dialog-btn')

packages/expect-puppeteer/src/matchers/toMatchElement.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { getContext, enhanceError } from '../utils'
1+
import { getContext, enhanceError, expandSearchExpr } from '../utils'
22
import { defaultOptions } from '../options'
33

4-
const isRegExp = input =>
5-
Object.prototype.toString.call(input) === '[object RegExp]'
6-
7-
const expandSearchExpr = expr => {
8-
if (isRegExp(expr)) return { text: null, regexp: expr.toString() }
9-
if (typeof expr === 'string') return { text: expr, regexp: null }
10-
return { text: null, regexp: null }
11-
}
12-
134
async function toMatchElement(
145
instance,
156
selector,

packages/expect-puppeteer/src/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ export const enhanceError = (error, message) => {
3636
error.message = `${message}\n${error.message}`
3737
return error
3838
}
39+
40+
const isRegExp = input =>
41+
Object.prototype.toString.call(input) === '[object RegExp]'
42+
43+
export const expandSearchExpr = expr => {
44+
if (isRegExp(expr)) return { text: null, regexp: expr.toString() }
45+
if (typeof expr === 'string') return { text: expr, regexp: null }
46+
return { text: null, regexp: null }
47+
}

0 commit comments

Comments
 (0)