File size: 6,652 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Locator, Page } from 'playwright';
import { TracksEvent, TracksEventProperties } from './types';

// Exporting just for unit testing
export const createEventMatchingPredicate = (
	expectedName: string,
	matchingProperties?: TracksEventProperties
) => {
	return ( event: TracksEvent ) => {
		const [ name, properties ] = event;
		if ( expectedName !== name ) {
			return false;
		}

		if ( ! matchingProperties ) {
			return true; // Nothing more to check, we're done here!
		}

		for ( const expectedProperty in matchingProperties ) {
			if ( properties[ expectedProperty ] !== matchingProperties[ expectedProperty ] ) {
				return false;
			}
		}

		return true;
	};
};

const sleep = async ( ms: number ) => new Promise( ( resolve ) => setTimeout( resolve, ms ) );

interface EventSearchOptions {
	/**
	 * Any properties that must be present and matching for the event to match.
	 */
	matchingProperties?: TracksEventProperties;

	/**
	 * The number of milliseconds to wait to see if the Tracks event fires. Useful if the events are debounced.
	 */
	waitForEventMs?: number;
}

/**
 * A class wrapping the editor Tracks events monitored in the browser
 */
export class EditorTracksEventManager {
	private page: Page;

	/**
	 * Construct an instance of the editor Tracks event manager
	 *
	 * @param page The Playwright page
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * A locator within the correct iframe for the editor, to correctly grab values off of window.
	 */
	private get editor(): Locator {
		// The Tracks events are stashed on "window" in whatever iframe is the actual editor (whether post, page, or site).
		// We need to consolidate down to a single locator that works across all editors, and handles if we have the Gutenframe wrapper.
		// It just matters that we're in the right iframe, so we use a very generic selector ("body") to get something safe and top-level.
		return this.page.url().includes( '/wp-admin' )
			? this.page.locator( 'body' ) // No Gutenframe
			: this.page
					.frameLocator(
						'[src*="wp-admin/post"],[src*="wp-admin/themes"],[src*="wp-admin/site-editor"]'
					) // Post, page, and site editor!
					.locator( 'body' ); // Gutenframe
	}

	/**
	 * Get the full stack (reverse chronological) of Tracks events that is tracked for E2E tests.
	 *
	 * @throws If we can't find the events. Empty but instantiated event stacks are still valid.
	 */
	async getAllEvents(): Promise< TracksEvent[] > {
		const eventStack = await this.editor.evaluate( () => window._e2eEventsStack );

		if ( ! eventStack ) {
			throw new Error(
				'Could not find the Tracks events. Expected "_e2eEventsStack" property on the editor window object.'
			);
		}

		return eventStack;
	}

	/**
	 * Clears the E2E Tracks events in the browser.
	 */
	async clearEvents(): Promise< void > {
		// We need to make sure that we're accurately finding the event stack first.
		// Otherwise, resetting it could cause weird artifacts later!
		await this.getAllEvents();
		await this.editor.evaluate( () => {
			window._e2eEventsStack = [];
		} );
	}

	/**
	 * Checks if a given Tracks event fired by matching on event name and optionally event properties
	 *
	 * @param {string} name The name of the event we are searching for.
	 * @param {EventSearchOptions} options Options to control searching for the event.
	 * @returns True/false based on whether a matching event was found.
	 */
	async didEventFire( name: string, options?: EventSearchOptions ): Promise< boolean > {
		const isEventOnStack = async () => {
			const eventStack = await this.getAllEvents();
			return eventStack.some( createEventMatchingPredicate( name, options?.matchingProperties ) );
		};
		const stopSearching = ( result: boolean ) => result;
		const maxMstoWait = options?.waitForEventMs || 0;

		const eventIsOnStack = await this.searchWithRetry( isEventOnStack, stopSearching, maxMstoWait );
		return eventIsOnStack;
	}

	/**
	 * Get the most Tracks event that matches the name and options provided.
	 *
	 * @param {string} name The name of the event we are searching for.
	 * @param {EventSearchOptions} options Options to control searching for the event.
	 * @returns The most recent matching Tracks event.
	 * @throws If no matching Tracks event was found.
	 */
	async getMostRecentMatchingEvent(
		name: string,
		options?: EventSearchOptions
	): Promise< TracksEvent > {
		const findMostRecentEventOnStack = async () => {
			const eventStack = await this.getAllEvents();
			return eventStack.find( createEventMatchingPredicate( name, options?.matchingProperties ) );
		};
		const stopSearching = ( result: TracksEvent | undefined ) => !! result;
		const maxMstoWait = options?.waitForEventMs || 0;

		const mostRecentEvent = await this.searchWithRetry(
			findMostRecentEventOnStack,
			stopSearching,
			maxMstoWait
		);

		if ( ! mostRecentEvent ) {
			throw new Error(
				`No Tracks event was found with provided name (${ name }) and matching properties.`
			);
		}

		return mostRecentEvent;
	}

	/**
	 * A shared private helper method for retrying an event search on a loop.
	 *
	 * Because Tracks events can be debounced, sometimes we'll need to wait and retry while searching.
	 *
	 * This supports generic typing because we could be looking for various things.
	 * That means our result return type could vary: boolean, a single event, an array of events, etc.
	 *
	 * Because our event searching function could return a variety of types,
	 * we need some way to translate those varied types to a boolean.
	 * It's safer to be explicit rather than force the type conversion.
	 * For example, an empty array is technically "truthy"!
	 *
	 * @param {Function} searchForEvent Function to do the event searching.
	 * @param {Function} stopSearching Function to determine when to stop searching, based on the result of the search.
	 * @param {number} maxMsToWait Max number of milliseconds to keep retrying.
	 * @returns The result of the searchForEvent function after necessary retries.
	 */
	private async searchWithRetry< SearchResult >(
		searchForEvent: () => Promise< SearchResult >,
		stopSearching: ( result: SearchResult ) => boolean,
		maxMsToWait: number
	): Promise< SearchResult > {
		const WAIT_INTERVAL_MS = 200;
		let totalWaitedMs = 0;

		// By doing an initial check and starting the loop with the wait,
		// we avoid doing an extra, unnecessary wait at the end.
		let result = await searchForEvent();
		while ( ! stopSearching( result ) && totalWaitedMs < maxMsToWait ) {
			await sleep( WAIT_INTERVAL_MS );
			totalWaitedMs += WAIT_INTERVAL_MS;
			result = await searchForEvent();
		}

		return result;
	}
}