Skip to content

Commit

Permalink
chore: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeppner-dataport committed Nov 6, 2023
1 parent d60b64c commit 00c6071
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ describe("LinkContentElementCreate", () => {
textAreaComponent.vm.$emit("input", url);
};

const submit = async () => {
const submitByClick = async () => {
const button = wrapper.find("button");
await button.trigger("click");
await nextTick();
};

const submitByEnter = async () => {
const textAreaComponent = wrapper.findComponent({ name: "v-textarea" });
textAreaComponent.vm.$emit(
"keydown",
Expand All @@ -51,6 +54,7 @@ describe("LinkContentElementCreate", () => {
keyCode: 13,
})
);
await nextTick();
};

const hasEmitted = (eventName: string): string | false => {
Expand All @@ -65,28 +69,34 @@ describe("LinkContentElementCreate", () => {
return typeof rulesProperty === "function";
};

return { wrapper, insertUrl, submit, hasEmitted, areRulesActive };
return {
wrapper,
insertUrl,
submitByClick,
submitByEnter,
hasEmitted,
areRulesActive,
};
};

describe("when valid url was entered", () => {
describe("when enter is pressed", () => {
it("should not show error-message", async () => {
const { wrapper, insertUrl, submit } = setup();
const { wrapper, insertUrl, submitByClick } = setup();

insertUrl(VALID_URL);
await submit();
await nextTick();
await submitByClick();

const alerts = wrapper.find('[role="alert"]');

expect(alerts.exists()).toBe(false);
});

it("should emit create:url event", async () => {
const { insertUrl, submit, hasEmitted } = setup();
const { insertUrl, submitByEnter, hasEmitted } = setup();

insertUrl(VALID_URL);
await submit();
await submitByEnter();

expect(hasEmitted("create:url")).toEqual(VALID_URL);
});
Expand All @@ -107,10 +117,10 @@ describe("LinkContentElementCreate", () => {

describe("when enter is pressed", () => {
it("should show invalid-url-error", async () => {
const { wrapper, insertUrl, submit } = setup();
const { wrapper, insertUrl, submitByEnter } = setup();

insertUrl(INVALID_URL);
await submit();
await submitByEnter();

const alerts = wrapper.find('[role="alert"]').text();

Expand All @@ -120,10 +130,10 @@ describe("LinkContentElementCreate", () => {
});

it("should not emit create:url event", async () => {
const { wrapper, insertUrl, submit } = setup();
const { wrapper, insertUrl, submitByEnter } = setup();

insertUrl(INVALID_URL);
await submit();
await submitByEnter();

const emitted = wrapper.emitted("create:url");
expect(emitted).toBeUndefined();
Expand All @@ -134,9 +144,9 @@ describe("LinkContentElementCreate", () => {
describe("when url field is empty", () => {
describe("when submit button is clicked", () => {
it("should show required-error-message", async () => {
const { wrapper, submit } = setup();
const { wrapper, submitByEnter } = setup();

await submit();
await submitByEnter();

const alerts = wrapper.find('[role="alert"]').text();
expect(alerts).toEqual(
Expand All @@ -145,9 +155,9 @@ describe("LinkContentElementCreate", () => {
});

it("should not emit create:url event", async () => {
const { wrapper, submit } = setup();
const { wrapper, submitByClick } = setup();

await submit();
await submitByClick();

const emitted = wrapper.emitted("create:url");
expect(emitted).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<template>
<v-card-text>
<v-form
@submit.prevent="onSubmit()"
ref="form"
:lazy-validation="true"
validate-on="submit"
>
<v-form @submit.prevent.stop="onSubmit" ref="form" :lazy-validation="true">
<div class="d-flex flex-row">
<v-textarea
v-model="url"
Expand Down Expand Up @@ -61,8 +56,8 @@ export default defineComponent({
const rules = computed(() => {
if (isValidationActive.value === true) {
return [
isValidUrl(t("util-validators-invalid-url")),
isRequired(t("common.validation.required2")),
isValidUrl(t("util-validators-invalid-url")),
];
}
return [];
Expand Down

0 comments on commit 00c6071

Please sign in to comment.