-
Notifications
You must be signed in to change notification settings - Fork 538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(instrumentation-user-interaction): support clicks in React apps #537
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
69a1c7b
fix(instrumentation-user-interaction): support clicks in React apps
kkruk-sumo 6209b38
Merge branch 'main' into support_react_clicks
kkruk-sumo 7ee1d07
chore: code maintenance
kkruk-sumo b14db18
Merge branch 'main' into support_react_clicks
kkruk-sumo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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 |
---|---|---|
|
@@ -40,6 +40,13 @@ const FILE_URL = | |
'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json'; | ||
|
||
describe('UserInteractionInstrumentation', () => { | ||
afterEach(() => { | ||
// clear body from elements created by some tests to make sure they are independent | ||
while (document.body.lastChild) { | ||
document.body.removeChild(document.body.lastChild); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this is necessary? Perhaps a comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added. |
||
} | ||
}); | ||
|
||
describe('when zone.js is available', () => { | ||
let contextManager: ZoneContextManager; | ||
let userInteractionInstrumentation: UserInteractionInstrumentation; | ||
|
@@ -313,6 +320,122 @@ describe('UserInteractionInstrumentation', () => { | |
}); | ||
}); | ||
|
||
it('should handle interactions listened on document - react < 17', done => { | ||
const btn1 = document.createElement('button'); | ||
btn1.setAttribute('id', 'btn1'); | ||
document.body.appendChild(btn1); | ||
const btn2 = document.createElement('button'); | ||
btn2.setAttribute('id', 'btn2'); | ||
document.body.appendChild(btn2); | ||
|
||
const listener = (event: MouseEvent) => { | ||
switch (event.target) { | ||
case btn1: | ||
getData(FILE_URL, () => { | ||
sandbox.clock.tick(10); | ||
}).then(() => {}); | ||
break; | ||
case btn2: | ||
getData(FILE_URL, () => { | ||
sandbox.clock.tick(10); | ||
}).then(() => {}); | ||
break; | ||
} | ||
}; | ||
|
||
document.addEventListener('click', listener); | ||
|
||
try { | ||
btn1.click(); | ||
btn2.click(); | ||
} finally { | ||
// remove added listener so we don't pollute other tests | ||
document.removeEventListener('click', listener); | ||
} | ||
|
||
sandbox.clock.tick(1000); | ||
originalSetTimeout(() => { | ||
assert.equal(exportSpy.args.length, 4, 'should export 4 spans'); | ||
|
||
const span1: tracing.ReadableSpan = exportSpy.args[0][0][0]; | ||
const span2: tracing.ReadableSpan = exportSpy.args[1][0][0]; | ||
const span3: tracing.ReadableSpan = exportSpy.args[2][0][0]; | ||
const span4: tracing.ReadableSpan = exportSpy.args[3][0][0]; | ||
|
||
assertClickSpan(span1, 'btn1'); | ||
assertClickSpan(span2, 'btn2'); | ||
|
||
assert.strictEqual( | ||
span1.spanContext().spanId, | ||
span3.parentSpanId, | ||
'span3 has wrong parent' | ||
); | ||
assert.strictEqual( | ||
span2.spanContext().spanId, | ||
span4.parentSpanId, | ||
'span4 has wrong parent' | ||
); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should handle interactions listened on a parent element (bubbled events) - react >= 17', done => { | ||
const root = document.createElement('div'); | ||
document.body.appendChild(root); | ||
|
||
const btn1 = document.createElement('button'); | ||
btn1.setAttribute('id', 'btn1'); | ||
root.appendChild(btn1); | ||
const btn2 = document.createElement('button'); | ||
btn2.setAttribute('id', 'btn2'); | ||
root.appendChild(btn2); | ||
|
||
root.addEventListener('click', event => { | ||
switch (event.target) { | ||
case btn1: | ||
getData(FILE_URL, () => { | ||
sandbox.clock.tick(10); | ||
}).then(() => {}); | ||
break; | ||
case btn2: | ||
getData(FILE_URL, () => { | ||
sandbox.clock.tick(10); | ||
}).then(() => {}); | ||
break; | ||
} | ||
}); | ||
|
||
btn1.click(); | ||
btn2.click(); | ||
|
||
sandbox.clock.tick(1000); | ||
originalSetTimeout(() => { | ||
assert.equal(exportSpy.args.length, 4, 'should export 4 spans'); | ||
|
||
const span1: tracing.ReadableSpan = exportSpy.args[0][0][0]; | ||
const span2: tracing.ReadableSpan = exportSpy.args[1][0][0]; | ||
const span3: tracing.ReadableSpan = exportSpy.args[2][0][0]; | ||
const span4: tracing.ReadableSpan = exportSpy.args[3][0][0]; | ||
|
||
assertClickSpan(span1, 'btn1'); | ||
assertClickSpan(span2, 'btn2'); | ||
|
||
assert.strictEqual( | ||
span1.spanContext().spanId, | ||
span3.parentSpanId, | ||
'span3 has wrong parent' | ||
); | ||
assert.strictEqual( | ||
span2.spanContext().spanId, | ||
span4.parentSpanId, | ||
'span4 has wrong parent' | ||
); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should handle unpatch', () => { | ||
const _window: WindowWithZone = window as unknown as WindowWithZone; | ||
const ZoneWithPrototype = _window.Zone; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate this test (having two targets and ensuring that the span names properly line up)!