diff options
Diffstat (limited to 'preact/hooks/test/browser/useDebugValue.test.js')
-rw-r--r-- | preact/hooks/test/browser/useDebugValue.test.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/preact/hooks/test/browser/useDebugValue.test.js b/preact/hooks/test/browser/useDebugValue.test.js new file mode 100644 index 0000000..d19ff6b --- /dev/null +++ b/preact/hooks/test/browser/useDebugValue.test.js @@ -0,0 +1,71 @@ +import { createElement, render, options } from 'preact'; +import { setupScratch, teardown } from '../../../test/_util/helpers'; +import { useDebugValue, useState } from 'preact/hooks'; + +/** @jsx createElement */ + +describe('useDebugValue', () => { + /** @type {HTMLDivElement} */ + let scratch; + + beforeEach(() => { + scratch = setupScratch(); + }); + + afterEach(() => { + teardown(scratch); + delete options.useDebugValue; + }); + + it('should do nothing when no options hook is present', () => { + function useFoo() { + useDebugValue('foo'); + return useState(0); + } + + 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.useDebugValue = sinon.spy()); + + function useFoo() { + useDebugValue('foo'); + return useState(0); + } + + function App() { + let [v] = useFoo(); + return <div>{v}</div>; + } + + render(<App />, scratch); + + expect(spy).to.be.calledOnce; + expect(spy).to.be.calledWith('foo'); + }); + + it('should apply optional formatter', () => { + let spy = (options.useDebugValue = sinon.spy()); + + function useFoo() { + useDebugValue('foo', x => x + 'bar'); + return useState(0); + } + + function App() { + let [v] = useFoo(); + return <div>{v}</div>; + } + + render(<App />, scratch); + + expect(spy).to.be.calledOnce; + expect(spy).to.be.calledWith('foobar'); + }); +}); |