Skip to content

Commit

Permalink
fix(auth form): initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Aug 18, 2024
1 parent 7ab9642 commit b392626
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
56 changes: 53 additions & 3 deletions src/components/organisms/AtAuthForm/AtAuthForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import userEvent from "@testing-library/user-event";
import { render, screen, fireEvent } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import { reactive } from "vue";
import { reactive, ref } from "vue";
import AuthForm from "./AtAuthForm.vue";

describe("AuthForm component", () => {
Expand Down Expand Up @@ -92,9 +92,57 @@ describe("AuthForm component", () => {
});
});

it("Should render the component register", async () => {
it("Should change email on initial value change", async () => {
const initialValues = ref({
email: "",
});

const email = "[email protected]";

const component = render(AuthForm, {
props: {
mode: "register",
initialValues: initialValues,
config: {
email: {
disabled: true
}
}
},
});

initialValues.value.email = email;
const inputEmail = await component.findByTestId("input-email");
expect(inputEmail.value).toBe(email);
});
it("Should disable email on config set", async () => {
const initialValues = ref({
email: "",
});

const email = "[email protected]";

const component = render(AuthForm, {
props: {
mode: "register",
initialValues: initialValues,
config: {
email: {
disabled: true
}
}
},
});

initialValues.value.email = email;
const inputEmail = await component.findByTestId("input-email");
await userEvent.type(inputEmail, "[email protected]")
expect(inputEmail.value).toBe(email);
});

it("Should disable email after initialization in register", async () => {
const initialValues = reactive({
email: "[email protected]",
email: "",
});

const component = render(AuthForm, {
Expand All @@ -103,6 +151,8 @@ describe("AuthForm component", () => {
},
});

initialValues.email = "[email protected]"

component.findByText("[email protected]");
initialValues.email = "[email protected]";
component.findByText("[email protected]");
Expand Down
40 changes: 20 additions & 20 deletions src/components/organisms/AtAuthForm/AtAuthForm.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<template>
<form @submit.prevent="loginUser()">
<div
class="flex items-center justify-center w-full"
:class="brandContainerClass"
>
<div class="flex items-center justify-center w-full" :class="brandContainerClass">
<div
class="text-6xl text-center cursor-pointer brand-font"
@click="$emit('home-pressed')"
Expand All @@ -20,6 +17,8 @@
<component
:is="AtInput"
v-model.trim="formData.email"
v-bind="getFieldConfig('email')"
:disabled="true"
data-testid="input-email"
required
/>
Expand All @@ -28,12 +27,7 @@
</template>
</component>

<component
:is="AtField"
field="password"
label="Password"
:errors="errors"
>
<component :is="AtField" field="password" label="Password" :errors="errors">
<component
:is="AtInputPassword"
data-testid="input-password"
Expand Down Expand Up @@ -103,7 +97,7 @@
</form>
</template>

<script setup>
<script setup lang="ts">
import { reactive, computed, toRefs, provide, watch } from "vue";
import AtButton from "../../atoms/AtButton/AtButton.vue";
import AtField from "../../atoms/AtField/AtField.vue";
Expand All @@ -113,6 +107,8 @@ import AtInputPassword from "../../molecules/AtInputPassword/AtInputPassword.vue
const emit = defineEmits({
submit: null,
"link-pressed": null,
"home-pressed": null
});
const props = defineProps({
Expand Down Expand Up @@ -209,10 +205,7 @@ const state = reactive({
}),
linkLabel: computed(() => {
return (
props.customLinkLabel ??
(mode.value == "register" ? "Login" : "Create One")
);
return props.customLinkLabel ?? (mode.value == "register" ? "Login" : "Create One");
}),
// validation
isDirty: false,
Expand All @@ -225,21 +218,28 @@ const state = reactive({
}),
});
const getFieldConfig = (field: string) => {
return props.config?.[field] ?? {};
}
watch(
initialValues,
(formValues) => {
Object.keys(state.formData).forEach((key) => {
const fieldConfig = props.config?.[key];
if (
fieldConfig &&
state.formData[key] !== formValues[fieldConfig.mapper || key]
) {
state.formData[key] = formValues[fieldConfig.mapper || key];
let fieldName = key
if (fieldConfig && state.formData[key] !== formValues[fieldConfig.mapper || key]) {
fieldName = fieldConfig.mapper || key
}
state.formData[key] = formValues[fieldName] ?? "";
});
},
{
immediate: true,
deep: true
}
);
Expand Down

0 comments on commit b392626

Please sign in to comment.