-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test(feedback): Write some test for feedback utils and error returns #12519
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
For reference, the fully built event looks something like this: | ||
|
||
```json | ||
{ | ||
"type": "feedback", | ||
"event_id": "d2132d31b39445f1938d7e21b6bf0ec4", | ||
"timestamp": 1597977777.6189718, | ||
"dist": "1.12", | ||
"platform": "javascript", | ||
"environment": "production", | ||
"release": 42, | ||
"tags": { "transaction": "/organizations/:orgId/performance/:eventSlug/" }, | ||
"sdk": { "name": "name", "version": "version" }, | ||
"user": { | ||
"id": "123", | ||
"username": "user", | ||
"email": "user@site.com", | ||
"ip_address": "192.168.11.12" | ||
}, | ||
"request": { | ||
"url": null, | ||
"headers": { | ||
"user-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15" | ||
} | ||
}, | ||
"contexts": { | ||
"feedback": { | ||
"message": "test message", | ||
"contact_email": "test@example.com", | ||
"type": "feedback" | ||
}, | ||
"trace": { | ||
"trace_id": "4C79F60C11214EB38604F4AE0781BFB2", | ||
"span_id": "FA90FDEAD5F74052", | ||
"type": "trace" | ||
}, | ||
"replay": { | ||
"replay_id": "e2d42047b1c5431c8cba85ee2a8ab25d" | ||
} | ||
} | ||
} | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/core'; | ||
import type { User } from '@sentry/types'; | ||
import { getUser } from './getUser'; | ||
|
||
const currentUser: User = { username: 'current' }; | ||
|
||
describe('getUser', () => { | ||
beforeEach(() => { | ||
getCurrentScope().clear(); | ||
getIsolationScope().clear(); | ||
getGlobalScope().clear(); | ||
}); | ||
|
||
it('should prefer the user from the current scope', () => { | ||
getCurrentScope().setUser(currentUser); | ||
|
||
expect(getUser()).toEqual(expect.objectContaining(currentUser)); | ||
}); | ||
|
||
it('should return the empty user if no explicit user is set', () => { | ||
getCurrentScope().setUser(null); | ||
|
||
expect(getUser()).toEqual({ email: undefined, id: undefined, ip_address: undefined, username: undefined }); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/core'; | ||
import type { User } from '@sentry/types'; | ||
|
||
export function getUser(): User | undefined { | ||
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. Wonder if we do anything similar in other integrations |
||
const currentUser = getCurrentScope().getUser(); | ||
if (currentUser && Object.keys(currentUser).length) { | ||
return currentUser; | ||
} | ||
const isolationUser = getIsolationScope().getUser(); | ||
if (isolationUser && Object.keys(isolationUser).length) { | ||
return isolationUser; | ||
} | ||
const globalUser = getGlobalScope().getUser(); | ||
return globalUser; | ||
Comment on lines
+9
to
+14
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. testing these two scopes was hard to do directly :( |
||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getMissingFields } from './validate'; | ||
|
||
const BASE_FEEDBACK = { | ||
message: '', | ||
name: '', | ||
email: '', | ||
attachments: undefined, | ||
}; | ||
const BASE_OPTIONS = { | ||
emailLabel: 'Email', | ||
isEmailRequired: false, | ||
isNameRequired: false, | ||
messageLabel: 'Message', | ||
nameLabel: 'Name', | ||
}; | ||
|
||
describe('validate', () => { | ||
it('should be invalid when message is falsey', () => { | ||
const result = getMissingFields(BASE_FEEDBACK, BASE_OPTIONS); | ||
|
||
expect(result).toEqual(['Message']); | ||
}); | ||
|
||
it('should be valid when message is filled out', () => { | ||
const result = getMissingFields({ ...BASE_FEEDBACK, message: 'Hello world' }, BASE_OPTIONS); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should be invalid when name & email are required', () => { | ||
const result = getMissingFields(BASE_FEEDBACK, { ...BASE_OPTIONS, isNameRequired: true, isEmailRequired: true }); | ||
|
||
expect(result).toEqual(['Name', 'Email', 'Message']); | ||
}); | ||
|
||
it('should be valid when name & email are required and not falsey', () => { | ||
const result = getMissingFields( | ||
{ message: 'Hello world', name: 'Alice', email: 'alice@example.com', attachments: undefined }, | ||
{ ...BASE_OPTIONS, isNameRequired: true, isEmailRequired: true }, | ||
); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
}); |
Oops, something went wrong.
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.
getUser()
surprisingly has the return typeUser | undefined
but when nothing is set we're getting a default empty user.