Skip to content

Commit

Permalink
test(utils): [vue] add icon,install,vnode and global-node test (#16216)
Browse files Browse the repository at this point in the history
* test(utils): [vue] add icon,install,vnode and global-node test

* test(utils): [global-node] remove repeat code of useless
  • Loading branch information
wzc520pyfm committed Apr 30, 2024
1 parent e75cee1 commit 98ce640
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/utils/__tests__/vue/global-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
42 changes: 42 additions & 0 deletions packages/utils/__tests__/vue/icon.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
98 changes: 98 additions & 0 deletions packages/utils/__tests__/vue/install.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
21 changes: 21 additions & 0 deletions packages/utils/__tests__/vue/vnode.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 98ce640

Please sign in to comment.