Skip to content

Commit

Permalink
test: Children.map/forEach/only/toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Dec 5, 2017
1 parent 14dc228 commit 72b1d90
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions packages/nerv/__tests__/children.spec.js
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)
})
})
})

0 comments on commit 72b1d90

Please sign in to comment.