UNPKG

4.46 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 { ElementHandle, Page, Dialog } 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(selector: string | MatchSelector, value: { [key: string]: any}, options?: ExpectTimingActions): Promise<void>;
88 toMatch(selector: string | MatchSelector, options?: ExpectTimingActions): Promise<void>;
89 toMatchElement(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<void>;
90 toSelect(selector: string | MatchSelector, valueOrText: string, options?: ExpectTimingActions): Promise<void>;
91 toUploadFile(selector: string | MatchSelector, filePath: string, options?: ExpectTimingActions): Promise<void>;
92}
93
94declare global {
95 namespace jest {
96 interface Matchers<R, T> {
97 // These must all match the ExpectPuppeteer interface above.
98 // We can't extend from it directly because some method names conflict in type-incompatible ways.
99 toClick(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<void>;
100 toDisplayDialog(block: () => Promise<void>): Promise<Dialog>;
101 toFill(selector: string | MatchSelector, value: string, options?: ExpectTimingActions): Promise<void>;
102 toFillForm(selector: string | MatchSelector, value: { [key: string]: any}, options?: ExpectTimingActions): Promise<void>;
103 toMatch(matcher: string | RegExp, options?: ExpectTimingActions): Promise<void>;
104 toMatchElement(selector: string | MatchSelector, options?: ExpectToClickOptions): Promise<ElementHandle>;
105 toSelect(selector: string | MatchSelector, valueOrText: string, options?: ExpectTimingActions): Promise<void>;
106 toUploadFile(selector: string | MatchSelector, filePath: string, options?: ExpectTimingActions): Promise<void>;
107 }
108 }
109}
110
111declare function expectPuppeteer(instance: ElementHandle | Page): ExpectPuppeteer;
112
113declare namespace expectPuppeteer {
114 function setDefaultOptions(options: ExpectDefaultOptions): void;
115 function getDefaultOptions(): ExpectDefaultOptions;
116}
117
118export = expectPuppeteer;