From add983888da989a7c9dd1ed3b7cd5b5282cd1ba4 Mon Sep 17 00:00:00 2001 From: Fabien CELLIER <33258660+lacell75@users.noreply.github.com> Date: Tue, 6 Aug 2024 17:28:12 +0200 Subject: [PATCH] support asymeyric matcher for array (#1624) * support asymeyric matcher for array * Update src/utils.ts --------- Co-authored-by: Christian Bromann --- src/utils.ts | 5 ++- test/matchers/element/toHaveText.test.ts | 40 ++++++++++++++++++++++++ test/utils.test.ts | 10 ++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 3d1bfdeb..57039fc6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) } diff --git a/test/matchers/element/toHaveText.test.ts b/test/matchers/element/toHaveText.test.ts index 84809377..a8861e9a 100755 --- a/test/matchers/element/toHaveText.test.ts +++ b/test/matchers/element/toHaveText.test.ts @@ -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 diff --git a/test/utils.test.ts b/test/utils.test.ts index 21d084e4..650cc0f9 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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', () => {