test: Add tests for some runtime helpers (#87)
* test for `renderList` * test for `toHandlers`
This commit is contained in:
parent
c4f9b6d592
commit
a8c82909fa
44
packages/runtime-core/__tests__/helpers/renderList.spec.ts
Normal file
44
packages/runtime-core/__tests__/helpers/renderList.spec.ts
Normal 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'])
|
||||
})
|
||||
})
|
25
packages/runtime-core/__tests__/helpers/toHandlers.spec.ts
Normal file
25
packages/runtime-core/__tests__/helpers/toHandlers.spec.ts
Normal 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
|
||||
})
|
||||
})
|
||||
})
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user