Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/mobile/app/(app)/clients/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function NewClientScreen() {
setErrors(result.errors);
showErrorToast(
"Review highlighted fields",
"First and last name are required.",
"Correct the highlighted fields and try again.",
);
return;
}
Expand All @@ -90,7 +90,7 @@ export default function NewClientScreen() {
<>
<Stack.Screen
options={{
gestureEnabled: !isDirty,
gestureEnabled: true,
headerShown: false,
presentation: "formSheet",
sheetAllowedDetents: [0.93],
Expand Down
58 changes: 54 additions & 4 deletions apps/mobile/src/components/__tests__/client-creation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { Alert } from "react-native";

import { act, fireEvent, renderWithTheme, screen, waitFor } from "@/test-utils";

Expand All @@ -15,9 +16,7 @@ jest.mock("expo-router", () => {
dispatch: jest.fn(),
setOptions: jest.fn(),
};
MockStack.Screen = function MockStackScreen() {
return null;
};
MockStack.Screen = jest.fn(() => null);
return {
__esModule: true,
router: {
Expand Down Expand Up @@ -84,6 +83,10 @@ describe("NewClientScreen", () => {
mockDb.select.mockReturnValue({ from: mockFrom });
});

afterEach(() => {
jest.restoreAllMocks();
});

it("inserts a minimal client and dismisses only after persistence succeeds", async () => {
renderWithTheme(<NewClientScreen />);
enterRequiredNames();
Expand All @@ -110,13 +113,60 @@ describe("NewClientScreen", () => {

expect(mockShowErrorToast).toHaveBeenCalledWith(
"Review highlighted fields",
"First and last name are required.",
"Correct the highlighted fields and try again.",
);
expect(screen.getByText("First name is required.")).toBeTruthy();
expect(screen.getByText("Last name is required.")).toBeTruthy();
expect(mockValues).not.toHaveBeenCalled();
});

it("uses generic toast guidance for non-name validation errors", () => {
renderWithTheme(<NewClientScreen />);
enterRequiredNames();
fireEvent.press(screen.getByLabelText("Expand Clinical section"));
fireEvent.changeText(screen.getByLabelText("Gravida"), "1");
fireEvent.changeText(screen.getByLabelText("Parity"), "2");

fireEvent.press(screen.getByText("Add client"));

expect(mockShowErrorToast).toHaveBeenCalledWith(
"Review highlighted fields",
"Correct the highlighted fields and try again.",
);
expect(
screen.getByText("Parity cannot be greater than gravida."),
).toBeTruthy();
expect(mockValues).not.toHaveBeenCalled();
});

it("keeps native dismissal enabled and confirms dirty navigation attempts", () => {
const alertSpy = jest.spyOn(Alert, "alert").mockImplementation();
renderWithTheme(<NewClientScreen />);

const screenOptions =
mockExpoRouter.Stack.Screen.mock.calls.at(-1)[0].options;
expect(screenOptions.gestureEnabled).toBe(true);

fireEvent.changeText(screen.getByLabelText("First name"), "Zara");

const beforeRemove = mockExpoRouter.navigation.addListener.mock.calls
.filter(([eventName]: [string]) => eventName === "beforeRemove")
.at(-1)[1];
const action = { type: "GO_BACK" };
const event = { data: { action }, preventDefault: jest.fn() };
act(() => beforeRemove(event));

expect(event.preventDefault).toHaveBeenCalledTimes(1);
expect(alertSpy).toHaveBeenCalledWith(
"Discard this client?",
"Your changes have not been saved.",
expect.any(Array),
);
const buttons = alertSpy.mock.calls[0][2];
act(() => buttons?.[1].onPress?.());
expect(mockExpoRouter.navigation.dispatch).toHaveBeenCalledWith(action);
});

it("lets nullable clinical choices return to an unset value", async () => {
renderWithTheme(<NewClientScreen />);
enterRequiredNames();
Expand Down
Loading