-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Children.map/forEach/only/toArray
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Children } from '../src/children' | ||
|
||
describe('Children', () => { | ||
describe('map', () => { | ||
it('should return itself when is undefined', () => { | ||
expect(Children.map()).toBe(undefined) | ||
}) | ||
it('should bind the ctx', () => { | ||
const children = ['1', '2', '3'] | ||
function times2 (n) { | ||
return n * 2 | ||
} | ||
expect(Children.map(children, times2, Number)).toEqual([2, 4, 6]) | ||
}) | ||
it('should exec map with every element', () => { | ||
const children = [1, 2, 3] | ||
function times2 (n) { | ||
return n * 2 | ||
} | ||
expect(Children.map(children, times2)).toEqual([2, 4, 6]) | ||
}) | ||
}) | ||
|
||
describe('forEach', () => { | ||
it('should return itself when is undefined', () => { | ||
expect(Children.forEach()).toBe(undefined) | ||
}) | ||
|
||
it('should exec with every element', () => { | ||
const children = [1, 2, 3] | ||
function times2 (n, i) { | ||
children[i] = n * 2 | ||
} | ||
Children.forEach(children, times2) | ||
expect(children).toEqual([2, 4, 6]) | ||
}) | ||
|
||
it('should bind the ctx', () => { | ||
const children = ['1', '2', '3'] | ||
function times2 (n, i) { | ||
children[i] = n * 2 | ||
} | ||
Children.forEach(children, times2, Number) | ||
expect(children).toEqual([2, 4, 6]) | ||
}) | ||
}) | ||
|
||
it('should return the only one children in a array', () => { | ||
expect(Children.only(['wallace'])).toBe('wallace') | ||
}) | ||
|
||
it('should throw error when input childrens', () => { | ||
expect(() => { | ||
Children.only([1, 2]) | ||
}).toThrowError(`Children.only() expects only one child.`) | ||
}) | ||
|
||
it('count should work', () => { | ||
expect(Children.count([])).toBe(0) | ||
}) | ||
|
||
describe('toArray', () => { | ||
it('should return empty array when undefined', () => { | ||
expect(Children.toArray().length).toBe(0) | ||
}) | ||
}) | ||
}) |