Skip to content

Commit 2c40ce5

Browse files
committed
Increase coverage
1 parent d28ffb8 commit 2c40ce5

7 files changed

Lines changed: 117 additions & 30 deletions

File tree

src/matchers/element/toHaveElementProperty.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,8 @@ export async function toHaveElementProperty(
7070
{ wait: options.wait, interval: options.interval }
7171
)
7272

73-
let message: string
74-
if (expectedValue === undefined) {
75-
message = enhanceError(el, !isNot, pass, this, verb, expectation, property, options)
76-
} else {
77-
const expected = wrapExpectedWithArray(el, prop, expectedValue)
78-
message = enhanceError(el, expected, prop, this, verb, expectation, property, options)
79-
}
73+
const expected = wrapExpectedWithArray(el, prop, expectedValue)
74+
const message = enhanceError(el, expected, prop, this, verb, expectation, property, options)
8075

8176
const result: ExpectWebdriverIO.AssertionResult = {
8277
pass,

src/util/executeCommand.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ export async function executeCommand<T>(
4444
if (!singleElementCompareStrategy && !mutipleElementsCompareStrategy) { throw new Error('No condition or customMultipleElementCompareStrategy provided to executeCommand') }
4545

4646
const elementOrArray = element ? element : elements ? elements : undefined
47+
48+
/* v8 ignore next -- @preserve -- should be unreachable due to checks above */
4749
if (!elementOrArray) {
48-
// Should be unreachable due to checks above
4950
throw new Error('No elements to process in executeCommand')
5051
}
5152

@@ -57,7 +58,7 @@ export async function executeCommand<T>(
5758
} else if (singleElementCompareStrategy && elements) {
5859
results = await map(elements, (el: WebdriverIO.Element) => singleElementCompareStrategy(el))
5960
} else {
60-
// To please tsc but never reached due to checks above
61+
/* v8 ignore next -- @preserve -- To please tsc but never reached due to checks above */
6162
throw new Error('Unable to process executeCommand with the provided parameters')
6263
}
6364

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
1-
import { vi, test, expect } from 'vitest'
1+
import { vi, test, expect, describe } from 'vitest'
22
import { browser } from '@wdio/globals'
3+
import logger from '@wdio/logger'
34

45
import { toHaveClipboardText } from '../../../src/matchers/browser/toHaveClipboardText'
56

67
vi.mock('@wdio/globals')
8+
vi.mock('@wdio/logger', () => ({
9+
default: vi.fn().mockReturnValue({
10+
warn: vi.fn()
11+
})
12+
}))
713

814
const beforeAssertion = vi.fn()
915
const afterAssertion = vi.fn()
1016

11-
test('toHaveClipboardText', async () => {
12-
browser.execute = vi.fn().mockResolvedValue('some clipboard text')
17+
describe(toHaveClipboardText, () => {
18+
test('success', async () => {
19+
browser.execute = vi.fn().mockResolvedValue('some clipboard text')
1320

14-
const result = await toHaveClipboardText(browser, 'some ClipBoard text', { ignoreCase: true, beforeAssertion, afterAssertion })
15-
expect(result.pass).toBe(true)
16-
expect(beforeAssertion).toBeCalledWith({
17-
matcherName: 'toHaveClipboardText',
18-
expectedValue: 'some ClipBoard text',
19-
options: { ignoreCase: true, beforeAssertion, afterAssertion }
21+
const result = await toHaveClipboardText(browser, 'some ClipBoard text', { ignoreCase: true, beforeAssertion, afterAssertion })
22+
expect(result.pass).toBe(true)
23+
expect(beforeAssertion).toBeCalledWith({
24+
matcherName: 'toHaveClipboardText',
25+
expectedValue: 'some ClipBoard text',
26+
options: { ignoreCase: true, beforeAssertion, afterAssertion }
27+
})
28+
expect(afterAssertion).toBeCalledWith({
29+
matcherName: 'toHaveClipboardText',
30+
expectedValue: 'some ClipBoard text',
31+
options: { ignoreCase: true, beforeAssertion, afterAssertion },
32+
result
33+
})
2034
})
21-
expect(afterAssertion).toBeCalledWith({
22-
matcherName: 'toHaveClipboardText',
23-
expectedValue: 'some ClipBoard text',
24-
options: { ignoreCase: true, beforeAssertion, afterAssertion },
25-
result
35+
36+
test('failure check with message', async () => {
37+
browser.execute = vi.fn().mockResolvedValue('actual text')
38+
39+
const result = await toHaveClipboardText(browser, 'expected text', { wait: 1 })
40+
41+
expect(result.pass).toBe(false)
42+
expect(result.message()).toEqual(`\
43+
Expect browser to have clipboard text
44+
45+
Expected: "expected text"
46+
Received: "actual text"`
47+
)
48+
})
49+
50+
test('should log warning if setPermissions fails', async () => {
51+
browser.execute = vi.fn().mockResolvedValue('text')
52+
const warnSpy = vi.fn()
53+
vi.mocked(logger).mockReturnValue({ warn: warnSpy } as any)
54+
vi.mocked(browser.setPermissions).mockRejectedValueOnce(new Error('unsupported'))
55+
56+
const result = await toHaveClipboardText(browser, 'text', { wait: 0 })
57+
58+
expect(result.pass).toBe(true)
59+
expect(browser.setPermissions).toHaveBeenCalledWith({ name: 'clipboard-read' }, 'granted')
2660
})
2761
})

test/matchers/element/toHaveWidth.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Received : [50, 50]`
214214
)
215215
})
216216

217-
test.only('not - failure lte - pass should be true', async () => {
217+
test('not - failure lte - pass should be true', async () => {
218218
elements.forEach(el => el.getSize = vi.fn().mockResolvedValue(50))
219219

220220
const result = await thisNotContext.toHaveWidth(elements, { lte: 51 })
@@ -228,7 +228,7 @@ Received : [50, 50]`
228228
)
229229
})
230230

231-
test.only('not - failure lte only first element - pass should be true', async () => {
231+
test('not - failure lte only first element - pass should be true', async () => {
232232
elements.forEach(el => el.getSize = vi.fn().mockResolvedValue(50))
233233

234234
const result = await thisNotContext.toHaveWidth(elements, [{ lte: 51 }, 51])
@@ -242,7 +242,7 @@ Received : [50, 50]`
242242
)
243243
})
244244

245-
test.only('not - failure gte - pass should be true', async () => {
245+
test('not - failure gte - pass should be true', async () => {
246246
elements.forEach(el => el.getSize = vi.fn().mockResolvedValue(50))
247247

248248
const result = await thisNotContext.toHaveWidth(elements, { gte: 49 })

test/matchers/elements/toBeElementsArrayOfSize.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Received : 2`
9494
)
9595
})
9696

97-
test.only('not - failure - lte - pass should be true', async () => {
97+
test('not - failure - lte - pass should be true', async () => {
9898
const result = await thisNotContext.toBeElementsArrayOfSize(els, { lte: 3 })
9999

100100
expect(result.pass).toBe(true) // failure, boolean is inverted later in .not cases
@@ -106,7 +106,7 @@ Received : 2`
106106
)
107107
})
108108

109-
test.only('not - failure - gte - pass should be true', async () => {
109+
test('not - failure - gte - pass should be true', async () => {
110110
const result = await thisNotContext.toBeElementsArrayOfSize(els, { gte: 1 })
111111

112112
expect(result.pass).toBe(true) // failure, boolean is inverted later in .not cases

test/util/stringUtil.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { isString, toJsonString } from '../../src/util/stringUtil'
3+
4+
describe('stringUtil', () => {
5+
describe(isString, () => {
6+
test('should return true for a string', () => {
7+
expect(isString('hello')).toBe(true)
8+
expect(isString('')).toBe(true)
9+
})
10+
11+
test('should return false for non-string values', () => {
12+
expect(isString(123)).toBe(false)
13+
expect(isString(true)).toBe(false)
14+
expect(isString({})).toBe(false)
15+
expect(isString(null)).toBe(false)
16+
expect(isString(undefined)).toBe(false)
17+
expect(isString([])).toBe(false)
18+
})
19+
})
20+
21+
describe(toJsonString, () => {
22+
test('should return the string as is if input is a string', () => {
23+
expect(toJsonString('hello')).toBe('hello')
24+
})
25+
26+
test('should return JSON string if input is a serializable object', () => {
27+
const obj = { foo: 'bar', num: 123 }
28+
expect(toJsonString(obj)).toBe(JSON.stringify(obj))
29+
})
30+
31+
test('should return string representation if JSON.stringify throws', () => {
32+
const circular: any = { foo: 'bar' }
33+
circular.self = circular
34+
35+
expect(toJsonString(circular)).toBe('[object Object]')
36+
expect(toJsonString(BigInt(9007199254740991))).toBe('9007199254740991')
37+
})
38+
39+
test('should return string representation for other types', () => {
40+
expect(toJsonString(123)).toBe('123')
41+
expect(toJsonString(true)).toBe('true')
42+
expect(toJsonString(null)).toBe('null')
43+
})
44+
45+
test('should handle undefined correctly', () => {
46+
expect(toJsonString(undefined)).toBeUndefined()
47+
})
48+
})
49+
})

test/utils.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ Received: "not displayed"`)
291291
beforeEach(() => {
292292
// Success for `.not`
293293
vi.mocked(command).mockResolvedValue(false)
294-
negatedContext = { ...context, isNot: true }
294+
negatedContext = {
295+
expectation: 'displayed',
296+
verb: 'be',
297+
isNot: true
298+
}
295299
})
296300

297301
test('should succeed so pass=false since it is inverted later', async () => {
@@ -433,7 +437,11 @@ Expect ${selectorName} to be displayed
433437
beforeEach(() => {
434438
// Success for `.not`
435439
vi.mocked(command).mockResolvedValue(false)
436-
negatedContext = { ...context, isNot: true }
440+
negatedContext = {
441+
expectation: 'displayed',
442+
verb: 'be',
443+
isNot: true
444+
}
437445
})
438446

439447
test('should succeed so pass=false since it is inverted later', async () => {

0 commit comments

Comments
 (0)