diff options
Diffstat (limited to 'preact/hooks/test/browser/useRef.test.js')
-rw-r--r-- | preact/hooks/test/browser/useRef.test.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/preact/hooks/test/browser/useRef.test.js b/preact/hooks/test/browser/useRef.test.js new file mode 100644 index 0000000..7d7a657 --- /dev/null +++ b/preact/hooks/test/browser/useRef.test.js @@ -0,0 +1,50 @@ +import { createElement, render } from 'preact'; +import { setupScratch, teardown } from '../../../test/_util/helpers'; +import { useRef } from 'preact/hooks'; + +/** @jsx createElement */ + +describe('useRef', () => { + /** @type {HTMLDivElement} */ + let scratch; + + beforeEach(() => { + scratch = setupScratch(); + }); + + afterEach(() => { + teardown(scratch); + }); + + it('provides a stable reference', () => { + const values = []; + + function Comp() { + const ref = useRef(1); + values.push(ref.current); + ref.current = 2; + return null; + } + + render(<Comp />, scratch); + render(<Comp />, scratch); + + expect(values).to.deep.equal([1, 2]); + }); + + it('defaults to undefined', () => { + const values = []; + + function Comp() { + const ref = useRef(); + values.push(ref.current); + ref.current = 2; + return null; + } + + render(<Comp />, scratch); + render(<Comp />, scratch); + + expect(values).to.deep.equal([undefined, 2]); + }); +}); |