-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2952 from preactjs/use-debug-name
Add addHookName to preact/devtools to relabel native hooks in devtools
- Loading branch information
Showing
7 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
/** | ||
* Customize the displayed name of a useState, useReducer or useRef hook | ||
* in the devtools panel. | ||
* | ||
* @param value Wrapped native hook. | ||
* @param name Custom name | ||
*/ | ||
export function addHookName<T>(value: T, name: string): T; |
This file contains 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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
import { options } from 'preact'; | ||
import { initDevTools } from './devtools'; | ||
|
||
initDevTools(); | ||
|
||
/** | ||
* Display a custom label for a custom hook for the devtools panel | ||
* @type {<T>(value: T, name: string) => T} | ||
*/ | ||
export function addHookName(value, name) { | ||
if (options._addHookName) { | ||
options._addHookName(name); | ||
} | ||
return value; | ||
} |
This file contains 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,51 @@ | ||
import { createElement, render, options } from 'preact'; | ||
import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
import { useState } from 'preact/hooks'; | ||
import { addHookName } from 'preact/devtools'; | ||
|
||
/** @jsx createElement */ | ||
|
||
describe('addHookName', () => { | ||
/** @type {HTMLDivElement} */ | ||
let scratch; | ||
|
||
beforeEach(() => { | ||
scratch = setupScratch(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(scratch); | ||
delete options._addHookName; | ||
}); | ||
|
||
it('should do nothing when no options hook is present', () => { | ||
function useFoo() { | ||
return addHookName(useState(0), 'foo'); | ||
} | ||
|
||
function App() { | ||
let [v] = useFoo(); | ||
return <div>{v}</div>; | ||
} | ||
|
||
expect(() => render(<App />, scratch)).to.not.throw(); | ||
}); | ||
|
||
it('should call options hook with value', () => { | ||
let spy = (options._addHookName = sinon.spy()); | ||
|
||
function useFoo() { | ||
return addHookName(useState(0), 'foo'); | ||
} | ||
|
||
function App() { | ||
let [v] = useFoo(); | ||
return <div>{v}</div>; | ||
} | ||
|
||
render(<App />, scratch); | ||
|
||
expect(spy).to.be.calledOnce; | ||
expect(spy).to.be.calledWith('foo'); | ||
}); | ||
}); |
This file contains 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 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 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