Skip to content

Commit

Permalink
fix case-insensitive with stringContaining matcher (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
lacell75 authored Aug 12, 2024
1 parent 90a9219 commit a9d19a4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isEqual from 'lodash.isequal'
import type { ParsedCSSValue } from 'webdriverio'
import expect from 'expect'

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

function isStringContainingMatcher(expected: any): expected is ExpectWebdriverIO.PartialMatcher {
return isAsymmeyricMatcher(expected) && ['StringContaining', 'StringNotContaining'].includes(expected.toString())
}

/**
* wait for expectation to succeed
* @param condition function
Expand Down Expand Up @@ -159,6 +164,10 @@ export const compareText = (
actual = actual.toLowerCase()
if (typeof expected === 'string') {
expected = expected.toLowerCase()
} else if (isStringContainingMatcher(expected)) {
expected = (expected.toString() === 'StringContaining'
? expect.stringContaining(expected.sample?.toString().toLowerCase())
: expect.not.stringContaining(expected.sample?.toString().toLowerCase())) as ExpectWebdriverIO.PartialMatcher
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('utils', () => {

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

test('should pass if string contains expected and using containing', () => {
Expand All @@ -31,6 +32,14 @@ describe('utils', () => {
expect(compareText('foo', expect.stringContaining('oo'), {}).result).toBe(true)
expect(compareText('foo', expect.not.stringContaining('oo'), {}).result).toBe(false)
})

test('should support asymmetric matchers and using ignoreCase', () => {
expect(compareText(' FOO ', expect.stringContaining('foo'), { ignoreCase: true }).result).toBe(true)
expect(compareText(' FOO ', expect.not.stringContaining('foo'), { ignoreCase: true }).result).toBe(false)
expect(compareText(' Foo ', expect.stringContaining('FOO'), { ignoreCase: true }).result).toBe(true)
expect(compareText(' Foo ', expect.not.stringContaining('FOO'), { ignoreCase: true }).result).toBe(false)
expect(compareText(' foo ', expect.stringContaining('foo'), { ignoreCase: true }).result).toBe(true)
})
})

describe('compareTextWithArray', () => {
Expand Down
1 change: 1 addition & 0 deletions types/expect-webdriverio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ declare namespace ExpectWebdriverIO {
sample?: any
$$typeof: symbol
asymmetricMatch(...args: any[]): boolean
toString(): string
}
}

Expand Down

0 comments on commit a9d19a4

Please sign in to comment.