|
1 | 1 | import { DEFAULT_WDA_PORT } from '@midscene/shared/constants'; |
2 | | -import { describe, expect, it } from 'vitest'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { IOSWebDriverClient } from '../../src/ios-webdriver-client'; |
3 | 4 | import type { IOSWebDriverClient as IOSWebDriverClientType } from '../../src/ios-webdriver-client'; |
4 | 5 |
|
| 6 | +function createClientWithSession() { |
| 7 | + const client = new IOSWebDriverClient({ |
| 8 | + port: DEFAULT_WDA_PORT, |
| 9 | + host: 'localhost', |
| 10 | + }); |
| 11 | + // Bypass createSession() — we only want to observe outbound HTTP calls. |
| 12 | + (client as any).sessionId = 'session-under-test'; |
| 13 | + const makeRequest = vi |
| 14 | + .spyOn(client as any, 'makeRequest') |
| 15 | + .mockResolvedValue(undefined); |
| 16 | + return { client, makeRequest }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('IOSWebDriverClient.typeText delivery modes', () => { |
| 20 | + it('sends the whole string in one /wda/keys request when delayMs is 0 (default)', async () => { |
| 21 | + const { client, makeRequest } = createClientWithSession(); |
| 22 | + |
| 23 | + await client.typeText('Al is amazing'); |
| 24 | + |
| 25 | + expect(makeRequest).toHaveBeenCalledTimes(1); |
| 26 | + expect(makeRequest).toHaveBeenCalledWith( |
| 27 | + 'POST', |
| 28 | + '/session/session-under-test/wda/keys', |
| 29 | + { |
| 30 | + value: [ |
| 31 | + 'A', |
| 32 | + 'l', |
| 33 | + ' ', |
| 34 | + 'i', |
| 35 | + 's', |
| 36 | + ' ', |
| 37 | + 'a', |
| 38 | + 'm', |
| 39 | + 'a', |
| 40 | + 'z', |
| 41 | + 'i', |
| 42 | + 'n', |
| 43 | + 'g', |
| 44 | + ], |
| 45 | + }, |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('emits one /wda/keys request per character when delayMs > 0', async () => { |
| 50 | + const { client, makeRequest } = createClientWithSession(); |
| 51 | + |
| 52 | + await client.typeText('Hi!', { delayMs: 1 }); |
| 53 | + |
| 54 | + expect(makeRequest).toHaveBeenCalledTimes(3); |
| 55 | + expect(makeRequest).toHaveBeenNthCalledWith( |
| 56 | + 1, |
| 57 | + 'POST', |
| 58 | + '/session/session-under-test/wda/keys', |
| 59 | + { value: ['H'] }, |
| 60 | + ); |
| 61 | + expect(makeRequest).toHaveBeenNthCalledWith( |
| 62 | + 2, |
| 63 | + 'POST', |
| 64 | + '/session/session-under-test/wda/keys', |
| 65 | + { value: ['i'] }, |
| 66 | + ); |
| 67 | + expect(makeRequest).toHaveBeenNthCalledWith( |
| 68 | + 3, |
| 69 | + 'POST', |
| 70 | + '/session/session-under-test/wda/keys', |
| 71 | + { value: ['!'] }, |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('trims surrounding whitespace and skips empty input', async () => { |
| 76 | + const { client, makeRequest } = createClientWithSession(); |
| 77 | + |
| 78 | + await client.typeText(' '); |
| 79 | + |
| 80 | + expect(makeRequest).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
5 | 84 | describe('IOSWebDriverClient - Simple Tests', () => { |
6 | 85 | describe('Module Structure', () => { |
7 | 86 | it('should export IOSWebDriverClient class', async () => { |
|
0 commit comments