@vtex/faststore-plugin-buyer-portal 2.0.40 → 2.0.42

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.
@@ -0,0 +1,262 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { states } from "../data/states";
4
+
5
+ import {
6
+ applyHiddenNumberDefault,
7
+ applyNumberInput,
8
+ applyZipInput,
9
+ collectAddressIssues,
10
+ fieldsToClearOnCountryChange,
11
+ getCountryRule,
12
+ resolveCountryCode,
13
+ } from "./countryRules";
14
+
15
+ const t = (key: string) => key;
16
+
17
+ const baseValues = {
18
+ name: "HQ",
19
+ streetAddress: "Main St",
20
+ streetAddress2: "",
21
+ streetNumber: "100",
22
+ country: "Brazil",
23
+ countryCode: "BRA",
24
+ zip: "01310-100",
25
+ state: "SP",
26
+ city: "São Paulo",
27
+ types: ["Shipping"],
28
+ neighborhood: "Bela Vista",
29
+ };
30
+
31
+ describe("resolveCountryCode / getCountryRule", () => {
32
+ it("matches country code case-insensitively", () => {
33
+ expect(resolveCountryCode("bra")).toBe("BRA");
34
+ expect(getCountryRule("BRA").zip.renderFirst).toBe(true);
35
+ });
36
+
37
+ it("matches legacy display name Brazil", () => {
38
+ expect(resolveCountryCode("Brazil")).toBe("BRA");
39
+ expect(getCountryRule("Brazil").streetNumber.required).toBe(true);
40
+ });
41
+
42
+ it("returns fallback for unrecognized country", () => {
43
+ const rule = getCountryRule("Narnia");
44
+ expect(rule.zip.pattern).toBeNull();
45
+ expect(rule.streetNumber.visible).toBe(false);
46
+ expect(rule.state.required).toBe(true);
47
+ });
48
+
49
+ it("never returns null from getCountryRule", () => {
50
+ expect(getCountryRule(undefined)).toBeTruthy();
51
+ expect(getCountryRule("")).toBeTruthy();
52
+ });
53
+ });
54
+
55
+ describe("applyZipInput", () => {
56
+ it("formats Brazil CEP and keeps separators deletable", () => {
57
+ expect(applyZipInput("BRA", "01310")).toBe("01310");
58
+ expect(applyZipInput("BRA", "013101")).toBe("01310-1");
59
+ expect(applyZipInput("BRA", "01310100")).toBe("01310-100");
60
+ expect(applyZipInput("BRA", "01310-")).toBe("01310");
61
+ });
62
+
63
+ it("formats US ZIP and ZIP+4 without truncating the extension", () => {
64
+ expect(applyZipInput("USA", "12345")).toBe("12345");
65
+ expect(applyZipInput("USA", "123456789")).toBe("12345-6789");
66
+ });
67
+
68
+ it("formats Canada with space only after the third character", () => {
69
+ expect(applyZipInput("CAN", "A1A")).toBe("A1A");
70
+ expect(applyZipInput("CAN", "A1A1A1")).toBe("A1A 1A1");
71
+ });
72
+
73
+ it("truncates Mexico/France at 5 and Colombia at 6", () => {
74
+ expect(applyZipInput("MEX", "123456")).toBe("12345");
75
+ expect(applyZipInput("FRA", "750011")).toBe("75001");
76
+ expect(applyZipInput("COL", "1101112")).toBe("110111");
77
+ });
78
+
79
+ it("leaves unresolved countries unchanged", () => {
80
+ expect(applyZipInput("ZZZ", "ab-12")).toBe("ab-12");
81
+ });
82
+ });
83
+
84
+ describe("applyNumberInput", () => {
85
+ it("keeps alphanumeric values for Mexico", () => {
86
+ expect(applyNumberInput("MEX", "12A")).toBe("12A");
87
+ });
88
+
89
+ it("keeps letters for Brazil", () => {
90
+ expect(applyNumberInput("BRA", "12A")).toBe("12A");
91
+ });
92
+ });
93
+
94
+ describe("fieldsToClearOnCountryChange", () => {
95
+ it("always clears state; keeps typed number when both countries show it", () => {
96
+ expect(fieldsToClearOnCountryChange("MEX", "BRA", "10")).toEqual(["state"]);
97
+ });
98
+
99
+ it("clears number when next country hides it", () => {
100
+ expect(fieldsToClearOnCountryChange("MEX", "USA", "10")).toEqual([
101
+ "state",
102
+ "streetNumber",
103
+ ]);
104
+ });
105
+
106
+ it("clears auto-filled Colombia N/A when switching to Mexico", () => {
107
+ expect(fieldsToClearOnCountryChange("COL", "MEX", "N/A")).toEqual([
108
+ "state",
109
+ "streetNumber",
110
+ ]);
111
+ });
112
+
113
+ it("clears neighborhood when switching to France", () => {
114
+ expect(fieldsToClearOnCountryChange("MEX", "FRA", "12")).toEqual([
115
+ "state",
116
+ "streetNumber",
117
+ "neighborhood",
118
+ ]);
119
+ });
120
+ });
121
+
122
+ describe("collectAddressIssues", () => {
123
+ it("accepts a complete Brazilian address", () => {
124
+ expect(collectAddressIssues(baseValues, t)).toEqual([]);
125
+ });
126
+
127
+ it("accepts unformatted Brazilian CEP on edit", () => {
128
+ expect(collectAddressIssues({ ...baseValues, zip: "01310100" }, t)).toEqual(
129
+ []
130
+ );
131
+ });
132
+
133
+ it("accepts US ZIP+4", () => {
134
+ expect(
135
+ collectAddressIssues(
136
+ {
137
+ ...baseValues,
138
+ country: "United States of America",
139
+ countryCode: "USA",
140
+ zip: "12345-6789",
141
+ state: "CA",
142
+ streetNumber: "",
143
+ neighborhood: "",
144
+ },
145
+ t
146
+ )
147
+ ).toEqual([]);
148
+ });
149
+
150
+ it("allows France without state or neighborhood", () => {
151
+ expect(
152
+ collectAddressIssues(
153
+ {
154
+ ...baseValues,
155
+ country: "France",
156
+ countryCode: "FRA",
157
+ zip: "75001",
158
+ state: "",
159
+ neighborhood: "",
160
+ streetNumber: "",
161
+ },
162
+ t
163
+ )
164
+ ).toEqual([]);
165
+ });
166
+
167
+ it("allows Colombia without postal code and requires state", () => {
168
+ expect(
169
+ collectAddressIssues(
170
+ {
171
+ ...baseValues,
172
+ country: "Colombia",
173
+ countryCode: "COL",
174
+ zip: "",
175
+ streetNumber: "N/A",
176
+ neighborhood: "",
177
+ state: "Antioquia",
178
+ },
179
+ t
180
+ )
181
+ ).toEqual([]);
182
+ });
183
+
184
+ it("requires Mexico colonia and flags invalid short zip", () => {
185
+ const issues = collectAddressIssues(
186
+ {
187
+ ...baseValues,
188
+ country: "Mexico",
189
+ countryCode: "MEX",
190
+ zip: "1234",
191
+ streetNumber: "12",
192
+ neighborhood: "",
193
+ state: "Jalisco",
194
+ },
195
+ t
196
+ );
197
+ expect(issues.map((issue) => issue.path).sort()).toEqual([
198
+ "neighborhood",
199
+ "zip",
200
+ ]);
201
+ });
202
+
203
+ it("flags Brazilian postal format when incomplete", () => {
204
+ const issues = collectAddressIssues({ ...baseValues, zip: "0131" }, t);
205
+ expect(issues.some((issue) => issue.path === "zip")).toBe(true);
206
+ });
207
+
208
+ it("reports country rules even when another required field is empty", () => {
209
+ const issues = collectAddressIssues(
210
+ {
211
+ ...baseValues,
212
+ name: "",
213
+ country: "Mexico",
214
+ countryCode: "MEX",
215
+ zip: "1234",
216
+ streetNumber: "",
217
+ neighborhood: "",
218
+ state: "Jalisco",
219
+ },
220
+ t
221
+ );
222
+ const paths = issues.map((issue) => issue.path);
223
+ expect(paths).toContain("name");
224
+ expect(paths).toContain("zip");
225
+ expect(paths).toContain("neighborhood");
226
+ expect(paths).toContain("streetNumber");
227
+ });
228
+
229
+ it("does not require hidden number for Colombia", () => {
230
+ const issues = collectAddressIssues(
231
+ {
232
+ ...baseValues,
233
+ country: "Colombia",
234
+ countryCode: "COL",
235
+ zip: "",
236
+ streetNumber: "",
237
+ neighborhood: "",
238
+ state: "Antioquia",
239
+ },
240
+ t
241
+ );
242
+ expect(issues.map((issue) => issue.path)).not.toContain("streetNumber");
243
+ });
244
+ });
245
+
246
+ describe("applyHiddenNumberDefault", () => {
247
+ it("writes Colombia N/A only over an empty value", () => {
248
+ expect(applyHiddenNumberDefault("COL", "")).toBe("N/A");
249
+ expect(applyHiddenNumberDefault("COL", "42")).toBe("42");
250
+ });
251
+ });
252
+
253
+ describe("Colombia state list", () => {
254
+ it("includes 33 departments including Bogotá and the archipelago", () => {
255
+ expect(states.COL).toHaveLength(33);
256
+ expect(states.COL).toContain("Bogotá D.C.");
257
+ expect(states.COL).toContain(
258
+ "Archipiélago de San Andrés, Providencia y Santa Catalina"
259
+ );
260
+ expect(states.FRA).toEqual([]);
261
+ });
262
+ });
@@ -0,0 +1,399 @@
1
+ import { CountryOptions, type CountryCodes } from "../data/countries";
2
+
3
+ import type { AddressInput } from "../types";
4
+
5
+ export type SupportedCountryCode = CountryCodes;
6
+
7
+ export type CountryRule = {
8
+ zip: {
9
+ required: boolean;
10
+ pattern: RegExp | null;
11
+ renderFirst: boolean;
12
+ };
13
+ streetNumber: {
14
+ visible: boolean;
15
+ required: boolean;
16
+ digitsOnly: boolean;
17
+ defaultValue: string | null;
18
+ };
19
+ neighborhood: {
20
+ visible: boolean;
21
+ required: boolean;
22
+ labelKey: string;
23
+ };
24
+ state: {
25
+ visible: boolean;
26
+ required: boolean;
27
+ };
28
+ streetAddress: {
29
+ separated: boolean;
30
+ };
31
+ };
32
+
33
+ export type AddressIssue = {
34
+ path: string;
35
+ message: string;
36
+ };
37
+
38
+ export type AddressFormValues = Pick<
39
+ AddressInput,
40
+ | "name"
41
+ | "streetAddress"
42
+ | "streetAddress2"
43
+ | "streetNumber"
44
+ | "country"
45
+ | "countryCode"
46
+ | "zip"
47
+ | "state"
48
+ | "city"
49
+ | "types"
50
+ | "neighborhood"
51
+ >;
52
+
53
+ export type ClearableAddressField = "state" | "streetNumber" | "neighborhood";
54
+
55
+ const NEIGHBORHOOD_LABEL = "addresses.forms.neighborhood";
56
+ const COLONIA_LABEL = "addresses.forms.colonia";
57
+
58
+ const FALLBACK_RULE: CountryRule = {
59
+ zip: { required: true, pattern: null, renderFirst: false },
60
+ streetNumber: {
61
+ visible: false,
62
+ required: false,
63
+ digitsOnly: false,
64
+ defaultValue: null,
65
+ },
66
+ neighborhood: {
67
+ visible: true,
68
+ required: false,
69
+ labelKey: NEIGHBORHOOD_LABEL,
70
+ },
71
+ state: { visible: true, required: true },
72
+ streetAddress: { separated: false },
73
+ };
74
+
75
+ const COUNTRY_RULES: Record<SupportedCountryCode, CountryRule> = {
76
+ BRA: {
77
+ zip: {
78
+ required: true,
79
+ pattern: /^\d{5}-?\d{3}$/,
80
+ renderFirst: true,
81
+ },
82
+ streetNumber: {
83
+ visible: true,
84
+ required: true,
85
+ digitsOnly: false,
86
+ defaultValue: null,
87
+ },
88
+ neighborhood: {
89
+ visible: true,
90
+ required: true,
91
+ labelKey: NEIGHBORHOOD_LABEL,
92
+ },
93
+ state: { visible: true, required: true },
94
+ streetAddress: { separated: true },
95
+ },
96
+ USA: {
97
+ zip: {
98
+ required: true,
99
+ pattern: /^\d{5}(-\d{4})?$/,
100
+ renderFirst: false,
101
+ },
102
+ streetNumber: {
103
+ visible: false,
104
+ required: false,
105
+ digitsOnly: false,
106
+ defaultValue: null,
107
+ },
108
+ neighborhood: {
109
+ visible: true,
110
+ required: false,
111
+ labelKey: NEIGHBORHOOD_LABEL,
112
+ },
113
+ state: { visible: true, required: true },
114
+ streetAddress: { separated: false },
115
+ },
116
+ CAN: {
117
+ zip: {
118
+ required: true,
119
+ pattern: /^[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d$/,
120
+ renderFirst: false,
121
+ },
122
+ streetNumber: {
123
+ visible: false,
124
+ required: false,
125
+ digitsOnly: false,
126
+ defaultValue: null,
127
+ },
128
+ neighborhood: {
129
+ visible: true,
130
+ required: false,
131
+ labelKey: NEIGHBORHOOD_LABEL,
132
+ },
133
+ state: { visible: true, required: true },
134
+ streetAddress: { separated: false },
135
+ },
136
+ MEX: {
137
+ zip: {
138
+ required: true,
139
+ pattern: /^\d{5}$/,
140
+ renderFirst: false,
141
+ },
142
+ streetNumber: {
143
+ visible: true,
144
+ required: true,
145
+ digitsOnly: false,
146
+ defaultValue: null,
147
+ },
148
+ neighborhood: {
149
+ visible: true,
150
+ required: true,
151
+ labelKey: COLONIA_LABEL,
152
+ },
153
+ state: { visible: true, required: true },
154
+ streetAddress: { separated: true },
155
+ },
156
+ FRA: {
157
+ zip: {
158
+ required: true,
159
+ pattern: /^\d{5}$/,
160
+ renderFirst: false,
161
+ },
162
+ streetNumber: {
163
+ visible: false,
164
+ required: false,
165
+ digitsOnly: false,
166
+ defaultValue: null,
167
+ },
168
+ neighborhood: {
169
+ visible: false,
170
+ required: false,
171
+ labelKey: NEIGHBORHOOD_LABEL,
172
+ },
173
+ state: { visible: false, required: false },
174
+ streetAddress: { separated: false },
175
+ },
176
+ COL: {
177
+ zip: {
178
+ required: false,
179
+ pattern: /^\d{6}$/,
180
+ renderFirst: false,
181
+ },
182
+ streetNumber: {
183
+ visible: false,
184
+ required: false,
185
+ digitsOnly: false,
186
+ defaultValue: "N/A",
187
+ },
188
+ neighborhood: {
189
+ visible: true,
190
+ required: false,
191
+ labelKey: NEIGHBORHOOD_LABEL,
192
+ },
193
+ state: { visible: true, required: true },
194
+ streetAddress: { separated: false },
195
+ },
196
+ };
197
+
198
+ export function resolveCountryCode(
199
+ storedCountryValue: string | null | undefined
200
+ ): SupportedCountryCode | null {
201
+ const trimmed = storedCountryValue?.trim();
202
+ if (!trimmed) return null;
203
+
204
+ const byCode = CountryOptions.find(
205
+ (option) => option.id.toLowerCase() === trimmed.toLowerCase()
206
+ );
207
+ if (byCode) return byCode.id;
208
+
209
+ const byName = CountryOptions.find(
210
+ (option) => option.name.toLowerCase() === trimmed.toLowerCase()
211
+ );
212
+ if (byName) return byName.id;
213
+
214
+ return null;
215
+ }
216
+
217
+ export function getCountryRule(
218
+ storedCountryValue: string | null | undefined
219
+ ): CountryRule {
220
+ const code = resolveCountryCode(storedCountryValue);
221
+ if (!code) return FALLBACK_RULE;
222
+ return COUNTRY_RULES[code];
223
+ }
224
+
225
+ function digitsOnly(value: string): string {
226
+ return value.replace(/\D/g, "");
227
+ }
228
+
229
+ export function applyZipInput(
230
+ country: string | null | undefined,
231
+ typedValue: string
232
+ ): string {
233
+ const code = resolveCountryCode(country);
234
+ if (!code) return typedValue;
235
+
236
+ switch (code) {
237
+ case "BRA": {
238
+ const digits = digitsOnly(typedValue).slice(0, 8);
239
+ if (digits.length > 5) {
240
+ return `${digits.slice(0, 5)}-${digits.slice(5)}`;
241
+ }
242
+ return digits;
243
+ }
244
+ case "USA": {
245
+ const digits = digitsOnly(typedValue).slice(0, 9);
246
+ if (digits.length > 5) {
247
+ return `${digits.slice(0, 5)}-${digits.slice(5)}`;
248
+ }
249
+ return digits;
250
+ }
251
+ case "CAN": {
252
+ const raw = typedValue
253
+ .replace(/[^A-Za-z0-9]/g, "")
254
+ .toUpperCase()
255
+ .slice(0, 6);
256
+ if (raw.length > 3) {
257
+ return `${raw.slice(0, 3)} ${raw.slice(3)}`;
258
+ }
259
+ return raw;
260
+ }
261
+ case "MEX":
262
+ case "FRA":
263
+ return digitsOnly(typedValue).slice(0, 5);
264
+ case "COL":
265
+ return digitsOnly(typedValue).slice(0, 6);
266
+ default:
267
+ return typedValue;
268
+ }
269
+ }
270
+
271
+ export function applyNumberInput(
272
+ country: string | null | undefined,
273
+ typedValue: string
274
+ ): string {
275
+ const rule = getCountryRule(country);
276
+ if (rule.streetNumber.digitsOnly) {
277
+ return digitsOnly(typedValue);
278
+ }
279
+ return typedValue;
280
+ }
281
+
282
+ export function fieldsToClearOnCountryChange(
283
+ previousCountry: string | null | undefined,
284
+ nextCountry: string | null | undefined,
285
+ currentStreetNumber: string | null | undefined
286
+ ): ClearableAddressField[] {
287
+ const previousRule = getCountryRule(previousCountry);
288
+ const nextRule = getCountryRule(nextCountry);
289
+ const fields: ClearableAddressField[] = ["state"];
290
+
291
+ const previousDefault = previousRule.streetNumber.defaultValue;
292
+ const shouldClearNumber =
293
+ !nextRule.streetNumber.visible ||
294
+ (previousDefault != null &&
295
+ (currentStreetNumber ?? "").trim() === previousDefault);
296
+
297
+ if (shouldClearNumber) {
298
+ fields.push("streetNumber");
299
+ }
300
+
301
+ if (!nextRule.neighborhood.visible) {
302
+ fields.push("neighborhood");
303
+ }
304
+
305
+ return fields;
306
+ }
307
+
308
+ function isBlank(value: string | null | undefined): boolean {
309
+ return !value?.trim();
310
+ }
311
+
312
+ export function collectAddressIssues(
313
+ values: AddressFormValues,
314
+ t: (key: string) => string
315
+ ): AddressIssue[] {
316
+ const issues: AddressIssue[] = [];
317
+ const countryKey = values.countryCode || values.country;
318
+ const rule = getCountryRule(countryKey);
319
+
320
+ const pushRequired = (path: string, key: string) => {
321
+ issues.push({ path, message: t(key) });
322
+ };
323
+
324
+ if (isBlank(values.name)) {
325
+ pushRequired("name", "addresses.validation.addressNameRequired");
326
+ }
327
+ if (isBlank(values.city)) {
328
+ pushRequired("city", "addresses.validation.cityRequired");
329
+ }
330
+ if (isBlank(values.country) && isBlank(values.countryCode)) {
331
+ pushRequired("country", "addresses.validation.countryRequired");
332
+ }
333
+ if (!values.types?.length) {
334
+ pushRequired("types", "addresses.validation.addressTypeRequired");
335
+ }
336
+ if (isBlank(values.streetAddress)) {
337
+ pushRequired("streetAddress", "addresses.validation.streetAddressRequired");
338
+ }
339
+
340
+ if (rule.state.visible && rule.state.required && isBlank(values.state)) {
341
+ pushRequired("state", "addresses.validation.stateRequired");
342
+ }
343
+
344
+ if (rule.zip.required && isBlank(values.zip)) {
345
+ pushRequired("zip", "addresses.validation.postalCodeRequired");
346
+ } else if (
347
+ !isBlank(values.zip) &&
348
+ rule.zip.pattern &&
349
+ !rule.zip.pattern.test(values.zip!.trim())
350
+ ) {
351
+ issues.push({
352
+ path: "zip",
353
+ message: t("addresses.validation.postalCodeInvalid"),
354
+ });
355
+ }
356
+
357
+ if (
358
+ rule.streetNumber.visible &&
359
+ rule.streetNumber.required &&
360
+ isBlank(values.streetNumber)
361
+ ) {
362
+ pushRequired("streetNumber", "addresses.validation.numberRequired");
363
+ } else if (
364
+ rule.streetNumber.digitsOnly &&
365
+ !isBlank(values.streetNumber) &&
366
+ /\D/.test(values.streetNumber!.trim())
367
+ ) {
368
+ issues.push({
369
+ path: "streetNumber",
370
+ message: t("addresses.validation.numberInvalid"),
371
+ });
372
+ }
373
+
374
+ if (
375
+ rule.neighborhood.visible &&
376
+ rule.neighborhood.required &&
377
+ isBlank(values.neighborhood)
378
+ ) {
379
+ pushRequired(
380
+ "neighborhood",
381
+ rule.neighborhood.labelKey === COLONIA_LABEL
382
+ ? "addresses.validation.coloniaRequired"
383
+ : "addresses.validation.neighborhoodRequired"
384
+ );
385
+ }
386
+
387
+ return issues;
388
+ }
389
+
390
+ export function applyHiddenNumberDefault(
391
+ country: string | null | undefined,
392
+ currentStreetNumber: string | null | undefined
393
+ ): string {
394
+ const rule = getCountryRule(country);
395
+ if (rule.streetNumber.defaultValue && isBlank(currentStreetNumber)) {
396
+ return rule.streetNumber.defaultValue;
397
+ }
398
+ return currentStreetNumber ?? "";
399
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./stateUtils";
2
2
  export * from "./resolveAddressDetailsTab";
3
+ export * from "./countryRules";
@@ -6,6 +6,7 @@ import { useRouter } from "next/router";
6
6
  import { useUI } from "@faststore/ui";
7
7
 
8
8
  import { AddressForm } from "../../../addresses/components";
9
+ import { collectAddressIssues } from "../../../addresses/utils";
9
10
  import {
10
11
  type BasicDrawerProps,
11
12
  BasicDrawer,
@@ -178,20 +179,7 @@ export const CreateCreditCardDrawer = ({
178
179
  };
179
180
 
180
181
  const hasMinimumAddressInformation = () => {
181
- const isBrazil = address.countryCode === "BRA";
182
- const hasRequiredFields =
183
- address.name &&
184
- address.streetAddress &&
185
- address.country &&
186
- address.zip &&
187
- address.state &&
188
- address.city &&
189
- address.types.length > 0;
190
-
191
- const hasBrazilianNumber =
192
- !isBrazil || (isBrazil && address.streetNumber?.trim());
193
-
194
- return (hasRequiredFields && hasBrazilianNumber) || useExistingAddress;
182
+ return useExistingAddress || collectAddressIssues(address, t).length === 0;
195
183
  };
196
184
 
197
185
  const hasCardInformation = () => {
@@ -33,4 +33,4 @@ export const STOREFRONT_RESOURCE_NAMES = {
33
33
  export type StorefrontResourceName =
34
34
  (typeof STOREFRONT_RESOURCE_NAMES)[keyof typeof STOREFRONT_RESOURCE_NAMES];
35
35
 
36
- export const CURRENT_VERSION = "2.0.40";
36
+ export const CURRENT_VERSION = "2.0.42";