mirror of
https://github.com/coder/coder.git
synced 2026-11-13 05:16:51 +08:00
This just implements a basic sign-in flow, using the new endpoints in #29 :  This brings over several dependencies that are necessary: - `formik` - `yep` Ports over some v1 code to bootstrap it: - `FormTextField` - `PasswordField` - `CoderIcon` And implements basic sign-in: Fixes #37 Fixes #43 This does not implement it navbar integration (importantly - there is no way to sign out yet, unless you manually delete your `session_token`). I'll do that in the next PR - figured this was big enough to get reviewed.
78 lines
1.9 KiB
TypeScript
78 lines
1.9 KiB
TypeScript
import { act, fireEvent, render, screen } from "@testing-library/react"
|
|
import { useFormik } from "formik"
|
|
import React from "react"
|
|
import * as yup from "yup"
|
|
import { formTextFieldFactory, FormTextFieldProps } from "./FormTextField"
|
|
|
|
namespace Helpers {
|
|
export interface FormValues {
|
|
name: string
|
|
}
|
|
|
|
export const requiredValidationMsg = "required"
|
|
|
|
const FormTextField = formTextFieldFactory<FormValues>()
|
|
|
|
export const Component: React.FC<Omit<FormTextFieldProps<FormValues>, "form" | "formFieldName">> = (props) => {
|
|
const form = useFormik<FormValues>({
|
|
initialValues: {
|
|
name: "",
|
|
},
|
|
onSubmit: (values, helpers) => {
|
|
return helpers.setSubmitting(false)
|
|
},
|
|
validationSchema: yup.object({
|
|
name: yup.string().required(requiredValidationMsg),
|
|
}),
|
|
})
|
|
|
|
return <FormTextField {...props} form={form} formFieldName="name" />
|
|
}
|
|
}
|
|
|
|
describe("FormTextField", () => {
|
|
describe("helperText", () => {
|
|
it("uses helperText prop when there are no errors", () => {
|
|
// Given
|
|
const props = {
|
|
helperText: "testing",
|
|
}
|
|
|
|
// When
|
|
const { queryByText } = render(<Helpers.Component {...props} />)
|
|
|
|
// Then
|
|
expect(queryByText(props.helperText)).toBeDefined()
|
|
})
|
|
|
|
it("uses validation message when there are errors", () => {
|
|
// Given
|
|
const props = {}
|
|
|
|
// When
|
|
const { container } = render(<Helpers.Component {...props} />)
|
|
const el = container.firstChild
|
|
|
|
// Then
|
|
expect(el).toBeDefined()
|
|
expect(screen.queryByText(Helpers.requiredValidationMsg)).toBeNull()
|
|
|
|
// When
|
|
act(() => {
|
|
fireEvent.focus(el as Element)
|
|
})
|
|
|
|
// Then
|
|
expect(screen.queryByText(Helpers.requiredValidationMsg)).toBeNull()
|
|
|
|
// When
|
|
act(() => {
|
|
fireEvent.blur(el as Element)
|
|
})
|
|
|
|
// Then
|
|
expect(screen.queryByText(Helpers.requiredValidationMsg)).toBeDefined()
|
|
})
|
|
})
|
|
})
|