UNPKG

4.32 kBTypeScriptView Raw
1/// <reference types="jest" />
2
3import { Dialog, ElementHandle, Page } from "puppeteer";
4
5/**
6 * Interval at which pageFunctions may be executed.
7 */
8type ExpectPolling = number | "mutation" | "raf";
9
10interface MatchSelector {
11 /**
12 * A selector type
13 */
14 type: "css" | "xpath";
15
16 /**
17 * The selector string
18 */
19 value: string;
20}
21
22/**
23 * Default options that apply to all expectations and can be set globally
24 */
25interface ExpectDefaultOptions {
26 /**
27 * An interval at which the pageFunction is executed. Defaults to "raf".
28 */
29 polling?: ExpectPolling | undefined;
30
31 /**
32 * Maximum time to wait for in milliseconds. Defaults to 500.
33 */
34 timeout?: number | undefined;
35}
36
37/**
38 * Configures how to poll for an element.
39 */
40interface ExpectTimingActions extends ExpectDefaultOptions {
41 /**
42 * delay to pass to the puppeteer element.type API
43 */
44 delay?: number | undefined;
45}
46
47interface ExpectToClickOptions extends ExpectTimingActions {
48 /**
49 * Defaults to left.
50 */
51 button?: "left" | "right" | "middle" | undefined;
52
53 /**
54 * defaults to 1. See UIEvent.detail.
55 */
56 clickCount?: number | undefined;
57
58 /**
59 * Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.
60 */
61 delay?: number | undefined;
62
63 /**
64 * A text or a RegExp to match in element textContent.
65 */
66 text?: string | RegExp | undefined;
67
68 /**
69 * wait for element to be present in DOM and to be visible, i.e. to not have display: none or visibility: hidden CSS properties. Defaults to false.
70 */
71 visible?: boolean | undefined;
72}
73
74interface ExpectPuppeteer {
75 // These must all match the ExpectPuppeteer interface above.
76 // We can't extend from it directly because some method names conflict in type-incompatible ways.
77 toClick(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<void>;
78 toDisplayDialog(block: () => Promise<void>): Promise<Dialog>;
79 toFill(selector: string | MatchSelector, value: string, options?: ExpectTimingActions): Promise<void>;
80 toFillForm(
81 selector: string | MatchSelector,
82 value: { [key: string]: any },
83 options?: ExpectTimingActions,
84 ): Promise<void>;
85 toMatch(selector: string | MatchSelector, options?: ExpectTimingActions): Promise<void>;
86 toMatchElement(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<void>;
87 toSelect(selector: string | MatchSelector, valueOrText: string, options?: ExpectTimingActions): Promise<void>;
88 toUploadFile(selector: string | MatchSelector, filePath: string, options?: ExpectTimingActions): Promise<void>;
89}
90
91declare global {
92 namespace jest {
93 interface Matchers<R, T> {
94 // These must all match the ExpectPuppeteer interface above.
95 // We can't extend from it directly because some method names conflict in type-incompatible ways.
96 toClick(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<void>;
97 toDisplayDialog(block: () => Promise<void>): Promise<Dialog>;
98 toFill(selector: string | MatchSelector, value: string, options?: ExpectTimingActions): Promise<void>;
99 toFillForm(
100 selector: string | MatchSelector,
101 value: { [key: string]: any },
102 options?: ExpectTimingActions,
103 ): Promise<void>;
104 toMatch(matcher: string | RegExp, options?: ExpectTimingActions): Promise<void>;
105 toMatchElement(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<ElementHandle>;
106 toSelect(
107 selector: string | MatchSelector,
108 valueOrText: string,
109 options?: ExpectTimingActions,
110 ): Promise<void>;
111 toUploadFile(
112 selector: string | MatchSelector,
113 filePath: string,
114 options?: ExpectTimingActions,
115 ): Promise<void>;
116 }
117 }
118}
119
120declare function expectPuppeteer(instance: ElementHandle | Page): ExpectPuppeteer;
121
122declare namespace expectPuppeteer {
123 function setDefaultOptions(options: ExpectDefaultOptions): void;
124 function getDefaultOptions(): ExpectDefaultOptions;
125}
126
127export = expectPuppeteer;