|
1 | | -import { vi, test, expect } from 'vitest' |
| 1 | +import { vi, test, expect, describe } from 'vitest' |
2 | 2 | import { browser } from '@wdio/globals' |
| 3 | +import logger from '@wdio/logger' |
3 | 4 |
|
4 | 5 | import { toHaveClipboardText } from '../../../src/matchers/browser/toHaveClipboardText' |
5 | 6 |
|
6 | 7 | vi.mock('@wdio/globals') |
| 8 | +vi.mock('@wdio/logger', () => ({ |
| 9 | + default: vi.fn().mockReturnValue({ |
| 10 | + warn: vi.fn() |
| 11 | + }) |
| 12 | +})) |
7 | 13 |
|
8 | 14 | const beforeAssertion = vi.fn() |
9 | 15 | const afterAssertion = vi.fn() |
10 | 16 |
|
11 | | -test('toHaveClipboardText', async () => { |
12 | | - browser.execute = vi.fn().mockResolvedValue('some clipboard text') |
| 17 | +describe(toHaveClipboardText, () => { |
| 18 | + test('success', async () => { |
| 19 | + browser.execute = vi.fn().mockResolvedValue('some clipboard text') |
13 | 20 |
|
14 | | - const result = await toHaveClipboardText(browser, 'some ClipBoard text', { ignoreCase: true, beforeAssertion, afterAssertion }) |
15 | | - expect(result.pass).toBe(true) |
16 | | - expect(beforeAssertion).toBeCalledWith({ |
17 | | - matcherName: 'toHaveClipboardText', |
18 | | - expectedValue: 'some ClipBoard text', |
19 | | - options: { ignoreCase: true, beforeAssertion, afterAssertion } |
| 21 | + const result = await toHaveClipboardText(browser, 'some ClipBoard text', { ignoreCase: true, beforeAssertion, afterAssertion }) |
| 22 | + expect(result.pass).toBe(true) |
| 23 | + expect(beforeAssertion).toBeCalledWith({ |
| 24 | + matcherName: 'toHaveClipboardText', |
| 25 | + expectedValue: 'some ClipBoard text', |
| 26 | + options: { ignoreCase: true, beforeAssertion, afterAssertion } |
| 27 | + }) |
| 28 | + expect(afterAssertion).toBeCalledWith({ |
| 29 | + matcherName: 'toHaveClipboardText', |
| 30 | + expectedValue: 'some ClipBoard text', |
| 31 | + options: { ignoreCase: true, beforeAssertion, afterAssertion }, |
| 32 | + result |
| 33 | + }) |
20 | 34 | }) |
21 | | - expect(afterAssertion).toBeCalledWith({ |
22 | | - matcherName: 'toHaveClipboardText', |
23 | | - expectedValue: 'some ClipBoard text', |
24 | | - options: { ignoreCase: true, beforeAssertion, afterAssertion }, |
25 | | - result |
| 35 | + |
| 36 | + test('failure check with message', async () => { |
| 37 | + browser.execute = vi.fn().mockResolvedValue('actual text') |
| 38 | + |
| 39 | + const result = await toHaveClipboardText(browser, 'expected text', { wait: 1 }) |
| 40 | + |
| 41 | + expect(result.pass).toBe(false) |
| 42 | + expect(result.message()).toEqual(`\ |
| 43 | +Expect browser to have clipboard text |
| 44 | +
|
| 45 | +Expected: "expected text" |
| 46 | +Received: "actual text"` |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + test('should log warning if setPermissions fails', async () => { |
| 51 | + browser.execute = vi.fn().mockResolvedValue('text') |
| 52 | + const warnSpy = vi.fn() |
| 53 | + vi.mocked(logger).mockReturnValue({ warn: warnSpy } as any) |
| 54 | + vi.mocked(browser.setPermissions).mockRejectedValueOnce(new Error('unsupported')) |
| 55 | + |
| 56 | + const result = await toHaveClipboardText(browser, 'text', { wait: 0 }) |
| 57 | + |
| 58 | + expect(result.pass).toBe(true) |
| 59 | + expect(browser.setPermissions).toHaveBeenCalledWith({ name: 'clipboard-read' }, 'granted') |
26 | 60 | }) |
27 | 61 | }) |
0 commit comments