Skip to content

Commit 565f54d

Browse files
committed
improve tests
1 parent d37b462 commit 565f54d

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

packages/browser-utils/test/instrument/metrics/inpt.test.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,86 @@ describe('_trackINP', () => {
2323
});
2424

2525
describe('_onInp', () => {
26-
const startStandaloneWebVitalSpan = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
27-
28-
beforeEach(() => {
29-
vi.clearAllMocks();
30-
});
31-
3226
it('early-returns if the INP metric entry has no value', () => {
27+
const startStandaloneWebVitalSpanSpy = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
28+
3329
const metric = {
3430
value: undefined,
3531
entries: [],
3632
};
3733
// @ts-expect-error - incomplete metric object
3834
_onInp({ metric });
39-
expect(startStandaloneWebVitalSpan).not.toHaveBeenCalled();
35+
36+
expect(startStandaloneWebVitalSpanSpy).not.toHaveBeenCalled();
4037
});
4138

4239
it('early-returns if the INP metric value is greater than 60 seconds', () => {
40+
const startStandaloneWebVitalSpanSpy = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
41+
4342
const metric = {
4443
value: 60_001,
45-
entries: [{ name: 'click', duration: 60_001, interactionId: 1 }],
44+
entries: [
45+
{ name: 'click', duration: 60_001, interactionId: 1 },
46+
{ name: 'click', duration: 60_000, interactionId: 2 },
47+
],
4648
};
4749
// @ts-expect-error - incomplete metric object
4850
_onInp({ metric });
49-
expect(startStandaloneWebVitalSpan).not.toHaveBeenCalled();
51+
52+
expect(startStandaloneWebVitalSpanSpy).not.toHaveBeenCalled();
5053
});
5154

5255
it('early-returns if the inp metric has an unknown interaction type', () => {
56+
const startStandaloneWebVitalSpanSpy = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
57+
5358
const metric = {
5459
value: 10,
5560
entries: [{ name: 'unknown', duration: 10, interactionId: 1 }],
5661
};
5762
// @ts-expect-error - incomplete metric object
5863
_onInp({ metric });
59-
expect(startStandaloneWebVitalSpan).not.toHaveBeenCalled();
64+
65+
expect(startStandaloneWebVitalSpanSpy).not.toHaveBeenCalled();
6066
});
6167

6268
it('starts a span for a valid INP metric entry', () => {
69+
const startStandaloneWebVitalSpanSpy = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
70+
6371
const metric = {
6472
value: 10,
6573
entries: [{ name: 'click', duration: 10, interactionId: 1 }],
6674
};
6775
// @ts-expect-error - incomplete metric object
6876
_onInp({ metric });
69-
expect(startStandaloneWebVitalSpan).toHaveBeenCalledWith({
77+
78+
expect(startStandaloneWebVitalSpanSpy).toHaveBeenCalledTimes(1);
79+
expect(startStandaloneWebVitalSpanSpy).toHaveBeenCalledWith({
80+
attributes: {
81+
'sentry.exclusive_time': 10,
82+
'sentry.op': 'ui.interaction.click',
83+
'sentry.origin': 'auto.http.browser.inp',
84+
},
85+
name: '<unknown>',
86+
startTime: NaN,
87+
transaction: undefined,
88+
});
89+
});
90+
91+
it('takes the correct entry based on the metric value', () => {
92+
const startStandaloneWebVitalSpanSpy = vi.spyOn(utils, 'startStandaloneWebVitalSpan');
93+
94+
const metric = {
95+
value: 10,
96+
entries: [
97+
{ name: 'click', duration: 10, interactionId: 1 },
98+
{ name: 'click', duration: 9, interactionId: 2 },
99+
],
100+
};
101+
// @ts-expect-error - incomplete metric object
102+
_onInp({ metric });
103+
104+
expect(startStandaloneWebVitalSpanSpy).toHaveBeenCalledTimes(1);
105+
expect(startStandaloneWebVitalSpanSpy).toHaveBeenCalledWith({
70106
attributes: {
71107
'sentry.exclusive_time': 10,
72108
'sentry.op': 'ui.interaction.click',

0 commit comments

Comments
 (0)