-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update signal integration tests (#1154)
- Loading branch information
Showing
7 changed files
with
261 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
packages/signals/signals-integration-tests/src/helpers/network-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Page, Route, Request } from '@playwright/test' | ||
import { SegmentEvent } from '@segment/analytics-next' | ||
|
||
type FulfillOptions = Parameters<Route['fulfill']>['0'] | ||
export interface XHRRequestOptions { | ||
method?: string | ||
body?: any | ||
contentType?: string | ||
responseType?: XMLHttpRequestResponseType | ||
responseLatency?: number | ||
} | ||
export class PageNetworkUtils { | ||
private defaultTestApiURL = 'http://localhost:5432/api/foo' | ||
private defaultResponseTimeout = 3000 | ||
constructor(public page: Page) {} | ||
|
||
async makeXHRCall( | ||
url = this.defaultTestApiURL, | ||
reqOptions: XHRRequestOptions = {} | ||
): Promise<void> { | ||
let normalizeUrl = url | ||
if (url.startsWith('/')) { | ||
normalizeUrl = new URL(url, this.page.url()).href | ||
} | ||
const req = this.page.waitForResponse(normalizeUrl ?? url, { | ||
timeout: this.defaultResponseTimeout, | ||
}) | ||
await this.page.evaluate( | ||
(args) => { | ||
const xhr = new XMLHttpRequest() | ||
xhr.open(args.method ?? 'POST', args.url) | ||
xhr.responseType = args.responseType ?? 'json' | ||
xhr.setRequestHeader( | ||
'Content-Type', | ||
args.contentType ?? 'application/json' | ||
) | ||
if (typeof args.responseLatency === 'number') { | ||
xhr.setRequestHeader( | ||
'x-test-latency', | ||
args.responseLatency.toString() | ||
) | ||
} | ||
xhr.send(args.body || JSON.stringify({ foo: 'bar' })) | ||
}, | ||
{ url, ...reqOptions } | ||
) | ||
await req | ||
} | ||
/** | ||
* Make a fetch call in the page context. By default it will POST a JSON object with {foo: 'bar'} | ||
*/ | ||
async makeFetchCall( | ||
url = this.defaultTestApiURL, | ||
request: Partial<RequestInit> = {} | ||
): Promise<void> { | ||
let normalizeUrl = url | ||
if (url.startsWith('/')) { | ||
normalizeUrl = new URL(url, this.page.url()).href | ||
} | ||
const req = this.page.waitForResponse(normalizeUrl ?? url, { | ||
timeout: this.defaultResponseTimeout, | ||
}) | ||
await this.page.evaluate( | ||
(args) => { | ||
return fetch(args.url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ foo: 'bar' }), | ||
...args.request, | ||
}) | ||
.then(console.log) | ||
.catch(console.error) | ||
}, | ||
{ url, request } | ||
) | ||
await req | ||
} | ||
|
||
async mockTestRoute( | ||
url = this.defaultTestApiURL, | ||
response?: Partial<FulfillOptions> | ||
) { | ||
if (url.startsWith('/')) { | ||
url = new URL(url, this.page.url()).href | ||
} | ||
await this.page.route(url, async (route) => { | ||
const latency = this.extractLatency(route) | ||
|
||
// if a custom latency is set in the request headers, use that instead | ||
|
||
await new Promise((resolve) => setTimeout(resolve, latency)) | ||
return route.fulfill({ | ||
contentType: 'application/json', | ||
status: 200, | ||
body: JSON.stringify({ someResponse: 'yep' }), | ||
...response, | ||
}) | ||
}) | ||
} | ||
private extractLatency(route: Route) { | ||
let latency = 0 | ||
if (route.request().headers()['x-test-latency']) { | ||
const customLatency = parseInt( | ||
route.request().headers()['x-test-latency'] | ||
) | ||
if (customLatency) { | ||
latency = customLatency | ||
} | ||
} | ||
return latency | ||
} | ||
} | ||
|
||
class SegmentAPIRequestBuffer { | ||
private requests: Request[] = [] | ||
public lastEvent() { | ||
return this.getEvents()[this.getEvents.length - 1] | ||
} | ||
public getEvents(): SegmentEvent[] { | ||
return this.requests.flatMap((req) => req.postDataJSON().batch) | ||
} | ||
|
||
clear() { | ||
this.requests = [] | ||
} | ||
addRequest(request: Request) { | ||
if (request.method().toLowerCase() !== 'post') { | ||
throw new Error( | ||
`Unexpected method: ${request.method()}, Tracking API only accepts POST` | ||
) | ||
} | ||
this.requests.push(request) | ||
} | ||
} | ||
|
||
export class SignalAPIRequestBuffer extends SegmentAPIRequestBuffer { | ||
/** | ||
* @example 'network', 'interaction', 'navigation', etc | ||
*/ | ||
override getEvents(signalType?: string): SegmentEvent[] { | ||
if (signalType) { | ||
return this.getEvents().filter((e) => e.properties!.type === signalType) | ||
} | ||
return super.getEvents() | ||
} | ||
|
||
override lastEvent(signalType?: string | undefined): SegmentEvent { | ||
if (signalType) { | ||
const res = | ||
this.getEvents(signalType)[this.getEvents(signalType).length - 1] | ||
if (!res) { | ||
throw new Error(`No signal of type ${signalType} found`) | ||
} | ||
return res | ||
} | ||
return super.lastEvent() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.