@terreno/ui 57.19.2 → 57.20.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/.ai/guidelines/core.md +1 -1
- package/CHANGELOG.md +393 -0
- package/dist/Accordion.d.ts.map +1 -1
- package/dist/Accordion.js +3 -3
- package/dist/Accordion.js.map +1 -1
- package/dist/Avatar.d.ts.map +1 -1
- package/dist/Avatar.js +3 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/Box.d.ts.map +1 -1
- package/dist/Box.js +38 -2
- package/dist/Box.js.map +1 -1
- package/dist/Card.js +2 -2
- package/dist/Card.js.map +1 -1
- package/dist/Common.d.ts +120 -7
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.d.ts +1 -1
- package/dist/ConsentFormScreen.d.ts.map +1 -1
- package/dist/ConsentFormScreen.js +13 -4
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +5 -3
- package/dist/DateTimeField.js.map +1 -1
- package/dist/Field.d.ts.map +1 -1
- package/dist/Field.js +2 -0
- package/dist/Field.js.map +1 -1
- package/dist/FormContainer.d.ts +68 -0
- package/dist/FormContainer.d.ts.map +1 -0
- package/dist/FormContainer.js +79 -0
- package/dist/FormContainer.js.map +1 -0
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +2 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts +1 -1
- package/dist/Heading.d.ts.map +1 -1
- package/dist/Heading.js +5 -2
- package/dist/Heading.js.map +1 -1
- package/dist/Icon.d.ts +4 -0
- package/dist/Icon.d.ts.map +1 -1
- package/dist/Icon.js +43 -2
- package/dist/Icon.js.map +1 -1
- package/dist/LabeledCheckBox.d.ts +22 -0
- package/dist/LabeledCheckBox.d.ts.map +1 -0
- package/dist/LabeledCheckBox.js +30 -0
- package/dist/LabeledCheckBox.js.map +1 -0
- package/dist/ListDetailLayout.d.ts +36 -0
- package/dist/ListDetailLayout.d.ts.map +1 -0
- package/dist/ListDetailLayout.js +153 -0
- package/dist/ListDetailLayout.js.map +1 -0
- package/dist/MarkdownEditor.d.ts.map +1 -1
- package/dist/MarkdownEditor.js +2 -1
- package/dist/MarkdownEditor.js.map +1 -1
- package/dist/MediaQuery.d.ts +1 -1
- package/dist/MediaQuery.d.ts.map +1 -1
- package/dist/MediaQuery.js +1 -1
- package/dist/MediaQuery.js.map +1 -1
- package/dist/OtpField.d.ts +60 -0
- package/dist/OtpField.d.ts.map +1 -0
- package/dist/OtpField.js +154 -0
- package/dist/OtpField.js.map +1 -0
- package/dist/ProvisionalConsentSigner.d.ts +21 -0
- package/dist/ProvisionalConsentSigner.d.ts.map +1 -0
- package/dist/ProvisionalConsentSigner.js +199 -0
- package/dist/ProvisionalConsentSigner.js.map +1 -0
- package/dist/ResponsiveBreakpoint.d.ts +5 -0
- package/dist/ResponsiveBreakpoint.d.ts.map +1 -1
- package/dist/ResponsiveBreakpoint.js +7 -0
- package/dist/ResponsiveBreakpoint.js.map +1 -1
- package/dist/SocialLoginButton.d.ts +10 -0
- package/dist/SocialLoginButton.d.ts.map +1 -1
- package/dist/SocialLoginButton.js +26 -8
- package/dist/SocialLoginButton.js.map +1 -1
- package/dist/SplitPage.d.ts +11 -0
- package/dist/SplitPage.d.ts.map +1 -1
- package/dist/SplitPage.js +11 -5
- package/dist/SplitPage.js.map +1 -1
- package/dist/SplitPage.native.d.ts.map +1 -1
- package/dist/SplitPage.native.js +3 -1
- package/dist/SplitPage.native.js.map +1 -1
- package/dist/SyncStatusBanner.d.ts +7 -1
- package/dist/SyncStatusBanner.d.ts.map +1 -1
- package/dist/SyncStatusBanner.js +20 -4
- package/dist/SyncStatusBanner.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +44 -10
- package/dist/TextField.js.map +1 -1
- package/dist/fieldElements/FieldError.d.ts.map +1 -1
- package/dist/fieldElements/FieldError.js +1 -1
- package/dist/fieldElements/FieldError.js.map +1 -1
- package/dist/index.d.ts +19 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +3 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +2 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/login/LastUsedBadge.d.ts +14 -0
- package/dist/login/LastUsedBadge.d.ts.map +1 -0
- package/dist/login/LastUsedBadge.js +23 -0
- package/dist/login/LastUsedBadge.js.map +1 -0
- package/dist/login/LoginAccountCard.d.ts +38 -0
- package/dist/login/LoginAccountCard.d.ts.map +1 -0
- package/dist/login/LoginAccountCard.js +72 -0
- package/dist/login/LoginAccountCard.js.map +1 -0
- package/dist/login/LoginAccountList.d.ts +33 -0
- package/dist/login/LoginAccountList.d.ts.map +1 -0
- package/dist/login/LoginAccountList.js +30 -0
- package/dist/login/LoginAccountList.js.map +1 -0
- package/dist/login/LoginScreen.d.ts +4 -4
- package/dist/login/LoginScreen.d.ts.map +1 -1
- package/dist/login/LoginScreen.js +294 -13
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/login/OutlinePill.d.ts +28 -0
- package/dist/login/OutlinePill.d.ts.map +1 -0
- package/dist/login/OutlinePill.js +48 -0
- package/dist/login/OutlinePill.js.map +1 -0
- package/dist/login/TwoFactorChallengeScreen.d.ts +26 -0
- package/dist/login/TwoFactorChallengeScreen.d.ts.map +1 -0
- package/dist/login/TwoFactorChallengeScreen.js +139 -0
- package/dist/login/TwoFactorChallengeScreen.js.map +1 -0
- package/dist/login/loginMethods.d.ts +27 -0
- package/dist/login/loginMethods.d.ts.map +1 -0
- package/dist/login/loginMethods.js +37 -0
- package/dist/login/loginMethods.js.map +1 -0
- package/dist/login/loginTypes.d.ts +202 -5
- package/dist/login/loginTypes.d.ts.map +1 -1
- package/dist/pdfHtmlTemplate.d.ts +17 -0
- package/dist/pdfHtmlTemplate.d.ts.map +1 -1
- package/dist/pdfHtmlTemplate.js +78 -0
- package/dist/pdfHtmlTemplate.js.map +1 -1
- package/dist/security/AccountSecurityScreen.d.ts +10 -0
- package/dist/security/AccountSecurityScreen.d.ts.map +1 -0
- package/dist/security/AccountSecurityScreen.js +50 -0
- package/dist/security/AccountSecurityScreen.js.map +1 -0
- package/dist/security/LinkedAccountsCard.d.ts +32 -0
- package/dist/security/LinkedAccountsCard.d.ts.map +1 -0
- package/dist/security/LinkedAccountsCard.js +145 -0
- package/dist/security/LinkedAccountsCard.js.map +1 -0
- package/dist/security/PasskeysCard.d.ts +39 -0
- package/dist/security/PasskeysCard.d.ts.map +1 -0
- package/dist/security/PasskeysCard.js +168 -0
- package/dist/security/PasskeysCard.js.map +1 -0
- package/dist/security/PasswordCard.d.ts +32 -0
- package/dist/security/PasswordCard.d.ts.map +1 -0
- package/dist/security/PasswordCard.js +198 -0
- package/dist/security/PasswordCard.js.map +1 -0
- package/dist/security/SessionsCard.d.ts +39 -0
- package/dist/security/SessionsCard.d.ts.map +1 -0
- package/dist/security/SessionsCard.js +168 -0
- package/dist/security/SessionsCard.js.map +1 -0
- package/dist/security/TwoFactorSettingsCard.d.ts +55 -0
- package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -0
- package/dist/security/TwoFactorSettingsCard.js +458 -0
- package/dist/security/TwoFactorSettingsCard.js.map +1 -0
- package/dist/security/securityMethods.d.ts +40 -0
- package/dist/security/securityMethods.d.ts.map +1 -0
- package/dist/security/securityMethods.js +66 -0
- package/dist/security/securityMethods.js.map +1 -0
- package/dist/security/securityTypes.d.ts +467 -0
- package/dist/security/securityTypes.d.ts.map +1 -0
- package/dist/security/securityTypes.js +2 -0
- package/dist/security/securityTypes.js.map +1 -0
- package/dist/security/userAgent.d.ts +26 -0
- package/dist/security/userAgent.d.ts.map +1 -0
- package/dist/security/userAgent.js +80 -0
- package/dist/security/userAgent.js.map +1 -0
- package/dist/signUp/OAuthButtons.d.ts +28 -5
- package/dist/signUp/OAuthButtons.d.ts.map +1 -1
- package/dist/signUp/OAuthButtons.js +44 -5
- package/dist/signUp/OAuthButtons.js.map +1 -1
- package/dist/signUp/SignUpScreen.d.ts.map +1 -1
- package/dist/signUp/SignUpScreen.js +50 -5
- package/dist/signUp/SignUpScreen.js.map +1 -1
- package/dist/signUp/signUpTypes.d.ts +21 -1
- package/dist/signUp/signUpTypes.d.ts.map +1 -1
- package/dist/useConsentForms.d.ts +9 -0
- package/dist/useConsentForms.d.ts.map +1 -1
- package/dist/useConsentForms.js +18 -0
- package/dist/useConsentForms.js.map +1 -1
- package/dist/useProvisionalConsent.d.ts +124 -0
- package/dist/useProvisionalConsent.d.ts.map +1 -0
- package/dist/useProvisionalConsent.js +79 -0
- package/dist/useProvisionalConsent.js.map +1 -0
- package/package.json +3 -2
- package/src/Accordion.test.tsx +5 -0
- package/src/Accordion.tsx +3 -8
- package/src/AreaChart.test.tsx +15 -0
- package/src/Avatar.test.tsx +8 -0
- package/src/Avatar.tsx +2 -0
- package/src/BarChart.test.tsx +15 -0
- package/src/Box.test.tsx +61 -1
- package/src/Box.tsx +44 -2
- package/src/Card.tsx +2 -2
- package/src/Common.ts +134 -7
- package/src/ConsentFormScreen.test.tsx +84 -0
- package/src/ConsentFormScreen.tsx +28 -4
- package/src/DateTimeField.tsx +5 -3
- package/src/DonutChart.test.tsx +15 -0
- package/src/Field.tsx +2 -0
- package/src/FilePickerButton.test.tsx +35 -0
- package/src/FormContainer.test.tsx +242 -0
- package/src/FormContainer.tsx +177 -0
- package/src/GPTChat.test.tsx +16 -0
- package/src/GPTChat.tsx +8 -2
- package/src/Heading.test.tsx +14 -0
- package/src/Heading.tsx +6 -1
- package/src/Icon.test.tsx +52 -1
- package/src/Icon.tsx +92 -3
- package/src/IconButton.test.tsx +85 -0
- package/src/LabeledCheckBox.test.tsx +45 -0
- package/src/LabeledCheckBox.tsx +89 -0
- package/src/ListDetailLayout.tsx +247 -0
- package/src/MarkdownEditor.tsx +2 -1
- package/src/MediaQuery.ts +1 -0
- package/src/OtpField.test.tsx +280 -0
- package/src/OtpField.tsx +307 -0
- package/src/ProvisionalConsentSigner.test.tsx +625 -0
- package/src/ProvisionalConsentSigner.tsx +423 -0
- package/src/ResponsiveBreakpoint.test.tsx +7 -0
- package/src/ResponsiveBreakpoint.tsx +14 -0
- package/src/RootImportRegression.test.tsx +3 -0
- package/src/SocialLoginButton.test.tsx +14 -0
- package/src/SocialLoginButton.tsx +51 -7
- package/src/SplitPage.native.tsx +3 -1
- package/src/SplitPage.tsx +11 -5
- package/src/SyncStatusBanner.test.tsx +61 -4
- package/src/SyncStatusBanner.tsx +29 -3
- package/src/TextField.test.tsx +150 -0
- package/src/TextField.tsx +66 -8
- package/src/__snapshots__/AddressField.test.tsx.snap +12 -0
- package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +15 -0
- package/src/__snapshots__/Avatar.test.tsx.snap +1 -0
- package/src/__snapshots__/CustomSelectField.test.tsx.snap +3 -0
- package/src/__snapshots__/EmailField.test.tsx.snap +6 -0
- package/src/__snapshots__/Field.test.tsx.snap +37 -0
- package/src/__snapshots__/HeightField.test.tsx.snap +2 -0
- package/src/__snapshots__/MobileAddressAutoComplete.test.tsx.snap +3 -0
- package/src/__snapshots__/NumberField.test.tsx.snap +3 -0
- package/src/__snapshots__/PhoneNumberField.test.tsx.snap +12 -0
- package/src/__snapshots__/Slider.test.tsx.snap +1 -0
- package/src/__snapshots__/SocialLoginButton.test.tsx.snap +43 -22
- package/src/__snapshots__/TapToEdit.test.tsx.snap +3 -0
- package/src/__snapshots__/TextArea.test.tsx.snap +17 -1
- package/src/__snapshots__/TextField.test.tsx.snap +19 -2
- package/src/__snapshots__/TypedSignatureField.test.tsx.snap +3 -0
- package/src/__snapshots__/UnifiedAddressAutoComplete.test.tsx.snap +18 -0
- package/src/__snapshots__/WebAddressAutocomplete.test.tsx.snap +12 -0
- package/src/bunSetup.ts +13 -1
- package/src/fieldElements/FieldError.tsx +1 -0
- package/src/fieldElements/__snapshots__/FieldError.test.tsx.snap +2 -0
- package/src/index.tsx +42 -1
- package/src/isolated/ListDetailLayout.isolated.tsx +634 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +7 -0
- package/src/login/LastUsedBadge.tsx +40 -0
- package/src/login/LoginAccountCard.tsx +205 -0
- package/src/login/LoginAccountList.tsx +115 -0
- package/src/login/LoginScreen.test.tsx +837 -1
- package/src/login/LoginScreen.tsx +674 -91
- package/src/login/OutlinePill.tsx +110 -0
- package/src/login/TwoFactorChallengeScreen.test.tsx +376 -0
- package/src/login/TwoFactorChallengeScreen.tsx +307 -0
- package/src/login/__snapshots__/LoginScreen.test.tsx.snap +1454 -334
- package/src/login/loginMethods.test.ts +53 -0
- package/src/login/loginMethods.ts +60 -0
- package/src/login/loginTypes.ts +208 -5
- package/src/pdfHtmlTemplate.test.ts +243 -4
- package/src/pdfHtmlTemplate.ts +93 -0
- package/src/security/AccountSecurityScreen.test.tsx +262 -0
- package/src/security/AccountSecurityScreen.tsx +156 -0
- package/src/security/LinkedAccountsCard.test.tsx +371 -0
- package/src/security/LinkedAccountsCard.tsx +297 -0
- package/src/security/PasskeysCard.test.tsx +378 -0
- package/src/security/PasskeysCard.tsx +371 -0
- package/src/security/PasswordCard.test.tsx +445 -0
- package/src/security/PasswordCard.tsx +342 -0
- package/src/security/SessionsCard.test.tsx +349 -0
- package/src/security/SessionsCard.tsx +362 -0
- package/src/security/TwoFactorSettingsCard.test.tsx +700 -0
- package/src/security/TwoFactorSettingsCard.tsx +872 -0
- package/src/security/securityMethods.test.ts +101 -0
- package/src/security/securityMethods.ts +104 -0
- package/src/security/securityTypes.ts +480 -0
- package/src/security/userAgent.test.ts +119 -0
- package/src/security/userAgent.ts +102 -0
- package/src/signUp/OAuthButtons.test.tsx +27 -0
- package/src/signUp/OAuthButtons.tsx +153 -19
- package/src/signUp/SignUpScreen.test.tsx +170 -1
- package/src/signUp/SignUpScreen.tsx +87 -25
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +22 -11
- package/src/signUp/signUpTypes.ts +23 -1
- package/src/useConsentForms.test.ts +37 -1
- package/src/useConsentForms.ts +25 -0
- package/src/useProvisionalConsent.test.ts +256 -0
- package/src/useProvisionalConsent.ts +218 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import {type FC, useCallback, useEffect, useRef, useState} from "react";
|
|
2
|
+
import {type TextInput, View} from "react-native";
|
|
3
|
+
|
|
4
|
+
import {Box} from "../Box";
|
|
5
|
+
import {Button} from "../Button";
|
|
6
|
+
import {Card} from "../Card";
|
|
7
|
+
import {FormContainer, type FormContainerHandle} from "../FormContainer";
|
|
8
|
+
import {Heading} from "../Heading";
|
|
9
|
+
import {LabeledCheckBox} from "../LabeledCheckBox";
|
|
10
|
+
import {Text} from "../Text";
|
|
11
|
+
import {TextField} from "../TextField";
|
|
12
|
+
import {getRejectionMessage} from "./securityMethods";
|
|
13
|
+
import type {PasswordCardMode, PasswordCardProps} from "./securityTypes";
|
|
14
|
+
|
|
15
|
+
const DEFAULT_MIN_LENGTH = 8;
|
|
16
|
+
|
|
17
|
+
/** Inline error per field, shown under that field after a submit attempt. */
|
|
18
|
+
interface PasswordFieldErrors {
|
|
19
|
+
currentPassword?: string;
|
|
20
|
+
newPassword?: string;
|
|
21
|
+
confirmPassword?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Fields in screen order, for focusing the first invalid one. */
|
|
25
|
+
const FIELD_ORDER: (keyof PasswordFieldErrors)[] = [
|
|
26
|
+
"currentPassword",
|
|
27
|
+
"newPassword",
|
|
28
|
+
"confirmPassword",
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/** Copy for each mode. */
|
|
32
|
+
const MODE_COPY: Record<
|
|
33
|
+
PasswordCardMode,
|
|
34
|
+
{title: string; description: string; submit: string; success: string}
|
|
35
|
+
> = {
|
|
36
|
+
change: {
|
|
37
|
+
description: "Choose a new password for signing in with your email.",
|
|
38
|
+
submit: "Change password",
|
|
39
|
+
success: "Your password has been changed.",
|
|
40
|
+
title: "Change password",
|
|
41
|
+
},
|
|
42
|
+
set: {
|
|
43
|
+
description:
|
|
44
|
+
"You sign in with a linked account. Add a password to also sign in with your email.",
|
|
45
|
+
submit: "Set password",
|
|
46
|
+
success: "Your password has been set. You can now sign in with your email.",
|
|
47
|
+
title: "Set a password",
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Changes the user's password, or sets a first one for a user who signed up with a social
|
|
53
|
+
* provider. Change mode asks for the current password and can sign out other devices.
|
|
54
|
+
* Validates length and confirmation inline, then reports through `onSubmit`; never calls an auth
|
|
55
|
+
* client itself.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* <PasswordCard
|
|
60
|
+
* hasPassword={accounts.some((account) => account.providerId === "credential")}
|
|
61
|
+
* onSubmit={async ({mode, currentPassword, newPassword, revokeOtherSessions}) => {
|
|
62
|
+
* if (mode === "change") {
|
|
63
|
+
* // The Better Auth client resolves {data, error} rather than throwing.
|
|
64
|
+
* const {error} = await authClient.changePassword({
|
|
65
|
+
* currentPassword: currentPassword ?? "",
|
|
66
|
+
* newPassword,
|
|
67
|
+
* revokeOtherSessions,
|
|
68
|
+
* });
|
|
69
|
+
* if (error) {
|
|
70
|
+
* throw new Error(error.message ?? "Couldn't change your password.");
|
|
71
|
+
* }
|
|
72
|
+
* } else {
|
|
73
|
+
* await setPassword({newPassword}); // a server route: Better Auth's setPassword is server-only
|
|
74
|
+
* }
|
|
75
|
+
* }}
|
|
76
|
+
* />
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export const PasswordCard: FC<PasswordCardProps> = ({
|
|
80
|
+
hasPassword,
|
|
81
|
+
onSubmit,
|
|
82
|
+
showRevokeOtherSessions = true,
|
|
83
|
+
minLength = DEFAULT_MIN_LENGTH,
|
|
84
|
+
error,
|
|
85
|
+
loading = false,
|
|
86
|
+
username,
|
|
87
|
+
testID = "password-card",
|
|
88
|
+
}) => {
|
|
89
|
+
const mode: PasswordCardMode = hasPassword ? "change" : "set";
|
|
90
|
+
const copy = MODE_COPY[mode];
|
|
91
|
+
const [currentPassword, setCurrentPassword] = useState("");
|
|
92
|
+
const [newPassword, setNewPassword] = useState("");
|
|
93
|
+
const [confirmPassword, setConfirmPassword] = useState("");
|
|
94
|
+
const [revokeOtherSessions, setRevokeOtherSessions] = useState(false);
|
|
95
|
+
const [fieldErrors, setFieldErrors] = useState<PasswordFieldErrors>({});
|
|
96
|
+
const [submitError, setSubmitError] = useState<string | undefined>(undefined);
|
|
97
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
98
|
+
const [successMessage, setSuccessMessage] = useState<string | undefined>(undefined);
|
|
99
|
+
const isSubmittingRef = useRef(false);
|
|
100
|
+
const previousModeRef = useRef(mode);
|
|
101
|
+
const inputRefs = useRef<Partial<Record<keyof PasswordFieldErrors, TextInput | null>>>({});
|
|
102
|
+
const formRef = useRef<FormContainerHandle>(null);
|
|
103
|
+
|
|
104
|
+
const setCurrentInputRef = useCallback((ref: TextInput | null): void => {
|
|
105
|
+
inputRefs.current.currentPassword = ref;
|
|
106
|
+
}, []);
|
|
107
|
+
const setNewInputRef = useCallback((ref: TextInput | null): void => {
|
|
108
|
+
inputRefs.current.newPassword = ref;
|
|
109
|
+
}, []);
|
|
110
|
+
const setConfirmInputRef = useCallback((ref: TextInput | null): void => {
|
|
111
|
+
inputRefs.current.confirmPassword = ref;
|
|
112
|
+
}, []);
|
|
113
|
+
|
|
114
|
+
const resetForm = useCallback((): void => {
|
|
115
|
+
setCurrentPassword("");
|
|
116
|
+
setNewPassword("");
|
|
117
|
+
setConfirmPassword("");
|
|
118
|
+
setRevokeOtherSessions(false);
|
|
119
|
+
setFieldErrors({});
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
// Setting a first password flips the card to change mode; start that form empty.
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
if (previousModeRef.current === mode) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
previousModeRef.current = mode;
|
|
128
|
+
resetForm();
|
|
129
|
+
setSubmitError(undefined);
|
|
130
|
+
}, [mode, resetForm]);
|
|
131
|
+
|
|
132
|
+
const isBusy = loading || isSubmitting;
|
|
133
|
+
const invalidFieldCount = Object.values(fieldErrors).filter(Boolean).length;
|
|
134
|
+
const shownError = error ?? submitError;
|
|
135
|
+
|
|
136
|
+
const handleCurrentChange = useCallback((value: string): void => {
|
|
137
|
+
setCurrentPassword(value);
|
|
138
|
+
setSuccessMessage(undefined);
|
|
139
|
+
setFieldErrors((current) => ({...current, currentPassword: undefined}));
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
const handleNewChange = useCallback((value: string): void => {
|
|
143
|
+
setNewPassword(value);
|
|
144
|
+
setSuccessMessage(undefined);
|
|
145
|
+
setFieldErrors((current) => ({...current, newPassword: undefined}));
|
|
146
|
+
}, []);
|
|
147
|
+
|
|
148
|
+
const handleConfirmChange = useCallback((value: string): void => {
|
|
149
|
+
setConfirmPassword(value);
|
|
150
|
+
setSuccessMessage(undefined);
|
|
151
|
+
setFieldErrors((current) => ({...current, confirmPassword: undefined}));
|
|
152
|
+
}, []);
|
|
153
|
+
|
|
154
|
+
const validate = useCallback((): PasswordFieldErrors => {
|
|
155
|
+
const errors: PasswordFieldErrors = {};
|
|
156
|
+
if (mode === "change" && !currentPassword) {
|
|
157
|
+
errors.currentPassword = "Enter your current password.";
|
|
158
|
+
}
|
|
159
|
+
if (newPassword.length < minLength) {
|
|
160
|
+
errors.newPassword = `Use at least ${minLength} characters.`;
|
|
161
|
+
}
|
|
162
|
+
if (confirmPassword !== newPassword) {
|
|
163
|
+
errors.confirmPassword = "Passwords don't match.";
|
|
164
|
+
}
|
|
165
|
+
return errors;
|
|
166
|
+
}, [confirmPassword, currentPassword, minLength, mode, newPassword]);
|
|
167
|
+
|
|
168
|
+
const handleSubmit = useCallback(async (): Promise<void> => {
|
|
169
|
+
// The ref blocks a second submission (Enter plus a press) before state updates land.
|
|
170
|
+
if (isBusy || isSubmittingRef.current) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const errors = validate();
|
|
174
|
+
setFieldErrors(errors);
|
|
175
|
+
const firstInvalidField = FIELD_ORDER.find((field) => Boolean(errors[field]));
|
|
176
|
+
if (firstInvalidField) {
|
|
177
|
+
inputRefs.current[firstInvalidField]?.focus?.();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
isSubmittingRef.current = true;
|
|
181
|
+
setIsSubmitting(true);
|
|
182
|
+
setSubmitError(undefined);
|
|
183
|
+
setSuccessMessage(undefined);
|
|
184
|
+
try {
|
|
185
|
+
await onSubmit({
|
|
186
|
+
...(mode === "change" ? {currentPassword} : {}),
|
|
187
|
+
mode,
|
|
188
|
+
newPassword,
|
|
189
|
+
revokeOtherSessions: mode === "change" && showRevokeOtherSessions && revokeOtherSessions,
|
|
190
|
+
});
|
|
191
|
+
resetForm();
|
|
192
|
+
setSuccessMessage(copy.success);
|
|
193
|
+
} catch (rejection: unknown) {
|
|
194
|
+
console.warn("PasswordCard: onSubmit rejected", rejection);
|
|
195
|
+
setSubmitError(
|
|
196
|
+
getRejectionMessage({
|
|
197
|
+
error: rejection,
|
|
198
|
+
fallback: "Couldn't update your password. Try again.",
|
|
199
|
+
})
|
|
200
|
+
);
|
|
201
|
+
} finally {
|
|
202
|
+
isSubmittingRef.current = false;
|
|
203
|
+
setIsSubmitting(false);
|
|
204
|
+
}
|
|
205
|
+
}, [
|
|
206
|
+
copy.success,
|
|
207
|
+
currentPassword,
|
|
208
|
+
isBusy,
|
|
209
|
+
mode,
|
|
210
|
+
newPassword,
|
|
211
|
+
onSubmit,
|
|
212
|
+
resetForm,
|
|
213
|
+
revokeOtherSessions,
|
|
214
|
+
showRevokeOtherSessions,
|
|
215
|
+
validate,
|
|
216
|
+
]);
|
|
217
|
+
|
|
218
|
+
// On web this goes through the <form>, so password managers see a real submit.
|
|
219
|
+
const requestSubmit = useCallback(async (): Promise<void> => {
|
|
220
|
+
await formRef.current?.requestSubmit();
|
|
221
|
+
}, []);
|
|
222
|
+
|
|
223
|
+
// Enter moves down the fields; in the confirm field it submits.
|
|
224
|
+
const focusNewPassword = useCallback((): void => {
|
|
225
|
+
inputRefs.current.newPassword?.focus?.();
|
|
226
|
+
}, []);
|
|
227
|
+
const focusConfirmPassword = useCallback((): void => {
|
|
228
|
+
inputRefs.current.confirmPassword?.focus?.();
|
|
229
|
+
}, []);
|
|
230
|
+
|
|
231
|
+
return (
|
|
232
|
+
<Card testID={testID}>
|
|
233
|
+
<FormContainer
|
|
234
|
+
accessibilityLabel={copy.title}
|
|
235
|
+
gap={4}
|
|
236
|
+
onSubmit={handleSubmit}
|
|
237
|
+
ref={formRef}
|
|
238
|
+
testID={`${testID}-form`}
|
|
239
|
+
username={username}
|
|
240
|
+
>
|
|
241
|
+
<Box gap={1}>
|
|
242
|
+
<Heading level={2} size="md" testID={`${testID}-title`}>
|
|
243
|
+
{copy.title}
|
|
244
|
+
</Heading>
|
|
245
|
+
<Text color="secondaryLight">{copy.description}</Text>
|
|
246
|
+
</Box>
|
|
247
|
+
|
|
248
|
+
{mode === "change" && (
|
|
249
|
+
<TextField
|
|
250
|
+
accessibilityLabel="Current password"
|
|
251
|
+
autoComplete="current-password"
|
|
252
|
+
blurOnSubmit={false}
|
|
253
|
+
disabled={isBusy}
|
|
254
|
+
errorText={fieldErrors.currentPassword}
|
|
255
|
+
inputRef={setCurrentInputRef}
|
|
256
|
+
onChange={handleCurrentChange}
|
|
257
|
+
onEnter={focusNewPassword}
|
|
258
|
+
returnKeyType="next"
|
|
259
|
+
testID={`${testID}-current`}
|
|
260
|
+
title="Current password"
|
|
261
|
+
type="password"
|
|
262
|
+
value={currentPassword}
|
|
263
|
+
/>
|
|
264
|
+
)}
|
|
265
|
+
|
|
266
|
+
<TextField
|
|
267
|
+
accessibilityLabel="New password"
|
|
268
|
+
autoComplete="new-password"
|
|
269
|
+
blurOnSubmit={false}
|
|
270
|
+
disabled={isBusy}
|
|
271
|
+
errorText={fieldErrors.newPassword}
|
|
272
|
+
helperText={fieldErrors.newPassword ? undefined : `At least ${minLength} characters.`}
|
|
273
|
+
inputRef={setNewInputRef}
|
|
274
|
+
onChange={handleNewChange}
|
|
275
|
+
onEnter={focusConfirmPassword}
|
|
276
|
+
returnKeyType="next"
|
|
277
|
+
testID={`${testID}-new`}
|
|
278
|
+
title="New password"
|
|
279
|
+
type="password"
|
|
280
|
+
value={newPassword}
|
|
281
|
+
/>
|
|
282
|
+
|
|
283
|
+
<TextField
|
|
284
|
+
accessibilityLabel="Confirm new password"
|
|
285
|
+
autoComplete="new-password"
|
|
286
|
+
disabled={isBusy}
|
|
287
|
+
errorText={fieldErrors.confirmPassword}
|
|
288
|
+
inputRef={setConfirmInputRef}
|
|
289
|
+
onChange={handleConfirmChange}
|
|
290
|
+
onEnter={requestSubmit}
|
|
291
|
+
returnKeyType="done"
|
|
292
|
+
testID={`${testID}-confirm`}
|
|
293
|
+
title="Confirm new password"
|
|
294
|
+
type="password"
|
|
295
|
+
value={confirmPassword}
|
|
296
|
+
/>
|
|
297
|
+
|
|
298
|
+
{mode === "change" && showRevokeOtherSessions && (
|
|
299
|
+
<LabeledCheckBox
|
|
300
|
+
checked={revokeOtherSessions}
|
|
301
|
+
disabled={isBusy}
|
|
302
|
+
label="Sign out of other devices"
|
|
303
|
+
onChange={setRevokeOtherSessions}
|
|
304
|
+
testID={`${testID}-revoke-other-sessions`}
|
|
305
|
+
/>
|
|
306
|
+
)}
|
|
307
|
+
|
|
308
|
+
{invalidFieldCount > 0 && (
|
|
309
|
+
<View role="alert" testID={`${testID}-validation-error`}>
|
|
310
|
+
<Text color="error">
|
|
311
|
+
{invalidFieldCount === 1
|
|
312
|
+
? "Fix the highlighted field."
|
|
313
|
+
: `Fix the ${invalidFieldCount} highlighted fields.`}
|
|
314
|
+
</Text>
|
|
315
|
+
</View>
|
|
316
|
+
)}
|
|
317
|
+
|
|
318
|
+
{Boolean(shownError) && (
|
|
319
|
+
<View role="alert" testID={`${testID}-error`}>
|
|
320
|
+
<Text color="error">{shownError}</Text>
|
|
321
|
+
</View>
|
|
322
|
+
)}
|
|
323
|
+
|
|
324
|
+
{Boolean(successMessage) && !shownError && (
|
|
325
|
+
<View role="status" testID={`${testID}-success`}>
|
|
326
|
+
<Text color="success">{successMessage}</Text>
|
|
327
|
+
</View>
|
|
328
|
+
)}
|
|
329
|
+
|
|
330
|
+
<Box alignItems="start">
|
|
331
|
+
<Button
|
|
332
|
+
disabled={isBusy}
|
|
333
|
+
loading={isBusy}
|
|
334
|
+
onClick={requestSubmit}
|
|
335
|
+
testID={`${testID}-submit`}
|
|
336
|
+
text={copy.submit}
|
|
337
|
+
/>
|
|
338
|
+
</Box>
|
|
339
|
+
</FormContainer>
|
|
340
|
+
</Card>
|
|
341
|
+
);
|
|
342
|
+
};
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import {afterAll, beforeAll, describe, expect, it, jest, mock, spyOn} from "bun:test";
|
|
2
|
+
import {act, fireEvent, waitFor} from "@testing-library/react-native";
|
|
3
|
+
import {DateTime, Settings} from "luxon";
|
|
4
|
+
import {ActivityIndicator} from "react-native";
|
|
5
|
+
|
|
6
|
+
import {renderWithTheme} from "../test-utils";
|
|
7
|
+
import {SessionsCard} from "./SessionsCard";
|
|
8
|
+
import type {AccountSession} from "./securityTypes";
|
|
9
|
+
|
|
10
|
+
const NOW = new Date("2026-03-10T12:00:00.000Z");
|
|
11
|
+
|
|
12
|
+
const CHROME_MAC =
|
|
13
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36";
|
|
14
|
+
const SAFARI_IPHONE =
|
|
15
|
+
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1";
|
|
16
|
+
const FIREFOX_WINDOWS =
|
|
17
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0";
|
|
18
|
+
|
|
19
|
+
const CURRENT: AccountSession = {
|
|
20
|
+
createdAt: "2026-03-01T12:00:00.000Z",
|
|
21
|
+
id: "s-current",
|
|
22
|
+
ipAddress: "203.0.113.4",
|
|
23
|
+
token: "tok-current",
|
|
24
|
+
updatedAt: "2026-03-09T12:00:00.000Z",
|
|
25
|
+
userAgent: CHROME_MAC,
|
|
26
|
+
};
|
|
27
|
+
const PHONE: AccountSession = {
|
|
28
|
+
createdAt: new Date("2026-03-02T12:00:00.000Z"),
|
|
29
|
+
id: "s-phone",
|
|
30
|
+
ipAddress: "198.51.100.7",
|
|
31
|
+
location: "Denver, US",
|
|
32
|
+
token: "tok-phone",
|
|
33
|
+
updatedAt: new Date("2026-03-10T11:55:00.000Z"),
|
|
34
|
+
userAgent: SAFARI_IPHONE,
|
|
35
|
+
};
|
|
36
|
+
const WINDOWS: AccountSession = {
|
|
37
|
+
createdAt: "2026-03-07T12:00:00.000Z",
|
|
38
|
+
id: "s-windows",
|
|
39
|
+
token: "tok-windows",
|
|
40
|
+
updatedAt: "2026-03-08T12:00:00.000Z",
|
|
41
|
+
userAgent: FIREFOX_WINDOWS,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const press = async (element: Parameters<typeof fireEvent.press>[0]): Promise<void> => {
|
|
45
|
+
await act(async () => {
|
|
46
|
+
fireEvent.press(element);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const isDisabled = (element: {props: Record<string, unknown>}): boolean => {
|
|
51
|
+
const state = element.props.accessibilityState as {disabled?: boolean} | undefined;
|
|
52
|
+
return Boolean(state?.disabled);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
describe("SessionsCard", () => {
|
|
56
|
+
// Pin Luxon's locale so relative times can be asserted as literal English strings.
|
|
57
|
+
const previousLocale = Settings.defaultLocale;
|
|
58
|
+
beforeAll(() => {
|
|
59
|
+
Settings.defaultLocale = "en-US";
|
|
60
|
+
});
|
|
61
|
+
afterAll(() => {
|
|
62
|
+
Settings.defaultLocale = previousLocale;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("shows each session's device, browser, location, sign-in and refresh times", () => {
|
|
66
|
+
const {getByTestId} = renderWithTheme(
|
|
67
|
+
<SessionsCard currentSessionId="s-current" now={NOW} sessions={[CURRENT, PHONE, WINDOWS]} />
|
|
68
|
+
);
|
|
69
|
+
expect(getByTestId("sessions-card-name-s-phone").props.children).toBe("Safari on iOS");
|
|
70
|
+
expect(getByTestId("sessions-card-details-s-phone").props.children).toBe(
|
|
71
|
+
"Denver, US · 198.51.100.7"
|
|
72
|
+
);
|
|
73
|
+
expect(getByTestId("sessions-card-signed-in-s-phone").props.children).toBe(
|
|
74
|
+
"Signed in 8 days ago"
|
|
75
|
+
);
|
|
76
|
+
expect(getByTestId("sessions-card-activity-s-phone").props.children).toBe(
|
|
77
|
+
"Last refreshed 5 minutes ago"
|
|
78
|
+
);
|
|
79
|
+
expect(getByTestId("sessions-card-name-s-windows").props.children).toBe("Firefox on Windows");
|
|
80
|
+
expect(getByTestId("sessions-card-signed-in-s-windows").props.children).toBe(
|
|
81
|
+
"Signed in 3 days ago"
|
|
82
|
+
);
|
|
83
|
+
expect(getByTestId("sessions-card-activity-s-windows").props.children).toBe(
|
|
84
|
+
"Last refreshed 2 days ago"
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("omits details and times when they are unknown", () => {
|
|
89
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
90
|
+
<SessionsCard now={NOW} sessions={[{id: "s-bare"}]} />
|
|
91
|
+
);
|
|
92
|
+
expect(getByTestId("sessions-card-name-s-bare").props.children).toBe("Unknown device");
|
|
93
|
+
expect(queryByTestId("sessions-card-details-s-bare")).toBeNull();
|
|
94
|
+
expect(queryByTestId("sessions-card-signed-in-s-bare")).toBeNull();
|
|
95
|
+
expect(queryByTestId("sessions-card-activity-s-bare")).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("lists the current session first, marked This device, active now, and not revocable", () => {
|
|
99
|
+
const onRevoke = mock((_session: AccountSession) => Promise.resolve());
|
|
100
|
+
const {getByTestId, queryByTestId, toJSON} = renderWithTheme(
|
|
101
|
+
<SessionsCard
|
|
102
|
+
currentSessionId="s-current"
|
|
103
|
+
now={NOW}
|
|
104
|
+
onRevoke={onRevoke}
|
|
105
|
+
sessions={[WINDOWS, PHONE, CURRENT]}
|
|
106
|
+
/>
|
|
107
|
+
);
|
|
108
|
+
expect(getByTestId("sessions-card-current-s-current")).toBeTruthy();
|
|
109
|
+
expect(queryByTestId("sessions-card-current-s-phone")).toBeNull();
|
|
110
|
+
expect(queryByTestId("sessions-card-revoke-s-current")).toBeNull();
|
|
111
|
+
expect(getByTestId("sessions-card-revoke-s-phone")).toBeTruthy();
|
|
112
|
+
expect(getByTestId("sessions-card-revoke-s-windows")).toBeTruthy();
|
|
113
|
+
expect(getByTestId("sessions-card-activity-s-current").props.children).toBe("Active now");
|
|
114
|
+
expect(getByTestId("sessions-card-signed-in-s-current").props.children).toBe(
|
|
115
|
+
"Signed in 9 days ago"
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// Others were passed oldest-first; the card lists the most recently refreshed first.
|
|
119
|
+
const order = JSON.stringify(toJSON()).match(/sessions-card-row-s-[a-z]+/g);
|
|
120
|
+
expect(order).toEqual([
|
|
121
|
+
"sessions-card-row-s-current",
|
|
122
|
+
"sessions-card-row-s-phone",
|
|
123
|
+
"sessions-card-row-s-windows",
|
|
124
|
+
]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("identifies the current session by token", () => {
|
|
128
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
129
|
+
<SessionsCard
|
|
130
|
+
currentSessionToken="tok-phone"
|
|
131
|
+
now={NOW}
|
|
132
|
+
onRevoke={mock(() => Promise.resolve())}
|
|
133
|
+
sessions={[CURRENT, PHONE]}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
expect(getByTestId("sessions-card-current-s-phone")).toBeTruthy();
|
|
137
|
+
expect(queryByTestId("sessions-card-revoke-s-phone")).toBeNull();
|
|
138
|
+
expect(getByTestId("sessions-card-revoke-s-current")).toBeTruthy();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("recomputes relative times every minute when now is not passed", () => {
|
|
142
|
+
jest.useFakeTimers();
|
|
143
|
+
const clearIntervalSpy = spyOn(globalThis, "clearInterval");
|
|
144
|
+
try {
|
|
145
|
+
// bunSetup pins Date.now before each test; drive it from here instead.
|
|
146
|
+
let currentMillis = NOW.getTime();
|
|
147
|
+
globalThis.Date.now = mock(() => currentMillis);
|
|
148
|
+
const view = renderWithTheme(<SessionsCard sessions={[PHONE]} />);
|
|
149
|
+
expect(view.getByTestId("sessions-card-activity-s-phone").props.children).toBe(
|
|
150
|
+
"Last refreshed 5 minutes ago"
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
currentMillis = DateTime.fromJSDate(NOW).plus({minutes: 10}).toMillis();
|
|
154
|
+
act(() => {
|
|
155
|
+
jest.advanceTimersByTime(59_999);
|
|
156
|
+
});
|
|
157
|
+
expect(view.getByTestId("sessions-card-activity-s-phone").props.children).toBe(
|
|
158
|
+
"Last refreshed 5 minutes ago"
|
|
159
|
+
);
|
|
160
|
+
act(() => {
|
|
161
|
+
jest.advanceTimersByTime(1);
|
|
162
|
+
});
|
|
163
|
+
expect(view.getByTestId("sessions-card-activity-s-phone").props.children).toBe(
|
|
164
|
+
"Last refreshed 15 minutes ago"
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const callsBeforeUnmount = clearIntervalSpy.mock.calls.length;
|
|
168
|
+
view.unmount();
|
|
169
|
+
expect(clearIntervalSpy.mock.calls.length).toBeGreaterThan(callsBeforeUnmount);
|
|
170
|
+
} finally {
|
|
171
|
+
clearIntervalSpy.mockRestore();
|
|
172
|
+
jest.useRealTimers();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("does not tick when now is passed", () => {
|
|
177
|
+
const setIntervalSpy = spyOn(globalThis, "setInterval");
|
|
178
|
+
try {
|
|
179
|
+
renderWithTheme(<SessionsCard now={NOW} sessions={[PHONE]} />);
|
|
180
|
+
expect(setIntervalSpy.mock.calls.some(([, delay]) => delay === 60_000)).toBe(false);
|
|
181
|
+
} finally {
|
|
182
|
+
setIntervalSpy.mockRestore();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("revokes another session and names the button after it", async () => {
|
|
187
|
+
const onRevoke = mock((_session: AccountSession) => Promise.resolve());
|
|
188
|
+
const {getByTestId} = renderWithTheme(
|
|
189
|
+
<SessionsCard
|
|
190
|
+
currentSessionId="s-current"
|
|
191
|
+
now={NOW}
|
|
192
|
+
onRevoke={onRevoke}
|
|
193
|
+
sessions={[CURRENT, PHONE]}
|
|
194
|
+
/>
|
|
195
|
+
);
|
|
196
|
+
const revoke = getByTestId("sessions-card-revoke-s-phone");
|
|
197
|
+
expect(revoke.props.accessibilityLabel).toBe("Sign out Safari on iOS, signed in 8 days ago");
|
|
198
|
+
await press(revoke);
|
|
199
|
+
await waitFor(() => {
|
|
200
|
+
expect(onRevoke).toHaveBeenCalledWith(PHONE);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("shows a spinner on the revoking row and disables the others until it settles", async () => {
|
|
205
|
+
let resolveRevoke: () => void = () => {};
|
|
206
|
+
const onRevoke = mock(
|
|
207
|
+
(_session: AccountSession) =>
|
|
208
|
+
new Promise<void>((resolve) => {
|
|
209
|
+
resolveRevoke = resolve;
|
|
210
|
+
})
|
|
211
|
+
);
|
|
212
|
+
const {getByTestId} = renderWithTheme(
|
|
213
|
+
<SessionsCard
|
|
214
|
+
currentSessionId="s-current"
|
|
215
|
+
now={NOW}
|
|
216
|
+
onRevoke={onRevoke}
|
|
217
|
+
onRevokeOthers={mock(() => Promise.resolve())}
|
|
218
|
+
sessions={[CURRENT, PHONE, WINDOWS]}
|
|
219
|
+
/>
|
|
220
|
+
);
|
|
221
|
+
await press(getByTestId("sessions-card-revoke-s-phone"));
|
|
222
|
+
const revoking = getByTestId("sessions-card-revoke-s-phone");
|
|
223
|
+
expect(revoking.findAllByType(ActivityIndicator).length).toBe(1);
|
|
224
|
+
const other = getByTestId("sessions-card-revoke-s-windows");
|
|
225
|
+
expect(isDisabled(other)).toBe(true);
|
|
226
|
+
expect(other.findAllByType(ActivityIndicator).length).toBe(0);
|
|
227
|
+
expect(isDisabled(getByTestId("sessions-card-revoke-others"))).toBe(true);
|
|
228
|
+
|
|
229
|
+
await act(async () => {
|
|
230
|
+
resolveRevoke();
|
|
231
|
+
});
|
|
232
|
+
await waitFor(() => {
|
|
233
|
+
expect(isDisabled(getByTestId("sessions-card-revoke-s-windows"))).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("confirms Sign out everywhere else before calling onRevokeOthers", async () => {
|
|
238
|
+
const onRevokeOthers = mock(() => Promise.resolve());
|
|
239
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
240
|
+
<SessionsCard
|
|
241
|
+
currentSessionId="s-current"
|
|
242
|
+
now={NOW}
|
|
243
|
+
onRevokeOthers={onRevokeOthers}
|
|
244
|
+
sessions={[CURRENT, PHONE, WINDOWS]}
|
|
245
|
+
/>
|
|
246
|
+
);
|
|
247
|
+
await press(getByTestId("sessions-card-revoke-others"));
|
|
248
|
+
expect(onRevokeOthers).not.toHaveBeenCalled();
|
|
249
|
+
expect(getByTestId("sessions-card-confirm")).toBeTruthy();
|
|
250
|
+
|
|
251
|
+
await press(getByTestId("sessions-card-confirm-submit"));
|
|
252
|
+
await waitFor(() => {
|
|
253
|
+
expect(onRevokeOthers).toHaveBeenCalledTimes(1);
|
|
254
|
+
});
|
|
255
|
+
expect(queryByTestId("sessions-card-confirm")).toBeNull();
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("does not sign out other sessions when the confirm is cancelled", async () => {
|
|
259
|
+
const onRevokeOthers = mock(() => Promise.resolve());
|
|
260
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
261
|
+
<SessionsCard
|
|
262
|
+
currentSessionId="s-current"
|
|
263
|
+
now={NOW}
|
|
264
|
+
onRevokeOthers={onRevokeOthers}
|
|
265
|
+
sessions={[CURRENT, PHONE]}
|
|
266
|
+
/>
|
|
267
|
+
);
|
|
268
|
+
await press(getByTestId("sessions-card-revoke-others"));
|
|
269
|
+
await press(getByTestId("sessions-card-confirm-cancel"));
|
|
270
|
+
expect(queryByTestId("sessions-card-confirm")).toBeNull();
|
|
271
|
+
expect(onRevokeOthers).not.toHaveBeenCalled();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("disables Sign out everywhere else when there are no other sessions", () => {
|
|
275
|
+
const {getByTestId} = renderWithTheme(
|
|
276
|
+
<SessionsCard
|
|
277
|
+
currentSessionId="s-current"
|
|
278
|
+
now={NOW}
|
|
279
|
+
onRevokeOthers={mock(() => Promise.resolve())}
|
|
280
|
+
sessions={[CURRENT]}
|
|
281
|
+
/>
|
|
282
|
+
);
|
|
283
|
+
expect(isDisabled(getByTestId("sessions-card-revoke-others"))).toBe(true);
|
|
284
|
+
expect(getByTestId("sessions-card-no-others")).toBeTruthy();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("shows a rejected callback's message as an alert", async () => {
|
|
288
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
289
|
+
const onRevoke = mock((_session: AccountSession) =>
|
|
290
|
+
Promise.reject(new Error("Session not found"))
|
|
291
|
+
);
|
|
292
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
293
|
+
<SessionsCard
|
|
294
|
+
currentSessionId="s-current"
|
|
295
|
+
now={NOW}
|
|
296
|
+
onRevoke={onRevoke}
|
|
297
|
+
sessions={[CURRENT, PHONE]}
|
|
298
|
+
/>
|
|
299
|
+
);
|
|
300
|
+
await press(getByTestId("sessions-card-revoke-s-phone"));
|
|
301
|
+
await waitFor(() => {
|
|
302
|
+
expect(getByTestId("sessions-card-error")).toBeTruthy();
|
|
303
|
+
});
|
|
304
|
+
expect(getByTestId("sessions-card-error").props.role).toBe("alert");
|
|
305
|
+
expect(getByText("Session not found")).toBeTruthy();
|
|
306
|
+
warn.mockRestore();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("falls back to a generic message when onRevokeOthers rejects without one", async () => {
|
|
310
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
311
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
312
|
+
<SessionsCard
|
|
313
|
+
currentSessionId="s-current"
|
|
314
|
+
now={NOW}
|
|
315
|
+
onRevokeOthers={mock(() => Promise.reject(undefined))}
|
|
316
|
+
sessions={[CURRENT, PHONE]}
|
|
317
|
+
/>
|
|
318
|
+
);
|
|
319
|
+
await press(getByTestId("sessions-card-revoke-others"));
|
|
320
|
+
await press(getByTestId("sessions-card-confirm-submit"));
|
|
321
|
+
await waitFor(() => {
|
|
322
|
+
expect(getByText("Couldn't sign out your other sessions. Try again.")).toBeTruthy();
|
|
323
|
+
});
|
|
324
|
+
warn.mockRestore();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("shows the error prop as an alert and disables buttons with disabled", () => {
|
|
328
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
329
|
+
<SessionsCard
|
|
330
|
+
currentSessionId="s-current"
|
|
331
|
+
disabled
|
|
332
|
+
error="Sessions couldn't load."
|
|
333
|
+
now={NOW}
|
|
334
|
+
onRevoke={mock(() => Promise.resolve())}
|
|
335
|
+
onRevokeOthers={mock(() => Promise.resolve())}
|
|
336
|
+
sessions={[CURRENT, PHONE]}
|
|
337
|
+
/>
|
|
338
|
+
);
|
|
339
|
+
expect(getByTestId("sessions-card-error").props.role).toBe("alert");
|
|
340
|
+
expect(getByText("Sessions couldn't load.")).toBeTruthy();
|
|
341
|
+
expect(isDisabled(getByTestId("sessions-card-revoke-s-phone"))).toBe(true);
|
|
342
|
+
expect(isDisabled(getByTestId("sessions-card-revoke-others"))).toBe(true);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("shows an empty state with no sessions", () => {
|
|
346
|
+
const {getByTestId} = renderWithTheme(<SessionsCard now={NOW} sessions={[]} />);
|
|
347
|
+
expect(getByTestId("sessions-card-empty")).toBeTruthy();
|
|
348
|
+
});
|
|
349
|
+
});
|