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/compat/test/browser/Children.test.js | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'preact/compat/test/browser/Children.test.js')
-rw-r--r-- | preact/compat/test/browser/Children.test.js | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/preact/compat/test/browser/Children.test.js b/preact/compat/test/browser/Children.test.js new file mode 100644 index 0000000..4e0c32d --- /dev/null +++ b/preact/compat/test/browser/Children.test.js @@ -0,0 +1,185 @@ +import { + setupScratch, + teardown, + serializeHtml +} from '../../../test/_util/helpers'; +import { div, span } from '../../../test/_util/dom'; +import React, { createElement, Children, render } from 'preact/compat'; + +describe('Children', () => { + /** @type {HTMLDivElement} */ + let scratch; + + beforeEach(() => { + scratch = setupScratch(); + }); + + afterEach(() => { + teardown(scratch); + }); + + describe('.count', () => { + let count; + function Foo(props) { + count = Children.count(props.children); + return <div>{count}</div>; + } + + it('should return 0 for no children', () => { + render(<Foo />, scratch); + expect(count).to.equal(0); + }); + + it('should return number of children', () => { + render( + <Foo> + <div /> + foo + </Foo>, + scratch + ); + expect(count).to.equal(2); + }); + }); + + describe('.only', () => { + let actual; + function Foo(props) { + actual = Children.only(props.children); + return <div>{actual}</div>; + } + + it('should only allow 1 child', () => { + render(<Foo>foo</Foo>, scratch); + expect(actual).to.equal('foo'); + }); + + it('should throw if no children are passed', () => { + // eslint-disable-next-line prefer-arrow-callback + expect(function() { + render(<Foo />, scratch); + }).to.throw(); + }); + + it('should throw if more children are passed', () => { + // eslint-disable-next-line prefer-arrow-callback + expect(function() { + render( + <Foo> + foo + <span /> + </Foo>, + scratch + ); + }).to.throw(); + }); + }); + + describe('.map', () => { + function Foo(props) { + let children = Children.map(props.children, child => ( + <span>{child}</span> + )); + return <div>{children}</div>; + } + + it('should iterate over children', () => { + render( + <Foo> + foo<div>bar</div> + </Foo>, + scratch + ); + let expected = div([span('foo'), span(div('bar'))]); + expect(serializeHtml(scratch)).to.equal(expected); + }); + + it('should work with no children', () => { + render(<Foo />, scratch); + expect(serializeHtml(scratch)).to.equal('<div></div>'); + }); + + it('should work with children as zero number', () => { + const testNumber = 0; + + render(<Foo>{testNumber}</Foo>, scratch); + expect(serializeHtml(scratch)).to.equal('<div><span>0</span></div>'); + }); + + it('should flatten result', () => { + const ProblemChild = ({ children }) => { + return React.Children.map(children, child => { + return React.Children.map(child.props.children, x => x); + }).filter(React.isValidElement); + }; + + const App = () => { + return ( + <ProblemChild> + <div> + <div>1</div> + <div>2</div> + </div> + </ProblemChild> + ); + }; + + render(<App />, scratch); + + expect(scratch.textContent).to.equal('12'); + }); + + it('should call with indices', () => { + const assertion = []; + const ProblemChild = ({ children }) => { + return React.Children.map(children, (child, i) => { + assertion.push(i); + return React.Children.map(child.props.children, (x, j) => { + assertion.push(j); + return x; + }); + }).filter(React.isValidElement); + }; + + const App = () => { + return ( + <ProblemChild> + <div> + <div>1</div> + <div>2</div> + </div> + <div> + <div>3</div> + <div>4</div> + </div> + </ProblemChild> + ); + }; + + render(<App />, scratch); + expect(scratch.textContent).to.equal('1234'); + expect(assertion.length).to.equal(6); + }); + }); + + describe('.forEach', () => { + function Foo(props) { + let children = []; + Children.forEach(props.children, child => + children.push(<span>{child}</span>) + ); + return <div>{children}</div>; + } + + it('should iterate over children', () => { + render( + <Foo> + foo<div>bar</div> + </Foo>, + scratch + ); + let expected = div([span('foo'), span(div('bar'))]); + expect(serializeHtml(scratch)).to.equal(expected); + }); + }); +}); |