-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathglobals_mock.test.ts
More file actions
167 lines (131 loc) · 6.3 KB
/
globals_mock.test.ts
File metadata and controls
167 lines (131 loc) · 6.3 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, it, expect, vi } from 'vitest'
import { $, $$ } from '@wdio/globals'
import { notFoundElementFactory } from './__mocks__/@wdio/globals.js'
vi.mock('@wdio/globals')
describe('globals mock', () => {
describe($, () => {
it('should return a ChainablePromiseElement', async () => {
const el = $('foo')
// It behaves like a promise
expect(el).toHaveProperty('then')
expect(el).toBeInstanceOf(Promise)
})
it('should resolve to an element', async () => {
const el = await $('foo')
expect(el.selector).toBe('foo')
// The resolved element should not be the proxy, but the underlying mock
expect(el.getElement).toBeDefined()
})
it('should resolve to an element on getElement', async () => {
const el = await $('foo')
const resolvedEl = await el.getElement()
expect(resolvedEl).toBe(el)
})
it('should allow calling getElement on the chainable promise', async () => {
const chainable = $('foo')
// 'getElement' should not be present in the chainable object if checked via `in`
// based on user request logs: 'getElements' in elements false
expect('getElement' in chainable).toBe(false)
// But it should be callable
const el = chainable.getElement()
expect(el).toBeInstanceOf(Promise)
const awaitedEl = await el
expect(awaitedEl.selector).toBe('foo')
expect(awaitedEl.getElement).toBeDefined()
})
it('should allow calling methods like isEnabled on the chainable promise', async () => {
const check = $('foo').isEnabled()
expect(check).toBeInstanceOf(Promise)
const result = await check
expect(result).toBe(true)
})
it('should allow chaining simple methods with await', async () => {
const text = await $('foo').getText()
expect(text).toBe(' Valid Text ')
})
})
describe($$, () => {
it('should return a ChainablePromiseArray', async () => {
const els = $$('foo')
expect(els).toHaveProperty('then')
// @ts-expect-error
expect(typeof els.then).toBe('function')
})
it('should resolve to an element array', async () => {
const els = await $$('foo')
expect(Array.isArray(els)).toBe(true)
expect(els).toHaveLength(2) // Default length in mock
expect(els.selector).toBe('foo')
})
it('should returns ElementArray on getElements', async () => {
const els = await $$('foo')
expect(await els.getElements()).toEqual(els)
})
it('should allow calling getElements on the chainable promise', async () => {
const chainable = $$('foo')
// 'getElements' should not be present in the chainable object if checked via `in`
expect('getElements' in chainable).toBe(false)
// But it should be callable
const els = await chainable.getElements()
expect(els).toHaveLength(2) // Default length
})
it('should allow iterating if awaited', async () => {
const els = await $$('foo')
// map is available on the resolved array
const selectors = els.map(el => el.selector)
expect(selectors).toEqual(['foo', 'foo'])
})
it('should allow calling methods like isEnabled on elements of chainable promise', async () => {
const check = $$('foo')[0].isEnabled()
expect(check).toBeInstanceOf(Promise)
const result = await check
expect(result).toBe(true)
})
it('should allow chaining simple methods with await', async () => {
const text = await $$('foo')[0].getText()
expect(text).toBe(' Valid Text ')
})
it('should returns ElementArray on getElements', async () => {
const els = await $$('foo')
expect(await els.getElements()).toEqual(els)
})
it('should return a promise-like object when accessing index out of bounds', () => {
const el = $$('foo')[3]
// It shouldn't throw synchronously
expect(el).toBeDefined()
expect(el).toBeInstanceOf(Promise)
// Methods should return a Promise
const getEl = el.getElement()
expect(getEl).toBeInstanceOf(Promise)
// catch unhandled rejection to avoid warnings
getEl.catch(() => {})
const getText = el.getText()
expect(getText).toBeInstanceOf(Promise)
// catch unhandled rejection to avoid warnings
getText.catch(() => {})
})
it('should throw "Index out of bounds" when awaiting index out of bounds', async () => {
await expect(async () => await $$('foo')[3]).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.')
await expect(async () => await $$('foo')[3].getElement()).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.')
await expect(async () => await $$('foo')[3].getText()).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.')
})
})
describe('notFoundElementFactory', () => {
it('should return false for isExisting', async () => {
const el = notFoundElementFactory('not-found')
expect(await el.isExisting()).toBe(false)
})
it('should resolve to itself when calling getElement', async () => {
const el = notFoundElementFactory('not-found')
expect(await el.getElement()).toBe(el)
})
it('should throw error on method calls', async () => {
const el = notFoundElementFactory('not-found')
expect(() => el.click()).toThrow("Can't call click on element with selector not-found because element wasn't found")
})
it('should throw error when awaiting a method call (sync throw)', async () => {
const el = notFoundElementFactory('not-found')
expect(() => el.getText()).toThrow("Can't call getText on element with selector not-found because element wasn't found")
})
})
})