Skip to content

Commit a9d19a4

Browse files
authored
fix case-insensitive with stringContaining matcher (#1629)
1 parent 90a9219 commit a9d19a4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import isEqual from 'lodash.isequal'
22
import type { ParsedCSSValue } from 'webdriverio'
3+
import expect from 'expect'
34

45
import { DEFAULT_OPTIONS } from './constants.js'
56
import type { WdioElementMaybePromise } from './types.js'
@@ -18,6 +19,10 @@ export function isAsymmeyricMatcher(expected: any): expected is ExpectWebdriverI
1819
return typeof expected === 'object' && '$$typeof' in expected && expected.$$typeof === asymmetricMatcher && expected.asymmetricMatch
1920
}
2021

22+
function isStringContainingMatcher(expected: any): expected is ExpectWebdriverIO.PartialMatcher {
23+
return isAsymmeyricMatcher(expected) && ['StringContaining', 'StringNotContaining'].includes(expected.toString())
24+
}
25+
2126
/**
2227
* wait for expectation to succeed
2328
* @param condition function
@@ -159,6 +164,10 @@ export const compareText = (
159164
actual = actual.toLowerCase()
160165
if (typeof expected === 'string') {
161166
expected = expected.toLowerCase()
167+
} else if (isStringContainingMatcher(expected)) {
168+
expected = (expected.toString() === 'StringContaining'
169+
? expect.stringContaining(expected.sample?.toString().toLowerCase())
170+
: expect.not.stringContaining(expected.sample?.toString().toLowerCase())) as ExpectWebdriverIO.PartialMatcher
162171
}
163172
}
164173

test/utils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('utils', () => {
2121

2222
test('should pass if same word but wrong case and using ignoreCase', () => {
2323
expect(compareText(' FOO ', 'foo', { ignoreCase: true }).result).toBe(true)
24+
expect(compareText(' foo ', 'FOO', { ignoreCase: true }).result).toBe(true)
2425
})
2526

2627
test('should pass if string contains expected and using containing', () => {
@@ -31,6 +32,14 @@ describe('utils', () => {
3132
expect(compareText('foo', expect.stringContaining('oo'), {}).result).toBe(true)
3233
expect(compareText('foo', expect.not.stringContaining('oo'), {}).result).toBe(false)
3334
})
35+
36+
test('should support asymmetric matchers and using ignoreCase', () => {
37+
expect(compareText(' FOO ', expect.stringContaining('foo'), { ignoreCase: true }).result).toBe(true)
38+
expect(compareText(' FOO ', expect.not.stringContaining('foo'), { ignoreCase: true }).result).toBe(false)
39+
expect(compareText(' Foo ', expect.stringContaining('FOO'), { ignoreCase: true }).result).toBe(true)
40+
expect(compareText(' Foo ', expect.not.stringContaining('FOO'), { ignoreCase: true }).result).toBe(false)
41+
expect(compareText(' foo ', expect.stringContaining('foo'), { ignoreCase: true }).result).toBe(true)
42+
})
3443
})
3544

3645
describe('compareTextWithArray', () => {

types/expect-webdriverio.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ declare namespace ExpectWebdriverIO {
560560
sample?: any
561561
$$typeof: symbol
562562
asymmetricMatch(...args: any[]): boolean
563+
toString(): string
563564
}
564565
}
565566

0 commit comments

Comments
 (0)