test: Add tests for some runtime helpers (#87)

* test for `renderList`
* test for `toHandlers`
This commit is contained in:
Jordan Pittman 2019-10-05 23:22:42 -04:00 committed by Evan You
parent c4f9b6d592
commit a8c82909fa
3 changed files with 72 additions and 2 deletions

View File

@ -0,0 +1,44 @@
import { renderList } from '../../src/helpers/renderList'
describe('renderList', () => {
it('should render items in an array', () => {
expect(
renderList(['1', '2', '3'], (item, index) => `node ${index}: ${item}`)
).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
})
it('should render characters of a string', () => {
expect(
renderList('123', (item, index) => `node ${index}: ${item}`)
).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
})
it('should render integers 1 through N when given a number N', () => {
expect(renderList(3, (item, index) => `node ${index}: ${item}`)).toEqual([
'node 0: 1',
'node 1: 2',
'node 2: 3'
])
})
it('should render properties in an object', () => {
expect(
renderList(
{ a: 1, b: 2, c: 3 },
(item, key, index) => `node ${index}/${key}: ${item}`
)
).toEqual(['node 0/a: 1', 'node 1/b: 2', 'node 2/c: 3'])
})
it('should render an item for entry in an iterable', () => {
const iterable = function*() {
yield 1
yield 2
yield 3
}
expect(
renderList(iterable(), (item, index) => `node ${index}: ${item}`)
).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
})
})

View File

@ -0,0 +1,25 @@
import { toHandlers } from '../../src/helpers/toHandlers'
import { mockWarn } from '@vue/runtime-test'
describe('toHandlers', () => {
mockWarn()
it('should not accept non-objects', () => {
toHandlers((null as unknown) as any)
toHandlers((undefined as unknown) as any)
expect(
'v-on with no argument expects an object value.'
).toHaveBeenWarnedTimes(2)
})
it('should properly change object keys', () => {
const input = () => {}
const change = () => {}
expect(toHandlers({ input, change })).toStrictEqual({
oninput: input,
onchange: change
})
})
})

View File

@ -57,11 +57,12 @@ export function mockWarn() {
found++
}
})
if (found > 0) {
if (found === n) {
return {
pass: true,
message: () =>
`expected "${received}" not to have been warned ${n} times.`
`expected "${received}" to have been warned ${n} times.`
}
} else {
return {