Skip to content

Commit 2f369d3

Browse files
committed
Add UT of global config for multiple matchers
1 parent 901e77a commit 2f369d3

3 files changed

Lines changed: 124 additions & 4 deletions

File tree

test/matchers/beMatchers.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { vi, test, describe, expect } from 'vitest'
1+
import { vi, test, describe, expect, afterEach, beforeEach } from 'vitest'
22
import { $ } from '@wdio/globals'
33
import { matcherLastWordName } from '../__fixtures__/utils.js'
44
import * as Matchers from '../../src/matchers.js'
5+
import { setOptions } from '../../src/index.js'
6+
import { DEFAULT_OPTIONS } from '../../src/constants.js'
7+
import { executeCommandBe } from '../../src/utils.js'
58

69
vi.mock('@wdio/globals')
710

@@ -17,6 +20,15 @@ const beMatchers = {
1720
'toBeSelected': 'isSelected',
1821
} satisfies Partial<Record<keyof typeof Matchers, keyof WebdriverIO.Element>>
1922

23+
vi.mock('../../src/utils.js', async (importOriginal) => {
24+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
25+
const actual = await importOriginal<typeof import('../../src/utils.js')>()
26+
return {
27+
...actual,
28+
executeCommandBe: vi.fn(actual.executeCommandBe)
29+
}
30+
})
31+
2032
describe('be* matchers', () => {
2133
describe('Ensure all toBe matchers are covered', () => {
2234

@@ -135,6 +147,35 @@ Expected: "${matcherLastWordName(matcherName)}"
135147
Received: "not ${matcherLastWordName(matcherName)}"`
136148
)
137149
})
150+
151+
describe('global options', () => {
152+
const defaultOptions = { ...DEFAULT_OPTIONS }
153+
154+
beforeEach(() => {
155+
// Set global options to custom values before each test
156+
setOptions({ wait: 99, interval: 101 })
157+
})
158+
159+
afterEach(() => {
160+
// Reset options after each test to avoid side effects
161+
setOptions(defaultOptions)
162+
expect(DEFAULT_OPTIONS.wait).not.toBe(99)
163+
})
164+
165+
test('should use globally set default options', async () => {
166+
const el = await $('sel')
167+
el.isDisplayed = vi.fn().mockResolvedValue(true)
168+
169+
await matcherFn.call({}, el)
170+
171+
expect(DEFAULT_OPTIONS.wait).toBe(99)
172+
expect(executeCommandBe).toHaveBeenCalledWith(
173+
el,
174+
expect.anything(),
175+
expect.objectContaining({ wait: 99, interval: 101 })
176+
)
177+
})
178+
})
138179
})
139180
})
140181
})

test/matchers/element/toBeDisplayed.test.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { vi, test, describe, expect } from 'vitest'
1+
import { vi, test, describe, expect, afterEach, beforeEach } from 'vitest'
22
import { $ } from '@wdio/globals'
33

44
import { toBeDisplayed } from '../../../src/matchers/element/toBeDisplayed.js'
55
import { executeCommandBe } from '../../../src/utils.js'
6-
import { DEFAULT_OPTIONS } from '../../../src/constants.js'
6+
import { DEFAULT_OPTIONS, DEFAULT_OPTIONS_TO_BE_DISPLAYED } from '../../../src/constants.js'
7+
import { setOptions } from '../../../src/index.js'
8+
import type { ChainablePromiseElement } from 'webdriverio'
79

810
vi.mock('@wdio/globals')
911
vi.mock('../../../src/utils.js', async (importOriginal) => {
@@ -15,7 +17,7 @@ vi.mock('../../../src/utils.js', async (importOriginal) => {
1517
}
1618
})
1719

18-
describe('toBeDisplayed', () => {
20+
describe(toBeDisplayed, () => {
1921
/**
2022
* result is inverted for toBeDisplayed because it inverts isEnabled result
2123
* `!await el.isEnabled()`
@@ -189,4 +191,55 @@ Expected: "displayed"
189191
Received: "not displayed"`
190192
)
191193
})
194+
195+
describe('global options', () => {
196+
const defaultOptions = { ...DEFAULT_OPTIONS }
197+
198+
let el: ChainablePromiseElement
199+
200+
beforeEach(async () => {
201+
// Reset to known state before each test
202+
setOptions({ wait: 99, interval: 101 })
203+
el = await $('sel')
204+
el.isDisplayed = vi.fn().mockResolvedValue(true)
205+
206+
})
207+
208+
afterEach(() => {
209+
// Reset options after each test to avoid side effects
210+
setOptions(defaultOptions)
211+
expect(DEFAULT_OPTIONS.wait).not.toBe(99)
212+
})
213+
214+
test('should use globally set default options with executeCommandBe', async () => {
215+
await toBeDisplayed.call({}, el)
216+
217+
expect(DEFAULT_OPTIONS.wait).toBe(99)
218+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(99)
219+
expect(executeCommandBe).toHaveBeenCalledWith(
220+
el,
221+
expect.anything(),
222+
expect.objectContaining({ wait: 99, interval: 101 })
223+
)
224+
})
225+
226+
test('should use globally set default options with isDisplayed', async () => {
227+
228+
await toBeDisplayed.call({}, el)
229+
230+
expect(executeCommandBe).toHaveBeenCalledWith(
231+
el,
232+
expect.anything(),
233+
expect.objectContaining({ wait: 99, interval: 101 })
234+
)
235+
expect(el.isDisplayed).toHaveBeenCalledWith(
236+
{
237+
withinViewport: false,
238+
contentVisibilityAuto: true,
239+
opacityProperty: true,
240+
visibilityProperty: true
241+
}
242+
)
243+
})
244+
})
192245
})

test/options.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { test, expect, describe, beforeEach, afterEach } from 'vitest'
2+
import { setOptions } from '../src/index.js'
3+
import { DEFAULT_OPTIONS, DEFAULT_OPTIONS_TO_BE_DISPLAYED } from '../src/constants.js'
4+
5+
describe('setDefaultOptions', () => {
6+
const defaultOptions = { ...DEFAULT_OPTIONS }
7+
beforeEach(() => {
8+
// Set global options to custom values before each test
9+
setOptions({ wait: 2000, interval: 100 })
10+
})
11+
afterEach(() => {
12+
// Reset global options to default values after each test
13+
setOptions(defaultOptions)
14+
})
15+
16+
test('setDefaultOptions should update both DEFAULT_OPTIONS_TO_BE_DISPLAYED and DEFAULT_OPTIONS', () => {
17+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).not.toBe(1234)
18+
expect(DEFAULT_OPTIONS.wait).not.toBe(1234)
19+
20+
setOptions({ wait: 1234 })
21+
22+
expect(DEFAULT_OPTIONS_TO_BE_DISPLAYED.wait).toBe(1234)
23+
expect(DEFAULT_OPTIONS.wait).toBe(1234)
24+
})
25+
})
26+

0 commit comments

Comments
 (0)