Skip to content

Commit

Permalink
Merge pull request #12 from davitorress/devops/testing
Browse files Browse the repository at this point in the history
add register form tests
  • Loading branch information
davitorress authored Mar 20, 2024
2 parents a434973 + 1f36168 commit 1ff29f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
39 changes: 39 additions & 0 deletions __tests__/forms/register.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react"
import { render, fireEvent, act } from "@testing-library/react-native"
import RegisterForm from "../../components/forms/RegisterForm"

test("submitting form with valid data should call onSubmit", async () => {
const onSubmit = jest.fn()
const { getByPlaceholderText } = render(<RegisterForm onSubmit={onSubmit} />)

const nameValueTest = "John Doe"
const emailValueTest = "john.doe@example.com"
const passwordValueTest = "password123"

const nameInput = getByPlaceholderText("Digite o seu nome completo")
const emailInput = getByPlaceholderText("Digite o seu e-mail")
const passwordInput = getByPlaceholderText("Digite a sua senha")
const confirmPasswordInput = getByPlaceholderText("Digite novamente a sua senha")

fireEvent.changeText(nameInput, nameValueTest)
fireEvent.changeText(emailInput, emailValueTest)
fireEvent.changeText(passwordInput, passwordValueTest)
fireEvent.changeText(confirmPasswordInput, passwordValueTest)

await act(async () => {
await onSubmit({
name: nameValueTest,
email: emailValueTest,
password: passwordValueTest,
confirmPassword: passwordValueTest,
})
})

expect(onSubmit).toHaveBeenCalledTimes(1)
expect(onSubmit).toHaveBeenCalledWith({
name: nameValueTest,
email: emailValueTest,
password: passwordValueTest,
confirmPassword: passwordValueTest,
})
})
11 changes: 9 additions & 2 deletions app/register.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { ScrollView, View } from "react-native"
import { useRouter } from "expo-router"
import { SafeAreaView } from "react-native-safe-area-context"

import RegisterForm from "@/components/forms/RegisterForm"
import ImageWithPlaceholder from "@/components/basic/ImageWithPlaceholder"

export default function RegisterScreen() {
const router = useRouter()

const handleFormSubmit = (data: unknown) => {
console.log(data)
router.navigate("/(tabs)/")
}

return (
<SafeAreaView className="m-0 flex-1 bg-white">
<ScrollView>
Expand All @@ -16,7 +23,7 @@ export default function RegisterScreen() {
/>

<View className="w-full mt-12 px-4">
<RegisterForm />
<RegisterForm onSubmit={handleFormSubmit} />
</View>
</View>
</ScrollView>
Expand Down
7 changes: 1 addition & 6 deletions components/forms/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const registerSchema = z

type RegisterData = z.infer<typeof registerSchema>

export default function RegisterForm() {
export default function RegisterForm({ onSubmit }: { onSubmit: (data: RegisterData) => void }) {
const router = useRouter()

const {
Expand All @@ -47,11 +47,6 @@ export default function RegisterForm() {
resolver: zodResolver(registerSchema),
})

const onSubmit = (data: RegisterData) => {
console.log(data)
router.navigate("/(tabs)/")
}

return (
<View className="w-full">
<View className="w-full">
Expand Down

0 comments on commit 1ff29f6

Please sign in to comment.