@jobber/components-native 0.95.0 → 0.95.1-CLEANUPfi-c27afbd.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jobber/components-native",
3
- "version": "0.95.0",
3
+ "version": "0.95.1-CLEANUPfi-c27afbd.0+c27afbdf1",
4
4
  "license": "MIT",
5
5
  "description": "React Native implementation of Atlantis",
6
6
  "repository": {
@@ -96,5 +96,5 @@
96
96
  "react-native-safe-area-context": "^5.4.0",
97
97
  "react-native-svg": ">=12.0.0"
98
98
  },
99
- "gitHead": "ea9cbb709f05442a469763a4f12a332a59cb6350"
99
+ "gitHead": "c27afbdf1236cd8a23a64ef0be784c7b0f793ddb"
100
100
  }
@@ -1,5 +1,7 @@
1
1
  import React from "react";
2
- import { fireEvent, render } from "@testing-library/react-native";
2
+ import { fireEvent, render, waitFor } from "@testing-library/react-native";
3
+ import { FormProvider, useForm } from "react-hook-form";
4
+ import { Button } from "react-native";
3
5
  import { Checkbox } from "./Checkbox";
4
6
 
5
7
  const accessibilityLabel = "testA11y";
@@ -102,3 +104,118 @@ describe("Checkbox", () => {
102
104
  expect(getByTestId("minus2")).toBeDefined();
103
105
  });
104
106
  });
107
+
108
+ describe("Checkbox with FormProvider", () => {
109
+ const mockOnSubmit = jest.fn();
110
+ const checkboxName = "testCheckbox";
111
+ const checkboxLabel = "Test Checkbox";
112
+ const saveButtonText = "Save";
113
+
114
+ function FormWithProvider({
115
+ defaultChecked,
116
+ }: {
117
+ readonly defaultChecked?: boolean;
118
+ }) {
119
+ const formMethods = useForm();
120
+
121
+ return (
122
+ <FormProvider {...formMethods}>
123
+ <Checkbox
124
+ name={checkboxName}
125
+ label={checkboxLabel}
126
+ defaultChecked={defaultChecked}
127
+ accessibilityLabel={accessibilityLabel}
128
+ />
129
+ <Button
130
+ onPress={formMethods.handleSubmit(values => mockOnSubmit(values))}
131
+ title={saveButtonText}
132
+ accessibilityLabel={saveButtonText}
133
+ />
134
+ </FormProvider>
135
+ );
136
+ }
137
+
138
+ beforeEach(() => {
139
+ mockOnSubmit.mockClear();
140
+ });
141
+
142
+ describe("defaultChecked prop sets form value", () => {
143
+ it("sets form value to true when defaultChecked is true", async () => {
144
+ const { getByLabelText } = render(
145
+ <FormWithProvider defaultChecked={true} />,
146
+ );
147
+
148
+ const saveButton = getByLabelText(saveButtonText);
149
+ await waitFor(() => {
150
+ fireEvent.press(saveButton);
151
+ });
152
+
153
+ expect(mockOnSubmit).toHaveBeenCalledWith({
154
+ [checkboxName]: true,
155
+ });
156
+ });
157
+
158
+ it("sets form value to false when defaultChecked is false", async () => {
159
+ const { getByLabelText } = render(
160
+ <FormWithProvider defaultChecked={false} />,
161
+ );
162
+
163
+ const saveButton = getByLabelText(saveButtonText);
164
+ await waitFor(() => {
165
+ fireEvent.press(saveButton);
166
+ });
167
+
168
+ expect(mockOnSubmit).toHaveBeenCalledWith({
169
+ [checkboxName]: false,
170
+ });
171
+ });
172
+
173
+ it("sets form value to undefined when defaultChecked is undefined", async () => {
174
+ const { getByLabelText } = render(<FormWithProvider />);
175
+
176
+ const saveButton = getByLabelText(saveButtonText);
177
+ await waitFor(() => {
178
+ fireEvent.press(saveButton);
179
+ });
180
+
181
+ expect(mockOnSubmit).toHaveBeenCalledWith({
182
+ [checkboxName]: undefined,
183
+ });
184
+ });
185
+ });
186
+
187
+ describe("checkbox value updates form value", () => {
188
+ it("updates form value when checkbox is toggled", async () => {
189
+ const { getByLabelText } = render(
190
+ <FormWithProvider defaultChecked={false} />,
191
+ );
192
+
193
+ const checkbox = getByLabelText(accessibilityLabel);
194
+ fireEvent.press(checkbox);
195
+
196
+ const saveButton = getByLabelText(saveButtonText);
197
+ await waitFor(() => {
198
+ fireEvent.press(saveButton);
199
+ });
200
+
201
+ expect(mockOnSubmit).toHaveBeenCalledWith({
202
+ [checkboxName]: true,
203
+ });
204
+ });
205
+
206
+ it("preserves defaultChecked value when checkbox is not interacted with", async () => {
207
+ const { getByLabelText } = render(
208
+ <FormWithProvider defaultChecked={true} />,
209
+ );
210
+
211
+ const saveButton = getByLabelText(saveButtonText);
212
+ await waitFor(() => {
213
+ fireEvent.press(saveButton);
214
+ });
215
+
216
+ expect(mockOnSubmit).toHaveBeenCalledWith({
217
+ [checkboxName]: true,
218
+ });
219
+ });
220
+ });
221
+ });
@@ -40,7 +40,7 @@ export function useFormController<T>({
40
40
  name: fieldName,
41
41
  control,
42
42
  rules: validations,
43
- defaultValue: value || undefined,
43
+ defaultValue: value ?? undefined,
44
44
  });
45
45
 
46
46
  // The naming convention established by react-hook-form for arrays of fields is, for example, "emails.0.description".