|
1 | 1 | import { describe, it, expect, vi } from 'vitest' |
2 | 2 | import { $, $$ } from '@wdio/globals' |
| 3 | +import { notFoundElementFactory } from './__mocks__/@wdio/globals.js' |
3 | 4 |
|
4 | 5 | vi.mock('@wdio/globals') |
5 | 6 |
|
@@ -90,5 +91,52 @@ describe('globals mock', () => { |
90 | 91 |
|
91 | 92 | expect(await els.getElements()).toEqual(els) |
92 | 93 | }) |
| 94 | + |
| 95 | + it('should return a promise-like object when accessing index out of bounds', () => { |
| 96 | + const el = $$('foo')[3] |
| 97 | + // It shouldn't throw synchronously |
| 98 | + expect(el).toBeDefined() |
| 99 | + expect(el).toBeInstanceOf(Promise) |
| 100 | + expect(typeof (el as any).then).toBe('function') |
| 101 | + |
| 102 | + // Methods should return a Promise |
| 103 | + const p1 = el.getElement() |
| 104 | + expect(p1).toBeInstanceOf(Promise) |
| 105 | + // catch unhandled rejection to avoid warnings |
| 106 | + p1.catch(() => {}) |
| 107 | + |
| 108 | + const p2 = el.getText() |
| 109 | + expect(p2).toBeInstanceOf(Promise) |
| 110 | + // catch unhandled rejection to avoid warnings |
| 111 | + p2.catch(() => {}) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should throw "Index out of bounds" when awaiting index out of bounds', async () => { |
| 115 | + await expect(async () => await $$('foo')[3]).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.') |
| 116 | + await expect(async () => await $$('foo')[3].getElement()).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.') |
| 117 | + await expect(async () => await $$('foo')[3].getText()).rejects.toThrow('Index out of bounds! $$(foo) returned only 2 elements.') |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + describe('notFoundElementFactory', () => { |
| 122 | + it('should return false for isExisting', async () => { |
| 123 | + const el = notFoundElementFactory('not-found') |
| 124 | + expect(await el.isExisting()).toBe(false) |
| 125 | + }) |
| 126 | + |
| 127 | + it('should resolve to itself when calling getElement', async () => { |
| 128 | + const el = notFoundElementFactory('not-found') |
| 129 | + expect(await el.getElement()).toBe(el) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should throw error on method calls', async () => { |
| 133 | + const el = notFoundElementFactory('not-found') |
| 134 | + expect(() => el.click()).toThrow("Can't call click on element with selector not-found because element wasn't found") |
| 135 | + }) |
| 136 | + |
| 137 | + it('should throw error when awaiting a method call (sync throw)', async () => { |
| 138 | + const el = notFoundElementFactory('not-found') |
| 139 | + expect(() => el.getText()).toThrow("Can't call getText on element with selector not-found because element wasn't found") |
| 140 | + }) |
93 | 141 | }) |
94 | 142 | }) |
0 commit comments