-
Notifications
You must be signed in to change notification settings - Fork 6k
feat: add more tests for browser/pages/vscode #3743
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
Conversation
3bd53d0
to
bac4716
Compare
This refactors some logic in src/browser/pages/vscode.ts related to setting the background color of the body to the editor background theme color.
bac4716
to
66dc4cc
Compare
Codecov Report
@@ Coverage Diff @@
## main #3743 +/- ##
==========================================
+ Coverage 61.55% 62.01% +0.46%
==========================================
Files 35 35
Lines 1813 1835 +22
Branches 365 370 +5
==========================================
+ Hits 1116 1138 +22
Misses 588 588
Partials 109 109
Continue to review full report at Codecov.
|
@@ -95,8 +95,64 @@ try { | |||
console.error(error) | |||
} | |||
|
|||
export function setBodyBackgroundToThemeBackgroundColor(document: Document, localStorage: Storage) { |
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.
At first, I didn't like the function signature — passing in document
and localStorage
— but then I justified it because:
- this function is only called in one place
- it makes it a lot easier to test when
document
orlocalStorage
is missing
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.
makes sense to me, this also allows you to test this by mocking document/localStorage, and avoids global state
) | ||
} | ||
|
||
document.body.style.background = editorBgColor |
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.
Here we assume that body
is part of the document
but I think it would be really rare/unusual if this weren't the case. I think we can leave as is and if we run into the rare issue, I can modify down the road.
} catch (error) { | ||
// Oh well. | ||
console.error("Something went wrong setting the body background to the theme background color.") | ||
console.error(error) |
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.
setBodyBackgroundToThemeBackgroundColor
throws specific errors, which will be caught by our catch
block and logged to the console in the browser.
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.
LGTM!
@@ -95,8 +95,64 @@ try { | |||
console.error(error) | |||
} | |||
|
|||
export function setBodyBackgroundToThemeBackgroundColor(document: Document, localStorage: Storage) { |
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.
makes sense to me, this also allows you to test this by mocking document/localStorage, and avoids global state
// We need to set the url in the JSDOM constructor | ||
// to prevent this error "SecurityError: localStorage is not available for opaque origins" | ||
// See: https://github.com/jsdom/jsdom/issues/2304#issuecomment-622314949 | ||
const { window } = new JSDOM("", { url: "http://localhost" }) |
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 guess this is because Node.js doesn't have window
?
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.
Exactly! window
is only available in browser environments because it's a browser global
This PR adds more tests for
browser/pages/vscode.ts
.Fixes N/A
Notes
The main purpose of this PR is to test this piece of code
This code grabs the
colorThemeData
fromlocalStorage
to get the editor background color based on the user's VSCode theme and then sets the background of thebody
to that color.