|
1 | | -import { describe, expect, test, vi } from 'vitest' |
| 1 | +import { describe, expect, test, vi, beforeEach } from 'vitest' |
2 | 2 | import { $, $$ } from '@wdio/globals' |
3 | | -import { executeCommand } from '../../src/util/executeCommand' |
| 3 | +import { executeCommand, defaultMultipleElementsIterationStrategy } from '../../src/util/executeCommand' |
4 | 4 |
|
5 | 5 | vi.mock('@wdio/globals') |
6 | 6 |
|
@@ -222,3 +222,71 @@ describe(executeCommand, () => { |
222 | 222 | }) |
223 | 223 | }) |
224 | 224 | }) |
| 225 | + |
| 226 | +describe(defaultMultipleElementsIterationStrategy, () => { |
| 227 | + |
| 228 | + describe('given single element', () => { |
| 229 | + let singleElement: WebdriverIO.Element |
| 230 | + let condition: any |
| 231 | + |
| 232 | + beforeEach(async () => { |
| 233 | + singleElement = await $('single-mock-element').getElement() |
| 234 | + condition = vi.fn().mockImplementation(async (_el, expected) => ({ result: true, value: expected })) |
| 235 | + }) |
| 236 | + |
| 237 | + test('should handle single element and single expected value', async () => { |
| 238 | + const result = await defaultMultipleElementsIterationStrategy(singleElement, 'val', condition) |
| 239 | + |
| 240 | + expect(result).toEqual([{ result: true, value: 'val' }]) |
| 241 | + }) |
| 242 | + |
| 243 | + test('should fail if single element and expected value is array (default)', async () => { |
| 244 | + const result = await defaultMultipleElementsIterationStrategy(singleElement, ['val'], condition) |
| 245 | + |
| 246 | + expect(result).toEqual([{ result: false, value: 'Expected value cannot be an array' }]) |
| 247 | + }) |
| 248 | + |
| 249 | + test('should handle single element and expected value array if supportArrayForSingleElement is true', async () => { |
| 250 | + const result = await defaultMultipleElementsIterationStrategy(singleElement, ['val'], condition, { supportArrayForSingleElement: true }) |
| 251 | + |
| 252 | + expect(result).toEqual([{ result: true, value: ['val'] }]) |
| 253 | + expect(condition).toHaveBeenCalledTimes(1) |
| 254 | + }) |
| 255 | + }) |
| 256 | + |
| 257 | + describe('given multiple elements', () => { |
| 258 | + let elements: WebdriverIO.ElementArray |
| 259 | + let condition: () => Promise<{ result: boolean; value: string }> |
| 260 | + |
| 261 | + beforeEach(async () => { |
| 262 | + elements = await $$('elements').getElements() |
| 263 | + expect(elements.length).toBe(2) |
| 264 | + condition = vi.fn() |
| 265 | + .mockResolvedValueOnce({ result: true, value: 'val1' }) |
| 266 | + .mockResolvedValueOnce({ result: true, value: 'val2' }) |
| 267 | + }) |
| 268 | + |
| 269 | + test('should iterate over array of elements and array of expected values', async () => { |
| 270 | + const result = await defaultMultipleElementsIterationStrategy(elements, ['val1', 'val2'], condition) |
| 271 | + |
| 272 | + expect(result).toEqual([{ result: true, value: 'val1' }, { result: true, value: 'val2' }]) |
| 273 | + expect(condition).toHaveBeenCalledTimes(2) |
| 274 | + }) |
| 275 | + |
| 276 | + test('should fail if array lengths mismatch', async () => { |
| 277 | + const result = await defaultMultipleElementsIterationStrategy([elements[0]] as any, ['val1', 'val2'], condition) |
| 278 | + |
| 279 | + expect(result).toEqual([{ result: false, value: 'Received array length 1, expected 2' }]) |
| 280 | + }) |
| 281 | + |
| 282 | + test('should iterate over array of elements and single expected value', async () => { |
| 283 | + condition = vi.fn() |
| 284 | + .mockResolvedValue({ result: true, value: 'val' }) |
| 285 | + |
| 286 | + const result = await defaultMultipleElementsIterationStrategy(elements, 'val', condition) |
| 287 | + |
| 288 | + expect(result).toEqual([{ result: true, value: 'val' }, { result: true, value: 'val' }]) |
| 289 | + expect(condition).toHaveBeenCalledTimes(2) |
| 290 | + }) |
| 291 | + }) |
| 292 | +}) |
0 commit comments