-
Notifications
You must be signed in to change notification settings - Fork 123
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
Version 5 #1057
base: main
Are you sure you want to change the base?
Version 5 #1057
Changes from 2 commits
1683b4e
dc91f11
a084400
3b14891
6bfbaef
7fb7b1a
bfea2ce
4150694
b634711
8e821c6
49dd902
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||
import React from 'react' | ||||||
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. It might be a bit better to do a namespace import:
Suggested change
Source: https://twitter.com/dan_abramov/status/1308739731551858689 This can make the difference in the future (React v19 or v20). What is the current roadmap for v5? Just curious 🙃 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. Thanks for the heads up. I have very limited time & motivation to improve on version 5. I hope this will change in the next months, but no promises. |
||||||
import { render, act, renderHook } from '@testing-library/react' | ||||||
import { HotkeysProvider, useHotkeysContext, useHotkeys } from '../src' | ||||||
import { ReactNode } from 'react' | ||||||
|
@@ -21,17 +22,6 @@ test('should default to wildcard scope', () => { | |||||
expect(result.current.enabledScopes).toEqual(['*']) | ||||||
}) | ||||||
|
||||||
test('should default to wildcard scope if empty array is provided as initialActiveScopes', () => { | ||||||
const wrapper = ({ children }: { children: ReactNode }) => ( | ||||||
<HotkeysProvider initiallyActiveScopes={[]}>{children}</HotkeysProvider> | ||||||
) | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper, | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual(['*']) | ||||||
}) | ||||||
|
||||||
test('should return active scopes and scope modifying functions', () => { | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper: HotkeysProvider, | ||||||
|
@@ -55,24 +45,6 @@ test('should activate scope by calling activateScope', () => { | |||||
expect(result.current.enabledScopes).toEqual(['foo']) | ||||||
}) | ||||||
|
||||||
test('should reactivate wildcard scope if all other scopes are deactivated', () => { | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper: HotkeysProvider, | ||||||
}) | ||||||
|
||||||
act(() => { | ||||||
result.current.enableScope('foo') | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual(['foo']) | ||||||
|
||||||
act(() => { | ||||||
result.current.disableScope('foo') | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual(['*']) | ||||||
}) | ||||||
|
||||||
test('should return multiple scopes if different scopes are activated', () => { | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper: HotkeysProvider, | ||||||
|
@@ -128,7 +100,7 @@ test('should toggle scope by calling toggleScope', () => { | |||||
result.current.toggleScope('foo') | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual(['*']) | ||||||
expect(result.current.enabledScopes).toEqual([]) | ||||||
|
||||||
act(() => { | ||||||
result.current.toggleScope('foo') | ||||||
|
@@ -137,7 +109,7 @@ test('should toggle scope by calling toggleScope', () => { | |||||
expect(result.current.enabledScopes).toEqual(['foo']) | ||||||
}) | ||||||
|
||||||
test('should keep wildcard scope active when all is the only active scope and gets deactivated', () => { | ||||||
test('should be able to disable wildcard like any other scope', () => { | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper: HotkeysProvider, | ||||||
}) | ||||||
|
@@ -146,7 +118,7 @@ test('should keep wildcard scope active when all is the only active scope and ge | |||||
result.current.disableScope('*') | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual(['*']) | ||||||
expect(result.current.enabledScopes).toEqual([]) | ||||||
}) | ||||||
|
||||||
test('should return initially set scopes', () => { | ||||||
|
@@ -239,3 +211,24 @@ test('should return descriptions for bound hotkeys', () => { | |||||
|
||||||
expect(result.current.hotkeys[0].description).toEqual('bar') | ||||||
}) | ||||||
|
||||||
test('should have no active scopes after deactivating all current scopes', () => { | ||||||
const { result } = renderHook(() => useHotkeysContext(), { | ||||||
wrapper: HotkeysProvider, | ||||||
}) | ||||||
act(() => { | ||||||
result.current.enableScope('foo') | ||||||
}) | ||||||
act(() => { | ||||||
result.current.enableScope('bar') | ||||||
}) | ||||||
expect(result.current.enabledScopes).toEqual(['foo', 'bar']) | ||||||
act(() => { | ||||||
result.current.disableScope('foo') | ||||||
}) | ||||||
act(() => { | ||||||
result.current.disableScope('bar') | ||||||
}) | ||||||
|
||||||
expect(result.current.enabledScopes).toEqual([]) | ||||||
}) |
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.
Maybe we could have this state wrapped in a React Context to allow separating enableScope and disableScope, thus allowing us to avoid re-renders where we use them.
This is a very painful problem right now, we cannot call useHotkeysContext() without having re-renders because of enabledScopes changing and causing every component calling useHotkeysContext() to re-render, even if they don't care about this dependency and just want to enable/disable a scope.
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 would be glad to collaborate on this because we do an extensive usage of scopes at https://github.com/twentyhq/twenty and we have problems managing re-renders.
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 don't think I quite understand this issue.
enableScope
anddisableScope
are already decoupled.enabledScopes
is a state, so changing the value will retrigger depend components. I understand that this causes re renders when callingenableScope
, but the only fix would be to have theenabledScopes
in another context, which, API wise, seems weird.Maybe I am missing something?