Skip to content

Commit a37d5fc

Browse files
authored
add network signal test (#1109)
1 parent 232708f commit a37d5fc

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

packages/signals/signals-integration-tests/playwright.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import path from 'path'
66
* See https://playwright.dev/docs/test-configuration.
77
*/
88
const config: PlaywrightTestConfig = {
9+
webServer: {
10+
command: 'yarn run server',
11+
url: 'http://127.0.0.1:3000',
12+
reuseExistingServer: !process.env.CI,
13+
},
914
testDir: './src',
1015
globalSetup: path.resolve(__dirname, 'playwright.global-setup.ts'),
1116
/* Maximum time one test can run for. */

packages/signals/signals-integration-tests/src/helpers/base-page-object.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export class BasePage {
99
public edgeFnDownloadURL = 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js'
1010
public edgeFn: string
1111

12-
constructor(url: string, edgeFn: string) {
12+
constructor(path: string, edgeFn: string) {
1313
this.edgeFn = edgeFn
14-
this.url = url
14+
this.url = 'http://localhost:3000/src/tests' + path
1515
}
1616

1717
/**
@@ -29,10 +29,14 @@ export class BasePage {
2929
}
3030

3131
private async setupMockedRoutes() {
32+
// clear any existing saved requests
33+
this.signalsApiReq = undefined as any as Request
34+
this.trackingApiReq = undefined as any as Request
35+
3236
await Promise.all([
3337
this.mockSignalsApi(),
38+
this.mockTrackingApi(),
3439
this.mockCDNSettings(),
35-
await this.mockTrackingApi(),
3640
])
3741
}
3842

packages/signals/signals-integration-tests/src/tests/signals-vanilla/basic.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ test.beforeEach(async ({ page }) => {
88
await indexPage.load(page)
99
})
1010

11+
test('network signals', async () => {
12+
/**
13+
* Make a fetch call, see if it gets sent to the signals endpoint
14+
*/
15+
await indexPage.mockRandomJSONApi()
16+
await indexPage.makeFetchCallToRandomJSONApi()
17+
await indexPage.waitForSignalsApiFlush()
18+
const batch = indexPage.signalsApiReq.postDataJSON().batch as SegmentEvent[]
19+
const networkEvents = batch.filter(
20+
(el: SegmentEvent) => el.properties!.type === 'network'
21+
)
22+
const requests = networkEvents.filter(
23+
(el) => el.properties!.data.action === 'Request'
24+
)
25+
expect(requests).toHaveLength(1)
26+
expect(requests[0].properties!.data.data).toEqual({ foo: 'bar' })
27+
28+
const responses = networkEvents.filter(
29+
(el) => el.properties!.data.action === 'Response'
30+
)
31+
expect(responses).toHaveLength(1)
32+
expect(responses[0].properties!.data.data).toEqual({ someResponse: 'yep' })
33+
})
34+
1135
test('instrumentation signals', async () => {
1236
/**
1337
* Make an analytics.page() call, see if it gets sent to the signals endpoint

packages/signals/signals-integration-tests/src/tests/signals-vanilla/index-page.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { BasePage } from '../../helpers/base-page-object'
2-
import path from 'path'
32
import { promiseTimeout } from '@internal/test-helpers'
43

54
const edgeFn = `
@@ -13,13 +12,40 @@ const edgeFn = `
1312

1413
export class IndexPage extends BasePage {
1514
constructor() {
16-
super(`file://${path.resolve(__dirname, 'index.html')}`, edgeFn)
15+
super(`/signals-vanilla/index.html`, edgeFn)
1716
}
17+
1818
async makeAnalyticsPageCall(): Promise<unknown> {
1919
const p = this.page.evaluate(() => {
2020
void window.analytics.page()
2121
return new Promise((resolve) => window.analytics.on('page', resolve))
2222
})
2323
return promiseTimeout(p, 2000, 'analytics.on("page") did not resolve')
2424
}
25+
26+
async mockRandomJSONApi() {
27+
await this.page.route('http://localhost:3000/api/foo', (route) => {
28+
return route.fulfill({
29+
contentType: 'application/json',
30+
status: 200,
31+
body: JSON.stringify({
32+
someResponse: 'yep',
33+
}),
34+
})
35+
})
36+
}
37+
38+
async makeFetchCallToRandomJSONApi(): Promise<void> {
39+
return this.page.evaluate(() => {
40+
return fetch('http://localhost:3000/api/foo', {
41+
method: 'POST',
42+
headers: {
43+
'Content-Type': 'application/json',
44+
},
45+
body: JSON.stringify({ foo: 'bar' }),
46+
})
47+
.then(console.log)
48+
.catch(console.error)
49+
})
50+
}
2551
}

0 commit comments

Comments
 (0)