Skip to content

Commit

Permalink
more tests....
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliL committed Jul 26, 2024
1 parent cacced1 commit 94c0451
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 25 deletions.
15 changes: 12 additions & 3 deletions src/components/InputStandard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@
<script lang="ts" setup>
import { useField } from "vee-validate";
import { toTypedSchema } from "@vee-validate/zod";
import { computed, onMounted, ref, type PropType, type Ref } from "vue";
import {
computed,
nextTick,
onMounted,
ref,
type PropType,
type Ref,
} from "vue";
import { any, type ZodType } from "zod";
import {
Expand Down Expand Up @@ -131,13 +138,15 @@ const errorData = computed((): ErrorData => {
const alignmentClass = props.align ? "text-" + props.align : "";
const fieldRef = ref(null);
const fieldRef = ref<HTMLInputElement | null>(null);
onMounted(() => {
fieldValue.value = props.modelValue;
if (props.focus) {
(fieldRef.value as any).focus();
nextTick(() => {
if (fieldRef.value) fieldRef.value.focus();
});
}
});
</script>
16 changes: 13 additions & 3 deletions src/components/SelectStandard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@
<script lang="ts" setup>
import { useField } from "vee-validate";
import { toTypedSchema } from "@vee-validate/zod";
import { computed, onMounted, ref, watch, type PropType, type Ref } from "vue";
import {
computed,
nextTick,
onMounted,
ref,
watch,
type PropType,
type Ref,
} from "vue";
import { any, type ZodType } from "zod";
import type { SelectBoxValue } from "@/model/SelectBoxValue";
Expand Down Expand Up @@ -105,11 +113,13 @@ const errorData = computed((): ErrorData => {
);
});
const fieldRef = ref(null);
const fieldRef = ref<HTMLSelectElement | null>(null);
onMounted(() => {
if (props.focus) {
(fieldRef.value as any).focus();
nextTick(() => {
if (fieldRef.value) fieldRef.value.focus();
});
}
});
Expand Down
21 changes: 21 additions & 0 deletions src/tests/TestUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fireEvent, waitFor } from "@testing-library/vue";
import { expect } from "vitest";

export async function waitForInputHasValue(
item: HTMLInputElement,
value: string,
message?: string,
) {
await waitFor(() => {
expect(item.value, message).toBe(value);
});
}

export async function setInputValueAndWait(
item: HTMLInputElement,
value: string,
message?: string,
) {
fireEvent.update(item, value);
await waitForInputHasValue(item, value, message);
}
80 changes: 80 additions & 0 deletions src/tests/components/InputStandard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { computed, ref } from "vue";
import { string } from "zod";
import InputStandard from "@/components/InputStandard.vue";
import { render, screen, waitFor } from "@testing-library/vue";
import { expect, test } from "vitest";
import { globErr } from "@/tools/views/ZodUtil";
import { setInputValueAndWait, waitForInputHasValue } from "../TestUtil";
import "@testing-library/jest-dom/vitest";

test("error message and regular label get set correctly", async () => {
const schema = string(globErr("my error")).min(5);
render(InputStandard, {
props: {
id: "inputStandard",
validationSchema: schema,
fieldLabel: "regular label",
},
});
const inputStandard = screen.getByTestId<HTMLInputElement>("inputStandard");
setInputValueAndWait(inputStandard, "1234");
await screen.findByLabelText("my error");
setInputValueAndWait(inputStandard, "12345");
await screen.findByLabelText("regular label");
});

test("computed schema also works", async () => {
const len = ref(5);
const schema = computed(() => string(globErr("my error")).min(len.value));
render(InputStandard, {
props: {
id: "inputStandard",
validationSchemaRef: schema,
fieldLabel: "regular label",
},
});
const inputStandard = screen.getByTestId<HTMLInputElement>("inputStandard");
setInputValueAndWait(inputStandard, "1234");
await screen.findByLabelText("my error");
setInputValueAndWait(inputStandard, "12345");
await screen.findByLabelText("regular label");
len.value = 6;
await screen.findByLabelText("my error");
});

test("untouched field shows regular label", async () => {
const schema = string(globErr("my error")).min(5);
render(InputStandard, {
props: {
id: "inputStandard",
validationSchema: schema,
fieldLabel: "regular label",
},
});
screen.getByTestId<HTMLInputElement>("inputStandard");
await screen.findByLabelText("regular label");
});

test("check that update:modelValue gets emitted", async () => {
const { emitted } = render(InputStandard, {
props: {
id: "inputStandard",
modelValue: "abcd",
},
});
const inputStandard = screen.getByTestId<HTMLInputElement>("inputStandard");
waitForInputHasValue(inputStandard, "abcd");
setInputValueAndWait(inputStandard, "1234");
expect((emitted()["update:modelValue"][0] as Array<string>)[0]).toBe("1234");
});

test("check focus", async () => {
render(InputStandard, {
props: {
id: "inputStandard",
focus: true,
},
});
const inputStandard = screen.getByTestId<HTMLInputElement>("inputStandard");
await waitFor(() => expect(document.activeElement).toBe(inputStandard));
});
20 changes: 1 addition & 19 deletions src/tests/views/moneyflow/CreateMoneyflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "@/stores/UserSessionStore";
import { createPinia, setActivePinia } from "pinia";
import "@testing-library/jest-dom/vitest";
import { setInputValueAndWait, waitForInputHasValue } from "@/tests/TestUtil";

vi.mock("@/service/PreDefMoneyflowService");
vi.mock("@/service/PostingAccountService");
Expand Down Expand Up @@ -461,16 +462,6 @@ const waitForOptionSelected = async (
});
};

const waitForInputHasValue = async (
item: HTMLInputElement,
value: string,
message?: string,
) => {
await waitFor(() => {
expect(item.value, message).toBe(value);
});
};

const selectOptionAndWait = async (
item: HTMLSelectElement,
value: string,
Expand All @@ -479,12 +470,3 @@ const selectOptionAndWait = async (
fireEvent.update(item, value);
await waitForOptionSelected(item, value, message);
};

const setInputValueAndWait = async (
item: HTMLInputElement,
value: string,
message?: string,
) => {
fireEvent.update(item, value);
await waitForInputHasValue(item, value, message);
};

0 comments on commit 94c0451

Please sign in to comment.