Reviewed-on: McPeakDev/nychthemeron#3 Co-authored-by: Matthew L McPeak <m.mcpeak98@icloud.com> Co-committed-by: Matthew L McPeak <m.mcpeak98@icloud.com>
94 lines
3 KiB
TypeScript
94 lines
3 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
import type { App } from 'vue'
|
|
import { createNychthemeron, Button, LoadingIcon } from '../src/index'
|
|
|
|
//eslint-disable-next-line
|
|
const callSetup = (component: any, props: Record<string, unknown>) => {
|
|
const mergedProps: Record<string, unknown> = {}
|
|
|
|
if (component.props) {
|
|
for (const [key, propDef] of Object.entries(component.props)) {
|
|
if (typeof propDef === 'object' && propDef !== null && 'default' in propDef) {
|
|
mergedProps[key] = (propDef as { default: unknown }).default
|
|
}
|
|
}
|
|
}
|
|
|
|
Object.assign(mergedProps, props)
|
|
|
|
if (component.setup) {
|
|
const result = component.setup(mergedProps, { slots: {}, attrs: {} })
|
|
if (typeof result === 'function') {
|
|
const vnode = result()
|
|
return (vnode?.props as Record<string, unknown>) || {}
|
|
}
|
|
return result
|
|
}
|
|
return mergedProps
|
|
}
|
|
|
|
describe('createNychthemeron', () => {
|
|
it('returns a Vue plugin with install method', () => {
|
|
const plugin = createNychthemeron()
|
|
expect(typeof plugin.install).toBe('function')
|
|
})
|
|
|
|
it('install registers NychButton and NychLoadingIcon globally', () => {
|
|
const plugin = createNychthemeron()
|
|
const mockApp = { component: vi.fn() } as unknown as App
|
|
plugin.install(mockApp)
|
|
expect(mockApp.component).toHaveBeenCalledWith('NychButton', expect.anything())
|
|
expect(mockApp.component).toHaveBeenCalledWith('NychLoadingIcon', expect.anything())
|
|
})
|
|
|
|
it('exposes Button and LoadingIcon components', () => {
|
|
const { Button: btn, LoadingIcon: icon } = createNychthemeron()
|
|
expect(btn).toBeDefined()
|
|
expect(icon).toBeDefined()
|
|
})
|
|
})
|
|
|
|
describe('Button', () => {
|
|
it('is a valid Vue component', () => {
|
|
expect(Button).toBeDefined()
|
|
expect(typeof (Button as Record<string, unknown>).setup).toBe('function')
|
|
})
|
|
|
|
it('auto-disables when loading is true', () => {
|
|
const result = callSetup(Button, { pt: {}, loading: true })
|
|
expect(result.disabled).toBe(true)
|
|
})
|
|
|
|
it('stays enabled when loading is false', () => {
|
|
const result = callSetup(Button, { pt: {}, loading: false })
|
|
expect(result.disabled).toBeFalsy()
|
|
})
|
|
|
|
it('injects severity class into pt.root', () => {
|
|
const result = callSetup(Button, { pt: {}, severity: 'danger' })
|
|
expect(result.pt?.root?.class).toContain('nych-button-danger')
|
|
})
|
|
|
|
it('defaults severity to primary', () => {
|
|
const result = callSetup(Button, { pt: {} })
|
|
expect(result.pt?.root?.class).toContain('nych-button-primary')
|
|
})
|
|
|
|
it('does not leak engine internal props', () => {
|
|
const result = callSetup(Button, { pt: {} })
|
|
expect(result).not.toHaveProperty('injectedClasses')
|
|
expect(result).not.toHaveProperty('injectedStyles')
|
|
expect(result).not.toHaveProperty('stylePlug')
|
|
})
|
|
|
|
it('does not leak severity to inner component', () => {
|
|
const result = callSetup(Button, { pt: {}, severity: 'danger' })
|
|
expect(result).not.toHaveProperty('severity')
|
|
})
|
|
})
|
|
|
|
describe('LoadingIcon', () => {
|
|
it('is a valid Vue component', () => {
|
|
expect(LoadingIcon).toBeDefined()
|
|
})
|
|
})
|