diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /preact/hooks/test/browser/useRef.test.js | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
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]); + }); +}); |