forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefetchElements.test.ts
More file actions
78 lines (65 loc) · 2.61 KB
/
refetchElements.test.ts
File metadata and controls
78 lines (65 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { vi, test, describe, beforeEach, expect } from 'vitest'
import { $$ } from '@wdio/globals'
import { refetchElements } from '../../src/util/refetchElements.js'
const createMockElementArray = (length: number): WebdriverIO.ElementArray => {
const array = Array.from({ length }, () => ({}))
const mockArray = {
selector: 'parent',
get length() { return array.length },
set length(newLength: number) { array.length = newLength },
parent: {
$: vi.fn(),
$$: vi.fn().mockReturnValue(array),
},
foundWith: '$$',
props: [],
[Symbol.iterator]: array[Symbol.iterator].bind(array),
filter: vi.fn().mockReturnThis(),
map: vi.fn().mockReturnThis(),
find: vi.fn().mockReturnThis(),
forEach: vi.fn(),
some: vi.fn(),
every: vi.fn(),
slice: vi.fn().mockReturnThis(),
toArray: vi.fn().mockReturnThis(),
}
return Object.assign(array, mockArray) as unknown as WebdriverIO.ElementArray
}
vi.mock('@wdio/globals', () => ({
$$: vi.fn().mockImplementation(() => createMockElementArray(5))
}))
describe('refetchElements', () => {
describe('given WebdriverIO.ElementArray type', () => {
let elements: WebdriverIO.ElementArray
beforeEach(async () => {
elements = (await $$('parent')) as unknown as WebdriverIO.ElementArray
// @ts-ignore
elements.parent._length = 5
})
test('default', async () => {
const actual = await refetchElements(elements, 5, true)
expect(actual.length).toBe(5)
})
test('wait is 0', async () => {
const actual = await refetchElements(elements, 0, true)
expect(actual).toEqual(elements)
})
test('should call $$ with all props', async () => {
elements.props = ['prop1', 'prop2']
await refetchElements(elements, 5, true)
expect(elements.parent.$$).toHaveBeenCalledWith('parent', 'prop1', 'prop2')
})
test('should call $$ with the proper parent this context', async () => {
const parentFoundWith = vi.mocked(elements.parent.$$)
await refetchElements(elements, 5, true)
expect(parentFoundWith.mock.contexts[0]).toBe(elements.parent)
})
})
describe('given WebdriverIO.Element[] type', () => {
const elements: WebdriverIO.Element[] = [] as unknown as WebdriverIO.Element[]
test('default', async () => {
const actual = await refetchElements(elements, 0)
expect(actual).toEqual([])
})
})
})