UNPKG

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