-
Notifications
You must be signed in to change notification settings - Fork 894
fix: prevent workspace search bar text from getting garbled #9703
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
32ebe81
chore: Reorganize hook calls for useWorkspacesFilter
Parkreiner 92006aa
refactor: Clean up some filter logic
Parkreiner 8d10578
refactor: Create debounce utility hooks
Parkreiner 913f944
Merge branch 'main' into filter-fix
Parkreiner fec4384
docs: Clean up comments for clarity
Parkreiner 2d4d285
fix: Update focus logic to apply for any inner focus
Parkreiner 11c06e1
fix: Add onBlur behavior for state syncs
Parkreiner 551780c
chore: Add progress for debounce test
Parkreiner 3d028a7
chore: Finish tests for debounce hooks
Parkreiner 9ea0e1a
Merge branch 'main' into filter-fix
Parkreiner 83c6022
docs: Add file description and warning
Parkreiner 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
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,193 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { useDebouncedFunction, useDebouncedValue } from "./debounce"; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
jest.spyOn(global, "setTimeout"); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// Most UI tests should be structure from the user's experience, but just | ||
// because these are more abstract, general-purpose hooks, it seemed harder to | ||
// do that. Had to bring in some mocks | ||
function renderDebouncedValue<T = unknown>(value: T, time: number) { | ||
return renderHook( | ||
({ value, time }: { value: T; time: number }) => { | ||
return useDebouncedValue(value, time); | ||
}, | ||
{ | ||
initialProps: { value, time }, | ||
}, | ||
); | ||
} | ||
|
||
function renderDebouncedFunction<Args extends unknown[]>( | ||
callbackArg: (...args: Args) => void | Promise<void>, | ||
time: number, | ||
) { | ||
return renderHook( | ||
({ callback, time }: { callback: typeof callbackArg; time: number }) => { | ||
return useDebouncedFunction<Args>(callback, time); | ||
}, | ||
{ | ||
initialProps: { callback: callbackArg, time }, | ||
}, | ||
); | ||
} | ||
|
||
describe(`${useDebouncedValue.name}`, () => { | ||
it("Should immediately return out the exact same value (by reference) on mount", () => { | ||
const value = {}; | ||
const { result } = renderDebouncedValue(value, 2000); | ||
|
||
expect(result.current).toBe(value); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("Should not immediately resync state as the hook re-renders with new value argument", async () => { | ||
let value = 0; | ||
const time = 5000; | ||
|
||
const { result, rerender } = renderDebouncedValue(value, time); | ||
expect(result.current).toEqual(0); | ||
|
||
for (let i = 1; i <= 5; i++) { | ||
setTimeout(() => { | ||
value++; | ||
rerender({ value, time }); | ||
}, i * 100); | ||
} | ||
|
||
await jest.advanceTimersByTimeAsync(time - 100); | ||
expect(result.current).toEqual(0); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("Should resync after specified milliseconds pass with no change to arguments", async () => { | ||
const initialValue = false; | ||
const time = 5000; | ||
|
||
const { result, rerender } = renderDebouncedValue(initialValue, time); | ||
expect(result.current).toEqual(false); | ||
|
||
rerender({ value: !initialValue, time }); | ||
await jest.runAllTimersAsync(); | ||
|
||
expect(result.current).toEqual(true); | ||
expect.hasAssertions(); | ||
}); | ||
}); | ||
|
||
describe(`${useDebouncedFunction.name}`, () => { | ||
describe("hook", () => { | ||
it("Should provide stable function references across re-renders", () => { | ||
const time = 5000; | ||
const { result, rerender } = renderDebouncedFunction(jest.fn(), time); | ||
|
||
const { debounced: oldDebounced, cancelDebounce: oldCancel } = | ||
result.current; | ||
|
||
rerender({ callback: jest.fn(), time }); | ||
const { debounced: newDebounced, cancelDebounce: newCancel } = | ||
result.current; | ||
|
||
expect(oldDebounced).toBe(newDebounced); | ||
expect(oldCancel).toBe(newCancel); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("Resets any pending debounces if the timer argument changes", async () => { | ||
const time = 5000; | ||
let count = 0; | ||
const incrementCount = () => { | ||
count++; | ||
}; | ||
|
||
const { result, rerender } = renderDebouncedFunction( | ||
incrementCount, | ||
time, | ||
); | ||
|
||
result.current.debounced(); | ||
rerender({ callback: incrementCount, time: time + 1 }); | ||
|
||
await jest.runAllTimersAsync(); | ||
expect(count).toEqual(0); | ||
expect.hasAssertions(); | ||
}); | ||
}); | ||
|
||
describe("debounced function", () => { | ||
it("Resolve the debounce after specified milliseconds pass with no other calls", async () => { | ||
let value = false; | ||
const { result } = renderDebouncedFunction(() => { | ||
value = !value; | ||
}, 100); | ||
|
||
result.current.debounced(); | ||
|
||
await jest.runOnlyPendingTimersAsync(); | ||
expect(value).toBe(true); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("Always uses the most recent callback argument passed in (even if it switches while a debounce is queued)", async () => { | ||
let count = 0; | ||
const time = 500; | ||
|
||
const { result, rerender } = renderDebouncedFunction(() => { | ||
count = 1; | ||
}, time); | ||
|
||
result.current.debounced(); | ||
rerender({ | ||
callback: () => { | ||
count = 9999; | ||
}, | ||
time, | ||
}); | ||
|
||
await jest.runAllTimersAsync(); | ||
expect(count).toEqual(9999); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
it("Should reset the debounce timer with repeated calls to the method", async () => { | ||
let count = 0; | ||
const { result } = renderDebouncedFunction(() => { | ||
count++; | ||
}, 2000); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
setTimeout(() => { | ||
result.current.debounced(); | ||
}, i * 100); | ||
} | ||
|
||
await jest.runAllTimersAsync(); | ||
expect(count).toBe(1); | ||
expect.hasAssertions(); | ||
}); | ||
}); | ||
|
||
describe("cancelDebounce function", () => { | ||
it("Should be able to cancel a pending debounce", async () => { | ||
let count = 0; | ||
const { result } = renderDebouncedFunction(() => { | ||
count++; | ||
}, 2000); | ||
|
||
const { debounced, cancelDebounce } = result.current; | ||
debounced(); | ||
cancelDebounce(); | ||
|
||
await jest.runAllTimersAsync(); | ||
expect(count).toEqual(0); | ||
expect.hasAssertions(); | ||
}); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.