test: lint

This commit is contained in:
Benjamin Canac
2024-04-12 14:00:58 +02:00
parent 9993b6727a
commit 290e4ce150
6 changed files with 40 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
import path from 'node:path'
import { mountSuspended } from '@nuxt/test-utils/runtime' import { mountSuspended } from '@nuxt/test-utils/runtime'
import path from 'path'
export default async function (nameOrHtml: string, options: any, component: any) { export default async function (nameOrHtml: string, options: any, component: any) {
let html: string let html: string

View File

@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest' import { describe, it, expect } from 'vitest'
import { defu } from 'defu'
import Checkbox, { type CheckboxProps } from '../../src/runtime/components/Checkbox.vue' import Checkbox, { type CheckboxProps } from '../../src/runtime/components/Checkbox.vue'
import ComponentRender from '../component-render' import ComponentRender from '../component-render'
import { defu } from 'defu'
describe('Checkbox', () => { describe('Checkbox', () => {
it.each([ it.each([
@@ -16,15 +16,14 @@ describe('Checkbox', () => {
['with indeterminate', { props: { indeterminate: true } }], ['with indeterminate', { props: { indeterminate: true } }],
['with help', { props: { label: 'Label', help: 'Help' } }], ['with help', { props: { label: 'Label', help: 'Help' } }],
['with required', { props: { label: 'Label', required: true } }], ['with required', { props: { label: 'Label', required: true } }],
['with custom color', { props: { label: 'Label', color: 'red' } }], ['with custom color', { props: { label: 'Label', color: 'red' as const } }],
['with size 2xs', { props: { size: '2xs' as const } }], ['with size 2xs', { props: { size: '2xs' as const } }],
['with size xs', { props: { size: 'xs' as const } }], ['with size xs', { props: { size: 'xs' as const } }],
['with size sm', { props: { size: 'sm' as const } }], ['with size sm', { props: { size: 'sm' as const } }],
['with size md', { props: { size: 'md' as const } }], ['with size md', { props: { size: 'md' as const } }],
['with size lg', { props: { size: 'lg' as const } }], ['with size lg', { props: { size: 'lg' as const } }],
['with size xl', { props: { size: 'xl' as const } }] ['with size xl', { props: { size: 'xl' as const } }]
// @ts-ignore ])('renders %s correctly', async (nameOrHtml: string, options: { props?: CheckboxProps, slots?: any }) => {
])('renders %s correctly', async (nameOrHtml: string, options: { props?: CheckboxProps }) => {
const html = await ComponentRender(nameOrHtml, defu(options, { props: { id: 42 } }), Checkbox) const html = await ComponentRender(nameOrHtml, defu(options, { props: { id: 42 } }), Checkbox)
expect(html).toMatchSnapshot() expect(html).toMatchSnapshot()
}) })

View File

@@ -1,5 +1,13 @@
import { reactive, ref, nextTick } from 'vue' import { reactive, ref, nextTick } from 'vue'
import { describe, it, expect, test, beforeEach, vi } from 'vitest' import { describe, it, expect, test, beforeEach, vi } from 'vitest'
import type { DOMWrapper, VueWrapper } from '@vue/test-utils'
import { flushPromises } from '@vue/test-utils'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { z } from 'zod'
import * as yup from 'yup'
import Joi from 'joi'
import * as valibot from 'valibot'
import ComponentRender from '../component-render'
import type { FormProps } from '../../src/runtime/components/Form.vue' import type { FormProps } from '../../src/runtime/components/Form.vue'
import { import {
UForm, UForm,
@@ -15,16 +23,8 @@ import {
// UToggle, // UToggle,
// URange // URange
} from '#components' } from '#components'
import { DOMWrapper, flushPromises, VueWrapper } from '@vue/test-utils'
import ComponentRender from '../component-render'
import { mountSuspended } from '@nuxt/test-utils/runtime' async function triggerEvent(
import { z } from 'zod'
import * as yup from 'yup'
import Joi from 'joi'
import * as valibot from 'valibot'
async function triggerEvent (
el: DOMWrapper<Element> | VueWrapper<any, any>, el: DOMWrapper<Element> | VueWrapper<any, any>,
event: string event: string
) { ) {
@@ -32,7 +32,7 @@ async function triggerEvent (
return flushPromises() return flushPromises()
} }
async function setValue ( async function setValue(
el: DOMWrapper<Element> | VueWrapper<any, any>, el: DOMWrapper<Element> | VueWrapper<any, any>,
value: any value: any
) { ) {
@@ -40,13 +40,14 @@ async function setValue (
return flushPromises() return flushPromises()
} }
async function renderForm (options: { async function renderForm(options: {
props: Partial<FormProps<any>> props: Partial<FormProps<any>>
slotVars?: object slotVars?: object
slotComponents?: any slotComponents?: any
slotTemplate: string slotTemplate: string
}) { }) {
const state = reactive({}) const state = reactive({})
return await mountSuspended(UForm, { return await mountSuspended(UForm, {
props: { props: {
id: 42, id: 42,
@@ -55,8 +56,8 @@ async function renderForm (options: {
}, },
slots: { slots: {
default: { default: {
// @ts-ignore // @ts-expect-error setup does not exist on type
setup () { setup() {
return { state, ...options.slotVars } return { state, ...options.slotVars }
}, },
components: { components: {
@@ -112,7 +113,7 @@ describe('Form', () => {
} }
], ],
['custom', { ['custom', {
async validate (state: any) { async validate(state: any) {
const errs = [] const errs = []
if (!state.email) if (!state.email)
errs.push({ name: 'email', message: 'Email is required' }) errs.push({ name: 'email', message: 'Email is required' })
@@ -152,7 +153,7 @@ describe('Form', () => {
await triggerEvent(form, 'submit.prevent') await triggerEvent(form, 'submit.prevent')
// @ts-ignore // @ts-expect-error object is possibly undefined
expect(wrapper.emitted('error')[0][0].errors).toMatchObject([ expect(wrapper.emitted('error')[0][0].errors).toMatchObject([
{ {
id: 'password', id: 'password',
@@ -181,7 +182,7 @@ describe('Form', () => {
const wrapper = await renderForm({ const wrapper = await renderForm({
props: { props: {
validateOn: ['blur'], validateOn: ['blur'],
async validate (state: any) { async validate(state: any) {
if (!state.value) if (!state.value)
return [{ name: 'value', message: 'Error message' }] return [{ name: 'value', message: 'Error message' }]
return [] return []
@@ -216,7 +217,7 @@ describe('Form', () => {
const wrapper = await renderForm({ const wrapper = await renderForm({
props: { props: {
validateOn: ['change'], validateOn: ['change'],
async validate (state: any) { async validate(state: any) {
if (!state.value) if (!state.value)
return [{ name: 'value', message: 'Error message' }] return [{ name: 'value', message: 'Error message' }]
return [] return []
@@ -416,7 +417,7 @@ describe('Form', () => {
const wrapper = await renderForm({ const wrapper = await renderForm({
props: { props: {
validateOn: ['input', 'blur'], validateOn: ['input', 'blur'],
async validate (state: any) { async validate(state: any) {
if (!state.value) if (!state.value)
return [{ name: 'value', message: 'Error message' }] return [{ name: 'value', message: 'Error message' }]
return [] return []
@@ -445,11 +446,10 @@ describe('Form', () => {
await setValue(input, validInputValue) await setValue(input, validInputValue)
// Waiting because of the debounced validation on input event. // Waiting because of the debounced validation on input event.
await new Promise((r) => setTimeout(r, 50)) await new Promise(r => setTimeout(r, 50))
expect(wrapper.text()).not.toContain('Error message') expect(wrapper.text()).not.toContain('Error message')
}) })
describe('api', async () => { describe('api', async () => {
let wrapper: any let wrapper: any
let form: any let form: any
@@ -462,7 +462,7 @@ describe('Form', () => {
UForm, UForm,
UInput UInput
}, },
setup () { setup() {
const form = ref() const form = ref()
const state = reactive({}) const state = reactive({})
const schema = z.object({ const schema = z.object({
@@ -605,7 +605,7 @@ describe('Form', () => {
UForm, UForm,
UInput UInput
}, },
setup () { setup() {
const form = ref() const form = ref()
const state = reactive({ nested: {} }) const state = reactive({ nested: {} })
const schema = z.object({ const schema = z.object({
@@ -618,7 +618,6 @@ describe('Form', () => {
field: z.string().min(1) field: z.string().min(1)
}) })
const onError = vi.fn() const onError = vi.fn()
const onSubmit = vi.fn() const onSubmit = vi.fn()
@@ -651,7 +650,7 @@ describe('Form', () => {
expect(wrapper.setupState.onSubmit).not.toHaveBeenCalled() expect(wrapper.setupState.onSubmit).not.toHaveBeenCalled()
expect(wrapper.setupState.onError).toHaveBeenCalledTimes(1) expect(wrapper.setupState.onError).toHaveBeenCalledTimes(1)
const onErrorCallArgs = wrapper.setupState.onError.mock.lastCall[0] const onErrorCallArgs = wrapper.setupState.onError.mock.lastCall[0]
expect(onErrorCallArgs.childrens[0].errors).toMatchObject([ { id: 'nestedInput', name: 'field', message: 'Required' } ]) expect(onErrorCallArgs.childrens[0].errors).toMatchObject([{ id: 'nestedInput', name: 'field', message: 'Required' }])
expect(onErrorCallArgs.errors).toMatchObject([ expect(onErrorCallArgs.errors).toMatchObject([
{ id: 'emailInput', name: 'email', message: 'Required' }, { id: 'emailInput', name: 'email', message: 'Required' },
{ id: 'passwordInput', name: 'password', message: 'Required' } { id: 'passwordInput', name: 'password', message: 'Required' }

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, test } from 'vitest' import { describe, it, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Input, { type InputProps } from '../../src/runtime/components/Input.vue' import Input, { type InputProps } from '../../src/runtime/components/Input.vue'
import ComponentRender from '../component-render' import ComponentRender from '../component-render'
import { mount } from '@vue/test-utils'
describe('Input', () => { describe('Input', () => {
it.each([ it.each([
@@ -35,9 +35,9 @@ describe('Input', () => {
}) })
it.each([ it.each([
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' } ], ['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 } ], ['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' } ] ['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }]
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => { ])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
const wrapper = mount(Input, { const wrapper = mount(Input, {
...options ...options

View File

@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest' import { describe, it, expect } from 'vitest'
import { defu } from 'defu'
import RadioGroup, { type RadioGroupProps } from '../../src/runtime/components/RadioGroup.vue' import RadioGroup, { type RadioGroupProps } from '../../src/runtime/components/RadioGroup.vue'
import ComponentRender from '../component-render' import ComponentRender from '../component-render'
import { defu } from 'defu'
const defaultOptions = [ const defaultOptions = [
{ value: '1', label: 'Option 1' }, { value: '1', label: 'Option 1' },

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, test } from 'vitest' import { describe, it, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Textarea, { type TextareaProps } from '../../src/runtime/components/Textarea.vue' import Textarea, { type TextareaProps } from '../../src/runtime/components/Textarea.vue'
import ComponentRender from '../component-render' import ComponentRender from '../component-render'
import { mount } from '@vue/test-utils'
describe('Textarea', () => { describe('Textarea', () => {
it.each([ it.each([
@@ -12,26 +12,25 @@ describe('Textarea', () => {
['with required', { props: { required: true } }], ['with required', { props: { required: true } }],
['with disabled', { props: { disabled: true } }], ['with disabled', { props: { disabled: true } }],
['with rows', { props: { rows: 5 } }], ['with rows', { props: { rows: 5 } }],
['with size', { props: { size: 'sm' } }], ['with size', { props: { size: 'sm' as const } }],
['with color', { props: { color: 'blue' } }], ['with color', { props: { color: 'blue' as const } }],
['with size 2xs', { props: { size: '2xs' as const } }], ['with size 2xs', { props: { size: '2xs' as const } }],
['with size xs', { props: { size: 'xs' as const } }], ['with size xs', { props: { size: 'xs' as const } }],
['with size sm', { props: { size: 'sm' as const } }], ['with size sm', { props: { size: 'sm' as const } }],
['with size md', { props: { size: 'md' as const } }], ['with size md', { props: { size: 'md' as const } }],
['with size lg', { props: { size: 'lg' as const } }], ['with size lg', { props: { size: 'lg' as const } }],
['with size xl', { props: { size: 'xl' as const } }], ['with size xl', { props: { size: 'xl' as const } }],
['with variant', { variant: 'outline' }], ['with variant', { props: { variant: 'outline' as const } }],
['with default slot', { slots: { default: () => 'Default slot' } }] ['with default slot', { slots: { default: () => 'Default slot' } }]
// @ts-ignore ])('renders %s correctly', async (nameOrHtml: string, options: { props?: TextareaProps, slots?: any }) => {
])('renders %s correctly', async (nameOrHtml: string, options: { props: TextareaProps, slots?: any }) => {
const html = await ComponentRender(nameOrHtml, options, Textarea) const html = await ComponentRender(nameOrHtml, options, Textarea)
expect(html).toMatchSnapshot() expect(html).toMatchSnapshot()
}) })
it.each([ it.each([
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' } ], ['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 } ], ['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' } ] ['with .lazy modifier', { props: { modelModifiers: { lazy: true } } }, { input: 'input', expected: 'input' }]
])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => { ])('%s works', async (_nameOrHtml: string, options: { props?: any, slots?: any }, spec: { input: any, expected: any }) => {
const wrapper = await mount(Textarea, { const wrapper = await mount(Textarea, {
...options ...options