Skip to content
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

#9227 Refactor useTheme to use useManagedStorageState #9228

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

mnholtz
Copy link
Collaborator

@mnholtz mnholtz commented Oct 3, 2024

What does this PR do?

Discussion

  • The useManagedStorageState hook throws on error, causing the unit test "uses activeTheme when error occurs in managed storage" to fail. Do we want to add error-handling to useManagedStorageState to preserve this behavior in the sidebar?

@mnholtz
Copy link
Collaborator Author

mnholtz commented Oct 3, 2024

Just calling out that I'd like clarification on

The useManagedStorageState hook throws on error, causing the unit test "uses activeTheme when error occurs in managed storage" to fail. Do we want to add error-handling to useManagedStorageState to preserve this behavior in the sidebar?

Base automatically changed from feature/9222_hide_sidebar_logo_policy to main October 3, 2024 20:17
useAsyncExternalStore(themeStorageSubscribe, themeStorage.get);

const { data: managedStorageState, isLoading: isManagedStorageLoading } =
useManagedStorageState();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use this as an opportunity to properly refactor useManagedStorageState to return AsyncState

Copy link
Contributor

@twschiller twschiller Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the following? I don't remember if there's a reason why we didn't use that other than that maybe the useAsyncExternalStore method didn't exist yet (I haven't checked) / or it just wasn't very valuable given that managed storage values should never change in practice:

  • useAsyncExternalStore
  • readManagedStorage

I think we then get error handling for free with those

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering the same thing, re why not use readManagedStorage + useAsyncExternalStore

Copy link
Contributor

@twschiller twschiller Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering the same thing, re why not use readManagedStorage + useAsyncExternalStore

I don't see a reason not to. The main difference will be it will be technically possible for the hook to return an error state. Whereas the current one could potentially stay isLoading indefinitely. But I don't think the difference matters in practice

@@ -57,7 +59,7 @@ beforeEach(() => {
useManagedStorageStateMock.mockReturnValue({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: use valueToAsyncState from asyncStateUtils

Comment on lines +114 to +117
jest.mocked(themeStorage.get).mockResolvedValue({
...customTheme,
showSidebarLogo: themeValue,
lastFetched: Date.now(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: save the time and use it to validate against:

Suggested change
jest.mocked(themeStorage.get).mockResolvedValue({
...customTheme,
showSidebarLogo: themeValue,
lastFetched: Date.now(),
const lastFetched = Date.now();
jest.mocked(themeStorage.get).mockResolvedValue({
...customTheme,
showSidebarLogo: themeValue,
lastFetched,


expect(result.current.activeTheme).toMatchObject({
...customTheme,
lastFetched: expect.any(Number),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Conflicts:
#	src/hooks/useTheme.test.ts
#	src/hooks/useTheme.ts
Copy link

github-actions bot commented Oct 7, 2024

Playwright test results

passed  133 passed
flaky  5 flaky
skipped  4 skipped

Details

report  Open report ↗︎
stats  142 tests across 46 suites
duration  14 minutes, 6 seconds
commit  b584c2a
info  For more information on how to debug and view this report, see our readme

Flaky tests

chrome › tests/pageEditor/addStarterBrick.spec.ts › Add starter brick to mod
msedge › tests/pageEditor/addStarterBrick.spec.ts › Add new mod with different starter brick components
msedge › tests/pageEditor/addStarterBrick.spec.ts › Add starter brick to mod
chrome › tests/pageEditor/copyMod.spec.ts › run a copied mod with a built-in integration
msedge › tests/regressions/welcomeStarterBricks.spec.ts › #8740: can view the starter mods on the pixiebrix.com/welcome page

Skipped tests

chrome › tests/regressions/doNotCloseSidebarOnPageEditorSave.spec.ts › #8104: Do not automatically close the sidebar when saving in the Page Editor
msedge › tests/regressions/doNotCloseSidebarOnPageEditorSave.spec.ts › #8104: Do not automatically close the sidebar when saving in the Page Editor
chrome › tests/runtime/googleSheetsIntegration.spec.ts › can activate a google spreadsheet mod with config options
msedge › tests/runtime/googleSheetsIntegration.spec.ts › can activate a google spreadsheet mod with config options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor useTheme to use useManagedStorageState
3 participants