test(utils): [vue] add icon,install,vnode and global-node test (#16216)

* test(utils): [vue] add icon,install,vnode and global-node test

* test(utils): [global-node] remove repeat code of useless
This commit is contained in:
wzc520pyfm 2024-04-30 10:37:27 +08:00 committed by GitHub
parent e75cee1ce4
commit 98ce640684
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 170 additions and 0 deletions

View File

@ -41,4 +41,13 @@ describe('global-nodes', () => {
expect(el.parentElement).toBe(target)
})
it('should create node with id', () => {
const myId = 'my-id'
const el = createGlobalNode(myId)
expect(el).not.toBeNull()
expect(el.getAttribute('id')).toBe(myId)
})
})

View File

@ -0,0 +1,42 @@
import { shallowMount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { TypeComponentsMap, ValidateComponentsMap } from '../..'
describe('TypeComponentsMap', () => {
it('Given a type "success", it should return SuccessFilled component', () => {
const component = shallowMount(TypeComponentsMap.success)
expect(component.exists()).toBe(true)
})
it('Given a type "warning", it should return WarningFilled component', () => {
const component = shallowMount(TypeComponentsMap.warning)
expect(component.exists()).toBe(true)
})
it('Given a type "error", it should return CircleCloseFilled component', () => {
const component = shallowMount(TypeComponentsMap.error)
expect(component.exists()).toBe(true)
})
it('Given a type "info", it should return InfoFilled component', () => {
const component = shallowMount(TypeComponentsMap.info)
expect(component.exists()).toBe(true)
})
})
describe('ValidateComponentsMap', () => {
it('Given a validation state "validating", it should return Loading component', () => {
const component = shallowMount(ValidateComponentsMap.validating)
expect(component.exists()).toBe(true)
})
it('Given a validation state "success", it should return CircleCheck component', () => {
const component = shallowMount(ValidateComponentsMap.success)
expect(component.exists()).toBe(true)
})
it('Given a validation state "error", it should return CircleClose component', () => {
const component = shallowMount(ValidateComponentsMap.error)
expect(component.exists()).toBe(true)
})
})

View File

@ -0,0 +1,98 @@
import { createApp } from 'vue'
import { describe, expect, it } from 'vitest'
import { withInstall, withInstallDirective } from '../..'
describe('withInstall', () => {
it('it should add an install method to the main component', () => {
const mainComponent = {
name: 'MainComponent',
render: () => null,
}
const componentWithInstall = withInstall(mainComponent)
expect(typeof componentWithInstall.install).toBe('function')
})
it('it should register the main component and extra components when calling install', () => {
const mainComponent = {
name: 'MainComponent',
render: () => null,
}
const extraComponents = {
ExtraComponent: {
name: 'ExtraComponent',
render: () => null,
},
}
const app = createApp({})
const componentWithInstall = withInstall(mainComponent, extraComponents)
componentWithInstall.install?.(app)
expect(app.component('MainComponent')).toBeTruthy()
expect(app.component('ExtraComponent')).toBeTruthy()
})
it('it should add extra components to the main component when provided', () => {
const mainComponent = {
name: 'MainComponent',
render: () => null,
}
const extraComponents = {
ExtraComponent: {
name: 'ExtraComponent',
render: () => null,
},
}
const componentWithInstall = withInstall(mainComponent, extraComponents)
expect(componentWithInstall.ExtraComponent).toBeTruthy()
})
it('it should not add extra components if none provided', () => {
const mainComponent = {
name: 'MainComponent',
render: () => null,
}
const componentWithInstall = withInstall(mainComponent)
expect(componentWithInstall.ExtraComponent).toBeFalsy()
})
})
describe('withInstallDirective', () => {
it('it should add an install method to the directive', () => {
const directive = {
mounted: () => null,
unmounted: () => null,
}
const directiveWithInstall = withInstallDirective(
directive,
'test-directive'
)
expect(typeof directiveWithInstall.install).toBe('function')
})
it('it should register the directive when calling install', () => {
const directive = {
mounted: () => null,
unmounted: () => null,
}
const app = createApp({})
const directiveWithInstall = withInstallDirective(
directive,
'test-directive'
)
directiveWithInstall.install?.(app)
expect(app.directive('test-directive')).toBeTruthy()
})
})

View File

@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest'
import { ensureOnlyChild } from '../..'
describe('ensureOnlyChild', () => {
it('it should throw an error if input is not an array or undefined', () => {
expect(() => {
ensureOnlyChild('not an array' as any)
}).toThrow('expect to receive a single Vue element child')
})
it('it should throw an error if input array has more than one element', () => {
expect(() => {
ensureOnlyChild([1, 2])
}).toThrow('expect to receive a single Vue element child')
})
it('it should return the only child if input is an array with one element', () => {
const child = { type: 'div' }
expect(ensureOnlyChild([child as any])).toEqual(child)
})
})