File tree Expand file tree Collapse file tree 4 files changed +64
-5
lines changed
packages/signals/signals-integration-tests Expand file tree Collapse file tree 4 files changed +64
-5
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,11 @@ import path from 'path'
6
6
* See https://playwright.dev/docs/test-configuration.
7
7
*/
8
8
const config : PlaywrightTestConfig = {
9
+ webServer : {
10
+ command : 'yarn run server' ,
11
+ url : 'http://127.0.0.1:3000' ,
12
+ reuseExistingServer : ! process . env . CI ,
13
+ } ,
9
14
testDir : './src' ,
10
15
globalSetup : path . resolve ( __dirname , 'playwright.global-setup.ts' ) ,
11
16
/* Maximum time one test can run for. */
Original file line number Diff line number Diff line change @@ -9,9 +9,9 @@ export class BasePage {
9
9
public edgeFnDownloadURL = 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js'
10
10
public edgeFn : string
11
11
12
- constructor ( url : string , edgeFn : string ) {
12
+ constructor ( path : string , edgeFn : string ) {
13
13
this . edgeFn = edgeFn
14
- this . url = url
14
+ this . url = 'http://localhost:3000/src/tests' + path
15
15
}
16
16
17
17
/**
@@ -29,10 +29,14 @@ export class BasePage {
29
29
}
30
30
31
31
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
+
32
36
await Promise . all ( [
33
37
this . mockSignalsApi ( ) ,
38
+ this . mockTrackingApi ( ) ,
34
39
this . mockCDNSettings ( ) ,
35
- await this . mockTrackingApi ( ) ,
36
40
] )
37
41
}
38
42
Original file line number Diff line number Diff line change @@ -8,6 +8,30 @@ test.beforeEach(async ({ page }) => {
8
8
await indexPage . load ( page )
9
9
} )
10
10
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
+
11
35
test ( 'instrumentation signals' , async ( ) => {
12
36
/**
13
37
* Make an analytics.page() call, see if it gets sent to the signals endpoint
Original file line number Diff line number Diff line change 1
1
import { BasePage } from '../../helpers/base-page-object'
2
- import path from 'path'
3
2
import { promiseTimeout } from '@internal/test-helpers'
4
3
5
4
const edgeFn = `
@@ -13,13 +12,40 @@ const edgeFn = `
13
12
14
13
export class IndexPage extends BasePage {
15
14
constructor ( ) {
16
- super ( `file:// ${ path . resolve ( __dirname , ' index.html' ) } ` , edgeFn )
15
+ super ( `/signals-vanilla/ index.html` , edgeFn )
17
16
}
17
+
18
18
async makeAnalyticsPageCall ( ) : Promise < unknown > {
19
19
const p = this . page . evaluate ( ( ) => {
20
20
void window . analytics . page ( )
21
21
return new Promise ( ( resolve ) => window . analytics . on ( 'page' , resolve ) )
22
22
} )
23
23
return promiseTimeout ( p , 2000 , 'analytics.on("page") did not resolve' )
24
24
}
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
+ }
25
51
}
You can’t perform that action at this time.
0 commit comments