11import { test , expect } from "@playwright/test" ;
22import { BeaconPayload } from "../../../src/beacon" ;
33import { entryTypeSupported } from "../../helpers/browsers" ;
4- import { getNavigationTimingMs } from "../../helpers/lux" ;
4+ import { getElapsedMs , getNavigationTimingMs } from "../../helpers/lux" ;
55import RequestInterceptor from "../../request-interceptor" ;
66
77test . describe ( "POST beacon LCP" , ( ) => {
@@ -22,7 +22,7 @@ test.describe("POST beacon LCP", () => {
2222
2323 test ( "LCP is reset between SPA page transitions" , async ( { page } ) => {
2424 const luxRequests = new RequestInterceptor ( page ) . createRequestMatcher ( "/store/" ) ;
25- await page . goto ( "/images .html?injectScript=LUX.auto=false;" , { waitUntil : "networkidle" } ) ;
25+ await page . goto ( "/default .html?injectScript=LUX.auto=false;" , { waitUntil : "networkidle" } ) ;
2626 await luxRequests . waitForMatchingRequest ( ( ) => page . evaluate ( ( ) => LUX . send ( ) ) ) ;
2727
2828 let b = luxRequests . get ( 0 ) ! . postDataJSON ( ) as BeaconPayload ;
@@ -35,11 +35,34 @@ test.describe("POST beacon LCP", () => {
3535 expect ( b . lcp ) . toBeUndefined ( ) ;
3636 }
3737
38+ // SPA page with no LCP events
3839 await page . evaluate ( ( ) => LUX . init ( ) ) ;
3940 await page . waitForTimeout ( 200 ) ;
4041 await luxRequests . waitForMatchingRequest ( ( ) => page . evaluate ( ( ) => LUX . send ( ) ) ) ;
4142
4243 b = luxRequests . get ( 1 ) ! . postDataJSON ( ) as BeaconPayload ;
4344 expect ( b . lcp ) . toBeUndefined ( ) ;
45+
46+ // SPA page with a new LCP event
47+ const beforeInit = await getElapsedMs ( page ) ;
48+ await page . evaluate ( ( ) => LUX . init ( ) ) ;
49+ const beforeInsert = await getElapsedMs ( page ) ;
50+ await page . evaluate ( ( ) => {
51+ const eve = document . createElement ( "img" ) ;
52+ eve . src = "/eve.jpg" ;
53+ eve . className = "new-lcp-image" ;
54+ eve . style . width = "500px" ;
55+ document . body . prepend ( eve ) ;
56+ } ) ;
57+ await page . waitForTimeout ( 50 ) ;
58+ const beforeBeacon = await getElapsedMs ( page ) ;
59+ await luxRequests . waitForMatchingRequest ( ( ) => page . evaluate ( ( ) => LUX . send ( ) ) ) ;
60+
61+ const insertTime = beforeInsert - beforeInit ;
62+ const beaconTime = beforeBeacon - beforeInit ;
63+
64+ b = luxRequests . get ( 2 ) ! . postDataJSON ( ) as BeaconPayload ;
65+ expect ( b . lcp ! . value ) . toBeBetween ( insertTime , beaconTime ) ;
66+ expect ( b . lcp ! . attribution ! . elementSelector ) . toEqual ( "html>body>img.new-lcp-image" ) ;
4467 } ) ;
4568} ) ;
0 commit comments