@jobber/components-native 0.95.0 → 0.95.1-CLEANUPfi-d275a8e.1

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-d275a8e.1+d275a8eca",
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": "d275a8ecaa05a97ddb522ad524ad3d5c2f5314b2"
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
+ });
@@ -8,7 +8,7 @@ import {
8
8
  waitFor,
9
9
  } from "@testing-library/react-native";
10
10
  import type { TextStyle } from "react-native";
11
- import { Platform } from "react-native";
11
+ import { Button, Platform } from "react-native";
12
12
  import { FormProvider, useForm } from "react-hook-form";
13
13
  import type { InputTextProps } from "./InputText";
14
14
  import { InputText } from "./InputText";
@@ -623,6 +623,120 @@ describe("InputText", () => {
623
623
  });
624
624
  });
625
625
  });
626
+
627
+ describe("with FormProvider", () => {
628
+ const mockOnSubmit = jest.fn();
629
+ const inputName = "testInput";
630
+ const inputAccessibilityLabel = "Test Input";
631
+ const saveButtonText = "Save";
632
+
633
+ function FormWithProvider({
634
+ defaultValue,
635
+ }: {
636
+ readonly defaultValue?: string;
637
+ }) {
638
+ const formMethods = useForm();
639
+
640
+ return (
641
+ <FormProvider {...formMethods}>
642
+ <InputText
643
+ name={inputName}
644
+ defaultValue={defaultValue}
645
+ accessibilityLabel={inputAccessibilityLabel}
646
+ />
647
+ <Button
648
+ onPress={formMethods.handleSubmit(values => mockOnSubmit(values))}
649
+ title={saveButtonText}
650
+ accessibilityLabel={saveButtonText}
651
+ />
652
+ </FormProvider>
653
+ );
654
+ }
655
+
656
+ beforeEach(() => {
657
+ mockOnSubmit.mockClear();
658
+ });
659
+
660
+ describe("defaultValue prop sets form value", () => {
661
+ it("sets form value to string when defaultValue is provided", async () => {
662
+ const testValue = "test value";
663
+ const { getByLabelText } = render(
664
+ <FormWithProvider defaultValue={testValue} />,
665
+ );
666
+
667
+ const saveButton = getByLabelText(saveButtonText);
668
+ await waitFor(() => {
669
+ fireEvent.press(saveButton);
670
+ });
671
+
672
+ expect(mockOnSubmit).toHaveBeenCalledWith({
673
+ [inputName]: testValue,
674
+ });
675
+ });
676
+
677
+ it("sets form value to undefined when defaultValue is undefined", async () => {
678
+ const { getByLabelText } = render(<FormWithProvider />);
679
+
680
+ const saveButton = getByLabelText(saveButtonText);
681
+ await waitFor(() => {
682
+ fireEvent.press(saveButton);
683
+ });
684
+
685
+ expect(mockOnSubmit).toHaveBeenCalledWith({
686
+ [inputName]: undefined,
687
+ });
688
+ });
689
+
690
+ it("sets form value to undefined when defaultValue is empty string", async () => {
691
+ const { getByLabelText } = render(<FormWithProvider defaultValue="" />);
692
+
693
+ const saveButton = getByLabelText(saveButtonText);
694
+ await waitFor(() => {
695
+ fireEvent.press(saveButton);
696
+ });
697
+
698
+ expect(mockOnSubmit).toHaveBeenCalledWith({
699
+ [inputName]: undefined,
700
+ });
701
+ });
702
+ });
703
+
704
+ describe("input value updates form value", () => {
705
+ it("updates form value when input text is changed", async () => {
706
+ const { getByLabelText } = render(
707
+ <FormWithProvider defaultValue="initial" />,
708
+ );
709
+
710
+ const input = getByLabelText(inputAccessibilityLabel);
711
+ fireEvent.changeText(input, "new value");
712
+
713
+ const saveButton = getByLabelText(saveButtonText);
714
+ await waitFor(() => {
715
+ fireEvent.press(saveButton);
716
+ });
717
+
718
+ expect(mockOnSubmit).toHaveBeenCalledWith({
719
+ [inputName]: "new value",
720
+ });
721
+ });
722
+
723
+ it("preserves defaultValue when input is not interacted with", async () => {
724
+ const testValue = "preserved value";
725
+ const { getByLabelText } = render(
726
+ <FormWithProvider defaultValue={testValue} />,
727
+ );
728
+
729
+ const saveButton = getByLabelText(saveButtonText);
730
+ await waitFor(() => {
731
+ fireEvent.press(saveButton);
732
+ });
733
+
734
+ expect(mockOnSubmit).toHaveBeenCalledWith({
735
+ [inputName]: testValue,
736
+ });
737
+ });
738
+ });
739
+ });
626
740
  });
627
741
 
628
742
  describe("Transform", () => {
@@ -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: typeof value === "boolean" ? value : 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".