From 01d0260898d717939046578ff2a87a7bf5800226 Mon Sep 17 00:00:00 2001 From: QuirinEcker Date: Wed, 13 Mar 2024 13:09:54 +0100 Subject: [PATCH] added some ui tests still wip --- src/components/EntryForm.test.ts | 96 ++++++++++++++++++++++++++++++-- src/components/EntryForm.vue | 16 ++++-- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/components/EntryForm.test.ts b/src/components/EntryForm.test.ts index 3c0cc13..347412a 100644 --- a/src/components/EntryForm.test.ts +++ b/src/components/EntryForm.test.ts @@ -2,20 +2,104 @@ import { faker } from "@faker-js/faker"; import { describe, expect, it } from "vitest"; import { mount } from '@vue/test-utils' import EntryForm from './EntryForm.vue' +import { Entry } from "@/data/entries"; describe('Entry Form tests', () => { + function getFormFields(component: ReturnType) { + return { + nameField: component.get('input'), + textField: component.get('textarea') + } + } + + function getSampleValues() { + return { + name: faker.word.noun(), + text: faker.lorem.paragraph() + } + } + + function getAdditionalElements(component: ReturnType, ...ids: string[]) { + const arr = [] + + for (const id of ids) { + arr.push(component.find(id)) + } + + return arr; + } + it('takes in values for name and title and displays them in the respected fields', () => { - const name = faker.word.noun() - const text = faker.lorem.paragraph() + const { name, text } = getSampleValues() + const component = mount(EntryForm, { props: { - name, text, action: 'edit' + action: 'edit', inputEntry: { + name, text + } } }) - const nameField = component.find('input') - const textField = component.find('textarea') - console.log(nameField.element.classList, textField.text()) + const { nameField, textField } = getFormFields(component) + expect(nameField.element.value).toBe(name) + expect(textField.element.value).toBe(text) }) + + it('takes no entry data and displays empty fields', () => { + const component = mount(EntryForm, { + props: { + action: 'edit' + } + }) + + const { nameField, textField } = getFormFields(component) + expect(nameField.element.value).toBe('') + expect(textField.element.value).toBe('') + }) + + + it('takes no action and displays create on the Button', () => { + const component = mount(EntryForm) + const button = component.find('button') + expect(button.text()).toBe('Create') + }) + + it('takes create action and displays Create on the Button', () => { + const component = mount(EntryForm, { props: { action: 'create' } }) + const button = component.find('button') + expect(button.text()).toBe('Create') + }) + + it('takes edit action and displays Edit on the Button', () => { + const component = mount(EntryForm, { props: { action: 'edit' } }) + const button = component.find('button') + expect(button.text()).toBe('Edit') + }) + + it('displays nothing when the form is valid and processed', () => { + const { name, text } = getSampleValues() + const component = mount(EntryForm) + const { nameField, textField } = getFormFields(component) + const [button, errorSpan] = getAdditionalElements(component, 'button', 'span') + + nameField.element.value = name + textField.element.value = text + + button.trigger('click') + expect(errorSpan.text()).toBe('') + }) + + it('displays that the name is missing when no name is entered', () => { + const { name } = getSampleValues() + const component = mount(EntryForm) + const { nameField } = getFormFields(component) + const [button, errorSpan] = getAdditionalElements(component, 'button', 'span') + + nameField.element.value = name + + button.trigger('click') + expect(errorSpan.text()).toBe('name is required') + }) + }) \ No newline at end of file diff --git a/src/components/EntryForm.vue b/src/components/EntryForm.vue index 9d92b00..c89862d 100644 --- a/src/components/EntryForm.vue +++ b/src/components/EntryForm.vue @@ -4,22 +4,25 @@ import { Input } from '@/components/ui/input'; import { Button } from './ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Entry } from '@/data/entries'; -import { ref } from 'vue'; +import { Ref, ref } from 'vue'; const emit = defineEmits<{ submit: [value: CreateEntrySchema] }>() -const props = defineProps<{ - action: 'create' | 'edit' +const props = withDefaults(defineProps<{ + action?: 'create' | 'edit' inputEntry?: Entry | undefined -}>() +}>(), { + action: 'create' +}) const nameField = ref(props.inputEntry ? props.inputEntry.name : '') const textField = ref(props.inputEntry ? props.inputEntry.text : '') +const errorMessage: Ref = ref(undefined) const createEntrySchema = z.object({ - name: z.string(), + name: z.string({required_error: 'name is required'}), text: z.string().optional() }) @@ -34,6 +37,7 @@ function submit() { if (result.success) { emit('submit', result.data) + } else { } } @@ -42,6 +46,8 @@ function submit() {