children.test.ts
This file
Path
src/jsx/children.test.ts
52 lines 路 1.5 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import { createElement } from '.'import { Children } from './children'
describe('map', () => { it('should map children', () => { const element = createElement('div', null, 1, 2, 3) const result = Children.map(element.children, (child) => (child as number) * 2) expect(result).toEqual([2, 4, 6]) })})
describe('forEach', () => { it('should iterate children', () => { const element = createElement('div', null, 1, 2, 3) const result: number[] = [] Children.forEach(element.children, (child) => { result.push(child as number) }) expect(result).toEqual([1, 2, 3]) })})
describe('count', () => { it('should count children', () => { const element = createElement('div', null, 1, 2, 3) const result = Children.count(element.children) expect(result).toBe(3) })})
describe('only', () => { it('should return the only child', () => { const element = createElement('div', null, 1) const result = Children.only(element.children) expect(result).toBe(1) })
it('should throw an error if there are multiple children', () => { const element = createElement('div', null, 1, 2) expect(() => Children.only(element.children)).toThrowError( 'Children.only() expects only one child' ) })})
describe('toArray', () => { it('should convert children to an array', () => { const element = createElement('div', null, 1, 2, 3) const result = Children.toArray(element.children) expect(result).toEqual([1, 2, 3]) })})