Skip to content

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

Merged
merged 1 commit into from
Jul 8, 2021

Conversation

jsjoeio
Copy link
Contributor

@jsjoeio jsjoeio commented Jul 7, 2021

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

document.body.style.background = JSON.parse(localStorage.getItem("colorThemeData")!).colorMap["editor.background"]

This code grabs the colorThemeData from localStorage to get the editor background color based on the user's VSCode theme and then sets the background of the body to that color.

@jsjoeio jsjoeio added the testing Anything related to testing label Jul 7, 2021
@jsjoeio jsjoeio added this to the 3.11.0 milestone Jul 7, 2021
@jsjoeio jsjoeio self-assigned this Jul 7, 2021
@jsjoeio jsjoeio force-pushed the jsjoeio-add-tests-vscode branch from 3bd53d0 to bac4716 Compare July 8, 2021 18:16
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.
@jsjoeio jsjoeio force-pushed the jsjoeio-add-tests-vscode branch from bac4716 to 66dc4cc Compare July 8, 2021 18:24
@codecov
Copy link

codecov bot commented Jul 8, 2021

Codecov Report

Merging #3743 (66dc4cc) into main (197bccc) will increase coverage by 0.46%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
src/browser/pages/vscode.ts 76.36% <100.00%> (+15.75%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 197bccc...66dc4cc. Read the comment docs.

@@ -95,8 +95,64 @@ try {
console.error(error)
}

export function setBodyBackgroundToThemeBackgroundColor(document: Document, localStorage: Storage) {
Copy link
Contributor Author

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 or localStorage is missing

Copy link
Contributor

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
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

@jsjoeio jsjoeio marked this pull request as ready for review July 8, 2021 18:27
@jsjoeio jsjoeio requested a review from a team as a code owner July 8, 2021 18:27
Copy link
Contributor

@jawnsy jawnsy left a 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) {
Copy link
Contributor

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" })
Copy link
Contributor

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?

Copy link
Contributor Author

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

@jsjoeio jsjoeio merged commit 15d256b into main Jul 8, 2021
@jsjoeio jsjoeio deleted the jsjoeio-add-tests-vscode branch July 8, 2021 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Anything related to testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants