Skip to content

Commit

Permalink
support asymeyric matcher for array (#1624)
Browse files Browse the repository at this point in the history
* support asymeyric matcher for array

* Update src/utils.ts

---------

Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
lacell75 and christian-bromann authored Aug 6, 2024
1 parent 663dfe9 commit add9838
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,16 @@ export const compareTextWithArray = (
}
if (ignoreCase) {
actual = actual.toLowerCase()
expectedArray = expectedArray.map((item) => (item instanceof RegExp ? item : item.toLowerCase()))
expectedArray = expectedArray.map((item) => (typeof item !== 'string' ? item : item.toLowerCase()))
}

const textInArray = expectedArray.some((expected) => {
if (expected instanceof RegExp) {
return !!actual.match(expected)
}
if (isAsymmeyricMatcher(expected)) {
return expected.asymmetricMatch(actual)
}
if (containing) {
return actual.includes(expected)
}
Expand Down
40 changes: 40 additions & 0 deletions test/matchers/element/toHaveText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,46 @@ describe('toHaveText', () => {
expect(el._attempts).toBe(1)
})

test('should return true if actual text contains the expected text', async () => {
const el: any = await $('sel')
el._text = function (): string {
return 'WebdriverIO'
}

const result = await toHaveText.bind({})(el, expect.stringContaining('iverIO'), {})
expect(result.pass).toBe(true)
})

test('should return false if actual text does not contain the expected text', async () => {
const el: any = await $('sel')
el._text = function (): string {
return 'WebdriverIO'
}

const result = await toHaveText.bind({})(el, expect.stringContaining('WDIO'), {})
expect(result.pass).toBe(false)
})

test('should return true if actual text contains one of the expected texts', async () => {
const el: any = await $('sel')
el._text = function (): string {
return 'WebdriverIO'
}

const result = await toHaveText.bind({})(el, [expect.stringContaining('iverIO'), expect.stringContaining('WDIO')], {})
expect(result.pass).toBe(true)
})

test('should return false if actual text does not contain the expected texts', async () => {
const el: any = await $('sel')
el._text = function (): string {
return 'WebdriverIO'
}

const result = await toHaveText.bind({})(el, [expect.stringContaining('EXAMPLE'), expect.stringContaining('WDIO')], {})
expect(result.pass).toBe(false)
})

describe('with RegExp', () => {
let el: any

Expand Down
10 changes: 10 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('utils', () => {
test('should pass if string contains and using containing', () => {
expect(compareTextWithArray('qwe_AsD_zxc', ['foo', 'zxc'], { ignoreCase: true, containing: true }).result).toBe(true)
})

test('should support asymmetric matchers', () => {
expect(compareTextWithArray('foo', [expect.stringContaining('oo'), expect.stringContaining('oobb')], {}).result).toBe(true)
expect(compareTextWithArray('foo', [expect.not.stringContaining('oo'), expect.stringContaining('oobb')] , {}).result).toBe(false)
})

test('should support asymmetric matchers and using ignoreCase', () => {
expect(compareTextWithArray('FOO', [expect.stringContaining('oo'), expect.stringContaining('oobb')], { ignoreCase: true }).result).toBe(true)
expect(compareTextWithArray('FOO', [expect.not.stringContaining('oo'), expect.stringContaining('oobb')] , { ignoreCase: true }).result).toBe(false)
})
})

describe('compareNumbers', () => {
Expand Down

0 comments on commit add9838

Please sign in to comment.