-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix: allow multiple bindCLIShortcuts calls with shortcut merging
#21103
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
+110
−7
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87895a2
fix: allow CLI shortcuts bind twice
edmundhung 5537396
align dev server and preview server behavior
edmundhung 0cfb2ca
avoid reinitializing readline interface
edmundhung 2f5880b
introduce 3rd parameter to bypass env check
edmundhung 758045a
setup a close listener only when initializing rl
edmundhung 4790de0
avoid duplicated shortcut accumulated indefinitely
edmundhung 66cb598
chore: use `.some` instead of `.find`
sapphi-red 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { describe, expect, test, vi } from 'vitest' | ||
| import { createServer } from '../server' | ||
| import { preview } from '../preview' | ||
| import { bindCLIShortcuts } from '../shortcuts' | ||
|
|
||
| describe('bindCLIShortcuts', () => { | ||
| test.each([ | ||
| ['dev server', () => createServer()], | ||
| ['preview server', () => preview()], | ||
| ])('binding custom shortcuts with the %s', async (_, startServer) => { | ||
| const server = await startServer() | ||
|
|
||
| try { | ||
| const xAction = vi.fn() | ||
| const yAction = vi.fn() | ||
|
|
||
| bindCLIShortcuts( | ||
| server, | ||
| { | ||
| customShortcuts: [ | ||
| { key: 'x', description: 'test x', action: xAction }, | ||
| { key: 'y', description: 'test y', action: yAction }, | ||
| ], | ||
| }, | ||
| true, | ||
| ) | ||
|
|
||
| expect.assert( | ||
| server._rl, | ||
| 'The readline interface should be defined after binding shortcuts.', | ||
| ) | ||
| expect(xAction).not.toHaveBeenCalled() | ||
|
|
||
| server._rl.emit('line', 'x') | ||
| await vi.waitFor(() => expect(xAction).toHaveBeenCalledOnce()) | ||
|
|
||
| const xUpdatedAction = vi.fn() | ||
| const zAction = vi.fn() | ||
|
|
||
| xAction.mockClear() | ||
| bindCLIShortcuts( | ||
| server, | ||
| { | ||
| customShortcuts: [ | ||
| { key: 'x', description: 'test x updated', action: xUpdatedAction }, | ||
| { key: 'z', description: 'test z', action: zAction }, | ||
| ], | ||
| }, | ||
| true, | ||
| ) | ||
|
|
||
| expect(xUpdatedAction).not.toHaveBeenCalled() | ||
| server._rl.emit('line', 'x') | ||
| await vi.waitFor(() => expect(xUpdatedAction).toHaveBeenCalledOnce()) | ||
|
|
||
| // Ensure original xAction is not called again | ||
| expect(xAction).not.toBeCalled() | ||
|
|
||
| expect(yAction).not.toHaveBeenCalled() | ||
| server._rl.emit('line', 'y') | ||
| await vi.waitFor(() => expect(yAction).toHaveBeenCalledOnce()) | ||
|
|
||
| expect(zAction).not.toHaveBeenCalled() | ||
| server._rl.emit('line', 'z') | ||
| await vi.waitFor(() => expect(zAction).toHaveBeenCalledOnce()) | ||
| } finally { | ||
| await server.close() | ||
| } | ||
| }) | ||
| }) | ||
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
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
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.
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.
It's probably a miss-understanding of the mocking system but I don't understand this assertion. Does using
toHaveBeenCalledOnceabove "reset" the counter? Otherwise I would have usetoHaveBeenCalledOnceagainUh oh!
There was an error while loading. Please reload this page.
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 reset the counter with
xAction.mockClear()right before callingbindCLIShortcutsagain. If you find it confusing, happy to remove that and just assert whether xAction is still only called once.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.
Oh I missed that line. Ok makes more sense thanks. I think it just taste then so no need to change