@terreno/ui 57.18.0 → 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.
Files changed (361) hide show
  1. package/.ai/guidelines/core.md +1 -1
  2. package/CHANGELOG.md +2074 -0
  3. package/dist/Accordion.d.ts.map +1 -1
  4. package/dist/Accordion.js +3 -3
  5. package/dist/Accordion.js.map +1 -1
  6. package/dist/AreaChart.d.ts.map +1 -1
  7. package/dist/AreaChart.js +2 -2
  8. package/dist/AreaChart.js.map +1 -1
  9. package/dist/Avatar.d.ts.map +1 -1
  10. package/dist/Avatar.js +3 -1
  11. package/dist/Avatar.js.map +1 -1
  12. package/dist/BarChart.d.ts.map +1 -1
  13. package/dist/BarChart.js +2 -2
  14. package/dist/BarChart.js.map +1 -1
  15. package/dist/Box.d.ts.map +1 -1
  16. package/dist/Box.js +66 -8
  17. package/dist/Box.js.map +1 -1
  18. package/dist/Card.d.ts.map +1 -1
  19. package/dist/Card.js +5 -4
  20. package/dist/Card.js.map +1 -1
  21. package/dist/ChartCard.d.ts.map +1 -1
  22. package/dist/ChartCard.js +4 -2
  23. package/dist/ChartCard.js.map +1 -1
  24. package/dist/Common.d.ts +240 -14
  25. package/dist/Common.d.ts.map +1 -1
  26. package/dist/Common.js.map +1 -1
  27. package/dist/ConsentFormScreen.d.ts +1 -1
  28. package/dist/ConsentFormScreen.d.ts.map +1 -1
  29. package/dist/ConsentFormScreen.js +13 -4
  30. package/dist/ConsentFormScreen.js.map +1 -1
  31. package/dist/DateTimeField.d.ts.map +1 -1
  32. package/dist/DateTimeField.js +5 -3
  33. package/dist/DateTimeField.js.map +1 -1
  34. package/dist/DonutChart.d.ts.map +1 -1
  35. package/dist/DonutChart.js +2 -2
  36. package/dist/DonutChart.js.map +1 -1
  37. package/dist/Field.d.ts.map +1 -1
  38. package/dist/Field.js +2 -0
  39. package/dist/Field.js.map +1 -1
  40. package/dist/FormContainer.d.ts +68 -0
  41. package/dist/FormContainer.d.ts.map +1 -0
  42. package/dist/FormContainer.js +79 -0
  43. package/dist/FormContainer.js.map +1 -0
  44. package/dist/GPTChat.d.ts.map +1 -1
  45. package/dist/GPTChat.js +2 -1
  46. package/dist/GPTChat.js.map +1 -1
  47. package/dist/Heading.d.ts +1 -1
  48. package/dist/Heading.d.ts.map +1 -1
  49. package/dist/Heading.js +5 -2
  50. package/dist/Heading.js.map +1 -1
  51. package/dist/Icon.d.ts +4 -0
  52. package/dist/Icon.d.ts.map +1 -1
  53. package/dist/Icon.js +43 -2
  54. package/dist/Icon.js.map +1 -1
  55. package/dist/LabeledCheckBox.d.ts +22 -0
  56. package/dist/LabeledCheckBox.d.ts.map +1 -0
  57. package/dist/LabeledCheckBox.js +30 -0
  58. package/dist/LabeledCheckBox.js.map +1 -0
  59. package/dist/LineChart.d.ts.map +1 -1
  60. package/dist/LineChart.js +2 -2
  61. package/dist/LineChart.js.map +1 -1
  62. package/dist/ListDetailLayout.d.ts +36 -0
  63. package/dist/ListDetailLayout.d.ts.map +1 -0
  64. package/dist/ListDetailLayout.js +153 -0
  65. package/dist/ListDetailLayout.js.map +1 -0
  66. package/dist/MarkdownEditor.d.ts.map +1 -1
  67. package/dist/MarkdownEditor.js +2 -1
  68. package/dist/MarkdownEditor.js.map +1 -1
  69. package/dist/MediaQuery.d.ts +1 -1
  70. package/dist/MediaQuery.d.ts.map +1 -1
  71. package/dist/MediaQuery.js +1 -1
  72. package/dist/MediaQuery.js.map +1 -1
  73. package/dist/OtpField.d.ts +60 -0
  74. package/dist/OtpField.d.ts.map +1 -0
  75. package/dist/OtpField.js +154 -0
  76. package/dist/OtpField.js.map +1 -0
  77. package/dist/Page.d.ts +1 -0
  78. package/dist/Page.d.ts.map +1 -1
  79. package/dist/Page.js +10 -1
  80. package/dist/Page.js.map +1 -1
  81. package/dist/ProvisionalConsentSigner.d.ts +21 -0
  82. package/dist/ProvisionalConsentSigner.d.ts.map +1 -0
  83. package/dist/ProvisionalConsentSigner.js +199 -0
  84. package/dist/ProvisionalConsentSigner.js.map +1 -0
  85. package/dist/ResponsiveBreakpoint.d.ts +5 -0
  86. package/dist/ResponsiveBreakpoint.d.ts.map +1 -1
  87. package/dist/ResponsiveBreakpoint.js +7 -0
  88. package/dist/ResponsiveBreakpoint.js.map +1 -1
  89. package/dist/Scorecard.d.ts.map +1 -1
  90. package/dist/Scorecard.js +5 -2
  91. package/dist/Scorecard.js.map +1 -1
  92. package/dist/Skeleton.d.ts +39 -0
  93. package/dist/Skeleton.d.ts.map +1 -0
  94. package/dist/Skeleton.js +195 -0
  95. package/dist/Skeleton.js.map +1 -0
  96. package/dist/SocialLoginButton.d.ts +10 -0
  97. package/dist/SocialLoginButton.d.ts.map +1 -1
  98. package/dist/SocialLoginButton.js +26 -8
  99. package/dist/SocialLoginButton.js.map +1 -1
  100. package/dist/SplitPage.d.ts +12 -1
  101. package/dist/SplitPage.d.ts.map +1 -1
  102. package/dist/SplitPage.js +14 -7
  103. package/dist/SplitPage.js.map +1 -1
  104. package/dist/SplitPage.native.d.ts +1 -1
  105. package/dist/SplitPage.native.d.ts.map +1 -1
  106. package/dist/SplitPage.native.js +6 -3
  107. package/dist/SplitPage.native.js.map +1 -1
  108. package/dist/SplitPageLoading.d.ts +14 -0
  109. package/dist/SplitPageLoading.d.ts.map +1 -0
  110. package/dist/SplitPageLoading.js +19 -0
  111. package/dist/SplitPageLoading.js.map +1 -0
  112. package/dist/SyncStatusBanner.d.ts +7 -1
  113. package/dist/SyncStatusBanner.d.ts.map +1 -1
  114. package/dist/SyncStatusBanner.js +20 -4
  115. package/dist/SyncStatusBanner.js.map +1 -1
  116. package/dist/Text.d.ts +3 -1
  117. package/dist/Text.d.ts.map +1 -1
  118. package/dist/Text.js +2 -0
  119. package/dist/Text.js.map +1 -1
  120. package/dist/TextField.d.ts.map +1 -1
  121. package/dist/TextField.js +44 -10
  122. package/dist/TextField.js.map +1 -1
  123. package/dist/charts/ChartFrame.d.ts +2 -2
  124. package/dist/charts/ChartFrame.d.ts.map +1 -1
  125. package/dist/charts/ChartFrame.js +9 -2
  126. package/dist/charts/ChartFrame.js.map +1 -1
  127. package/dist/fieldElements/FieldError.d.ts.map +1 -1
  128. package/dist/fieldElements/FieldError.js +1 -1
  129. package/dist/fieldElements/FieldError.js.map +1 -1
  130. package/dist/index.d.ts +21 -3
  131. package/dist/index.d.ts.map +1 -1
  132. package/dist/index.js +16 -1
  133. package/dist/index.js.map +1 -1
  134. package/dist/lazyBoundaries/heavyOptionalExports.d.ts +3 -0
  135. package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
  136. package/dist/lazyBoundaries/heavyOptionalExports.js +2 -0
  137. package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
  138. package/dist/login/LastUsedBadge.d.ts +14 -0
  139. package/dist/login/LastUsedBadge.d.ts.map +1 -0
  140. package/dist/login/LastUsedBadge.js +23 -0
  141. package/dist/login/LastUsedBadge.js.map +1 -0
  142. package/dist/login/LoginAccountCard.d.ts +38 -0
  143. package/dist/login/LoginAccountCard.d.ts.map +1 -0
  144. package/dist/login/LoginAccountCard.js +72 -0
  145. package/dist/login/LoginAccountCard.js.map +1 -0
  146. package/dist/login/LoginAccountList.d.ts +33 -0
  147. package/dist/login/LoginAccountList.d.ts.map +1 -0
  148. package/dist/login/LoginAccountList.js +30 -0
  149. package/dist/login/LoginAccountList.js.map +1 -0
  150. package/dist/login/LoginScreen.d.ts +4 -4
  151. package/dist/login/LoginScreen.d.ts.map +1 -1
  152. package/dist/login/LoginScreen.js +294 -13
  153. package/dist/login/LoginScreen.js.map +1 -1
  154. package/dist/login/OutlinePill.d.ts +28 -0
  155. package/dist/login/OutlinePill.d.ts.map +1 -0
  156. package/dist/login/OutlinePill.js +48 -0
  157. package/dist/login/OutlinePill.js.map +1 -0
  158. package/dist/login/TwoFactorChallengeScreen.d.ts +26 -0
  159. package/dist/login/TwoFactorChallengeScreen.d.ts.map +1 -0
  160. package/dist/login/TwoFactorChallengeScreen.js +139 -0
  161. package/dist/login/TwoFactorChallengeScreen.js.map +1 -0
  162. package/dist/login/loginMethods.d.ts +27 -0
  163. package/dist/login/loginMethods.d.ts.map +1 -0
  164. package/dist/login/loginMethods.js +37 -0
  165. package/dist/login/loginMethods.js.map +1 -0
  166. package/dist/login/loginTypes.d.ts +202 -5
  167. package/dist/login/loginTypes.d.ts.map +1 -1
  168. package/dist/pdfHtmlTemplate.d.ts +17 -0
  169. package/dist/pdfHtmlTemplate.d.ts.map +1 -1
  170. package/dist/pdfHtmlTemplate.js +78 -0
  171. package/dist/pdfHtmlTemplate.js.map +1 -1
  172. package/dist/security/AccountSecurityScreen.d.ts +10 -0
  173. package/dist/security/AccountSecurityScreen.d.ts.map +1 -0
  174. package/dist/security/AccountSecurityScreen.js +50 -0
  175. package/dist/security/AccountSecurityScreen.js.map +1 -0
  176. package/dist/security/LinkedAccountsCard.d.ts +32 -0
  177. package/dist/security/LinkedAccountsCard.d.ts.map +1 -0
  178. package/dist/security/LinkedAccountsCard.js +145 -0
  179. package/dist/security/LinkedAccountsCard.js.map +1 -0
  180. package/dist/security/PasskeysCard.d.ts +39 -0
  181. package/dist/security/PasskeysCard.d.ts.map +1 -0
  182. package/dist/security/PasskeysCard.js +168 -0
  183. package/dist/security/PasskeysCard.js.map +1 -0
  184. package/dist/security/PasswordCard.d.ts +32 -0
  185. package/dist/security/PasswordCard.d.ts.map +1 -0
  186. package/dist/security/PasswordCard.js +198 -0
  187. package/dist/security/PasswordCard.js.map +1 -0
  188. package/dist/security/SessionsCard.d.ts +39 -0
  189. package/dist/security/SessionsCard.d.ts.map +1 -0
  190. package/dist/security/SessionsCard.js +168 -0
  191. package/dist/security/SessionsCard.js.map +1 -0
  192. package/dist/security/TwoFactorSettingsCard.d.ts +55 -0
  193. package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -0
  194. package/dist/security/TwoFactorSettingsCard.js +458 -0
  195. package/dist/security/TwoFactorSettingsCard.js.map +1 -0
  196. package/dist/security/securityMethods.d.ts +40 -0
  197. package/dist/security/securityMethods.d.ts.map +1 -0
  198. package/dist/security/securityMethods.js +66 -0
  199. package/dist/security/securityMethods.js.map +1 -0
  200. package/dist/security/securityTypes.d.ts +467 -0
  201. package/dist/security/securityTypes.d.ts.map +1 -0
  202. package/dist/security/securityTypes.js +2 -0
  203. package/dist/security/securityTypes.js.map +1 -0
  204. package/dist/security/userAgent.d.ts +26 -0
  205. package/dist/security/userAgent.d.ts.map +1 -0
  206. package/dist/security/userAgent.js +80 -0
  207. package/dist/security/userAgent.js.map +1 -0
  208. package/dist/signUp/OAuthButtons.d.ts +28 -5
  209. package/dist/signUp/OAuthButtons.d.ts.map +1 -1
  210. package/dist/signUp/OAuthButtons.js +44 -5
  211. package/dist/signUp/OAuthButtons.js.map +1 -1
  212. package/dist/signUp/SignUpScreen.d.ts.map +1 -1
  213. package/dist/signUp/SignUpScreen.js +50 -5
  214. package/dist/signUp/SignUpScreen.js.map +1 -1
  215. package/dist/signUp/signUpTypes.d.ts +21 -1
  216. package/dist/signUp/signUpTypes.d.ts.map +1 -1
  217. package/dist/useConsentForms.d.ts +9 -0
  218. package/dist/useConsentForms.d.ts.map +1 -1
  219. package/dist/useConsentForms.js +18 -0
  220. package/dist/useConsentForms.js.map +1 -1
  221. package/dist/useProvisionalConsent.d.ts +124 -0
  222. package/dist/useProvisionalConsent.d.ts.map +1 -0
  223. package/dist/useProvisionalConsent.js +79 -0
  224. package/dist/useProvisionalConsent.js.map +1 -0
  225. package/dist/withLoading.d.ts +20 -0
  226. package/dist/withLoading.d.ts.map +1 -0
  227. package/dist/withLoading.js +24 -0
  228. package/dist/withLoading.js.map +1 -0
  229. package/package.json +4 -2
  230. package/src/Accordion.test.tsx +5 -0
  231. package/src/Accordion.tsx +3 -8
  232. package/src/AreaChart.test.tsx +15 -0
  233. package/src/AreaChart.tsx +6 -0
  234. package/src/Avatar.test.tsx +8 -0
  235. package/src/Avatar.tsx +2 -0
  236. package/src/BarChart.test.tsx +15 -0
  237. package/src/BarChart.tsx +6 -0
  238. package/src/Box.test.tsx +131 -1
  239. package/src/Box.tsx +77 -8
  240. package/src/Card.test.tsx +48 -0
  241. package/src/Card.tsx +13 -2
  242. package/src/ChartCard.test.tsx +30 -0
  243. package/src/ChartCard.tsx +18 -1
  244. package/src/Common.ts +263 -14
  245. package/src/ConsentFormScreen.test.tsx +84 -0
  246. package/src/ConsentFormScreen.tsx +28 -4
  247. package/src/DateTimeField.tsx +5 -3
  248. package/src/DonutChart.test.tsx +15 -0
  249. package/src/DonutChart.tsx +6 -0
  250. package/src/Field.tsx +2 -0
  251. package/src/FilePickerButton.test.tsx +35 -0
  252. package/src/FormContainer.test.tsx +242 -0
  253. package/src/FormContainer.tsx +177 -0
  254. package/src/GPTChat.test.tsx +16 -0
  255. package/src/GPTChat.tsx +8 -2
  256. package/src/Heading.test.tsx +14 -0
  257. package/src/Heading.tsx +6 -1
  258. package/src/Icon.test.tsx +52 -1
  259. package/src/Icon.tsx +92 -3
  260. package/src/IconButton.test.tsx +85 -0
  261. package/src/LabeledCheckBox.test.tsx +45 -0
  262. package/src/LabeledCheckBox.tsx +89 -0
  263. package/src/LineChart.test.tsx +18 -0
  264. package/src/LineChart.tsx +6 -0
  265. package/src/ListDetailLayout.tsx +247 -0
  266. package/src/MarkdownEditor.tsx +2 -1
  267. package/src/MediaQuery.ts +1 -0
  268. package/src/OtpField.test.tsx +280 -0
  269. package/src/OtpField.tsx +307 -0
  270. package/src/Page.test.tsx +38 -0
  271. package/src/Page.tsx +38 -19
  272. package/src/ProvisionalConsentSigner.test.tsx +625 -0
  273. package/src/ProvisionalConsentSigner.tsx +423 -0
  274. package/src/ResponsiveBreakpoint.test.tsx +7 -0
  275. package/src/ResponsiveBreakpoint.tsx +14 -0
  276. package/src/RootImportRegression.test.tsx +3 -0
  277. package/src/Scorecard.test.tsx +11 -0
  278. package/src/Scorecard.tsx +21 -0
  279. package/src/Skeleton.test.tsx +262 -0
  280. package/src/Skeleton.tsx +430 -0
  281. package/src/SocialLoginButton.test.tsx +14 -0
  282. package/src/SocialLoginButton.tsx +51 -7
  283. package/src/SplitPage.native.tsx +16 -3
  284. package/src/SplitPage.tsx +29 -12
  285. package/src/SplitPageLoading.test.tsx +51 -0
  286. package/src/SplitPageLoading.tsx +53 -0
  287. package/src/SyncStatusBanner.test.tsx +61 -4
  288. package/src/SyncStatusBanner.tsx +29 -3
  289. package/src/Text.tsx +4 -1
  290. package/src/TextField.test.tsx +150 -0
  291. package/src/TextField.tsx +66 -8
  292. package/src/__snapshots__/AddressField.test.tsx.snap +12 -0
  293. package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +15 -0
  294. package/src/__snapshots__/Avatar.test.tsx.snap +1 -0
  295. package/src/__snapshots__/CustomSelectField.test.tsx.snap +3 -0
  296. package/src/__snapshots__/EmailField.test.tsx.snap +6 -0
  297. package/src/__snapshots__/Field.test.tsx.snap +37 -0
  298. package/src/__snapshots__/HeightField.test.tsx.snap +2 -0
  299. package/src/__snapshots__/MobileAddressAutoComplete.test.tsx.snap +3 -0
  300. package/src/__snapshots__/NumberField.test.tsx.snap +3 -0
  301. package/src/__snapshots__/Page.test.tsx.snap +28 -5
  302. package/src/__snapshots__/PhoneNumberField.test.tsx.snap +12 -0
  303. package/src/__snapshots__/Slider.test.tsx.snap +1 -0
  304. package/src/__snapshots__/SocialLoginButton.test.tsx.snap +43 -22
  305. package/src/__snapshots__/SplitPage.test.tsx.snap +16 -0
  306. package/src/__snapshots__/TapToEdit.test.tsx.snap +3 -0
  307. package/src/__snapshots__/TextArea.test.tsx.snap +17 -1
  308. package/src/__snapshots__/TextField.test.tsx.snap +19 -2
  309. package/src/__snapshots__/TypedSignatureField.test.tsx.snap +3 -0
  310. package/src/__snapshots__/UnifiedAddressAutoComplete.test.tsx.snap +18 -0
  311. package/src/__snapshots__/WebAddressAutocomplete.test.tsx.snap +12 -0
  312. package/src/bunSetup.ts +13 -1
  313. package/src/charts/ChartFrame.tsx +19 -8
  314. package/src/fieldElements/FieldError.tsx +1 -0
  315. package/src/fieldElements/__snapshots__/FieldError.test.tsx.snap +2 -0
  316. package/src/index.tsx +44 -1
  317. package/src/isolated/ListDetailLayout.isolated.tsx +634 -0
  318. package/src/lazyBoundaries/heavyOptionalExports.tsx +7 -0
  319. package/src/login/LastUsedBadge.tsx +40 -0
  320. package/src/login/LoginAccountCard.tsx +205 -0
  321. package/src/login/LoginAccountList.tsx +115 -0
  322. package/src/login/LoginScreen.test.tsx +837 -1
  323. package/src/login/LoginScreen.tsx +674 -91
  324. package/src/login/OutlinePill.tsx +110 -0
  325. package/src/login/TwoFactorChallengeScreen.test.tsx +376 -0
  326. package/src/login/TwoFactorChallengeScreen.tsx +307 -0
  327. package/src/login/__snapshots__/LoginScreen.test.tsx.snap +1454 -334
  328. package/src/login/loginMethods.test.ts +53 -0
  329. package/src/login/loginMethods.ts +60 -0
  330. package/src/login/loginTypes.ts +208 -5
  331. package/src/pdfHtmlTemplate.test.ts +243 -4
  332. package/src/pdfHtmlTemplate.ts +93 -0
  333. package/src/security/AccountSecurityScreen.test.tsx +262 -0
  334. package/src/security/AccountSecurityScreen.tsx +156 -0
  335. package/src/security/LinkedAccountsCard.test.tsx +371 -0
  336. package/src/security/LinkedAccountsCard.tsx +297 -0
  337. package/src/security/PasskeysCard.test.tsx +378 -0
  338. package/src/security/PasskeysCard.tsx +371 -0
  339. package/src/security/PasswordCard.test.tsx +445 -0
  340. package/src/security/PasswordCard.tsx +342 -0
  341. package/src/security/SessionsCard.test.tsx +349 -0
  342. package/src/security/SessionsCard.tsx +362 -0
  343. package/src/security/TwoFactorSettingsCard.test.tsx +700 -0
  344. package/src/security/TwoFactorSettingsCard.tsx +872 -0
  345. package/src/security/securityMethods.test.ts +101 -0
  346. package/src/security/securityMethods.ts +104 -0
  347. package/src/security/securityTypes.ts +480 -0
  348. package/src/security/userAgent.test.ts +119 -0
  349. package/src/security/userAgent.ts +102 -0
  350. package/src/signUp/OAuthButtons.test.tsx +27 -0
  351. package/src/signUp/OAuthButtons.tsx +153 -19
  352. package/src/signUp/SignUpScreen.test.tsx +170 -1
  353. package/src/signUp/SignUpScreen.tsx +87 -25
  354. package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +22 -11
  355. package/src/signUp/signUpTypes.ts +23 -1
  356. package/src/useConsentForms.test.ts +37 -1
  357. package/src/useConsentForms.ts +25 -0
  358. package/src/useProvisionalConsent.test.ts +256 -0
  359. package/src/useProvisionalConsent.ts +218 -0
  360. package/src/withLoading.test.tsx +61 -0
  361. package/src/withLoading.tsx +50 -0
@@ -0,0 +1,700 @@
1
+ import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {act, fireEvent, render, waitFor} from "@testing-library/react-native";
3
+ import {setStringAsync} from "expo-clipboard";
4
+ import type {ReactElement} from "react";
5
+ import {AccessibilityInfo, Platform, Share} from "react-native";
6
+
7
+ import {OtpField} from "../OtpField";
8
+ import {ThemeProvider} from "../Theme";
9
+ import {renderWithTheme} from "../test-utils";
10
+ import type {TwoFactorEnrollment} from "./securityTypes";
11
+ import {parseTotpDigits, parseTotpSecret, TwoFactorSettingsCard} from "./TwoFactorSettingsCard";
12
+
13
+ const SECRET = "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP";
14
+ const ENROLLMENT: TwoFactorEnrollment = {
15
+ backupCodes: ["aaaa-1111", "bbbb-2222", "cccc-3333"],
16
+ totpURI: `otpauth://totp/Terreno:jo%40example.com?secret=${SECRET}&issuer=Terreno&digits=6&period=30`,
17
+ };
18
+ const NEW_CODES = ["dddd-4444", "eeee-5555"];
19
+
20
+ const announce = AccessibilityInfo.announceForAccessibility as ReturnType<typeof mock>;
21
+
22
+ const press = async (element: Parameters<typeof fireEvent.press>[0]): Promise<void> => {
23
+ await act(async () => {
24
+ fireEvent.press(element);
25
+ });
26
+ };
27
+
28
+ const typeText = async (
29
+ element: Parameters<typeof fireEvent.changeText>[0],
30
+ text: string
31
+ ): Promise<void> => {
32
+ await act(async () => {
33
+ fireEvent.changeText(element, text);
34
+ });
35
+ };
36
+
37
+ const isDisabled = (element: {props: Record<string, unknown>}): boolean => {
38
+ const state = element.props.accessibilityState as {disabled?: boolean} | undefined;
39
+ return Boolean(state?.disabled);
40
+ };
41
+
42
+ const renderOffCard = (
43
+ overrides: Partial<Parameters<typeof TwoFactorSettingsCard>[0]> = {}
44
+ ): {
45
+ onStartEnroll: ReturnType<typeof mock>;
46
+ onVerify: ReturnType<typeof mock>;
47
+ utils: ReturnType<typeof renderWithTheme>;
48
+ } => {
49
+ const onStartEnroll = mock((_password?: string) => Promise.resolve(ENROLLMENT));
50
+ const onVerify = mock((_code: string) => Promise.resolve());
51
+ const utils = renderWithTheme(
52
+ <TwoFactorSettingsCard
53
+ enabled={false}
54
+ onStartEnroll={onStartEnroll}
55
+ onVerify={onVerify}
56
+ {...overrides}
57
+ />
58
+ );
59
+ return {onStartEnroll, onVerify, utils};
60
+ };
61
+
62
+ describe("parseTotpDigits", () => {
63
+ it("reads the digits parameter from an otpauth URI", () => {
64
+ expect(parseTotpDigits(ENROLLMENT.totpURI)).toBe(6);
65
+ expect(parseTotpDigits("otpauth://totp/x?secret=A&digits=8")).toBe(8);
66
+ });
67
+
68
+ it("returns undefined when digits is missing or out of range", () => {
69
+ expect(parseTotpDigits("otpauth://totp/x?secret=A")).toBeUndefined();
70
+ expect(parseTotpDigits("otpauth://totp/x?digits=abc")).toBeUndefined();
71
+ expect(parseTotpDigits("otpauth://totp/x?digits=40")).toBeUndefined();
72
+ });
73
+ });
74
+
75
+ describe("parseTotpSecret", () => {
76
+ it("reads the secret parameter from an otpauth URI", () => {
77
+ expect(parseTotpSecret(ENROLLMENT.totpURI)).toBe(SECRET);
78
+ expect(parseTotpSecret(`otpauth://totp/x?issuer=A&secret=${SECRET}`)).toBe(SECRET);
79
+ });
80
+
81
+ it("returns undefined when the URI has no secret", () => {
82
+ expect(parseTotpSecret("otpauth://totp/x?issuer=A")).toBeUndefined();
83
+ expect(parseTotpSecret("not a uri")).toBeUndefined();
84
+ });
85
+ });
86
+
87
+ describe("TwoFactorSettingsCard", () => {
88
+ describe("when two-factor is off", () => {
89
+ it("shows Off and a Turn on button, and hides the enrolled actions", () => {
90
+ const {utils} = renderOffCard({
91
+ onDisable: mock(() => Promise.resolve()),
92
+ onRegenerateBackupCodes: mock(() => Promise.resolve(NEW_CODES)),
93
+ });
94
+ expect(utils.getByTestId("two-factor-settings-card-status").props.children).toBe("Off");
95
+ expect(utils.getByTestId("two-factor-settings-card-enable")).toBeTruthy();
96
+ expect(utils.queryByTestId("two-factor-settings-card-regenerate")).toBeNull();
97
+ expect(utils.queryByTestId("two-factor-settings-card-disable")).toBeNull();
98
+ });
99
+
100
+ it("walks through password, QR code, verify and backup codes in order", async () => {
101
+ const {onStartEnroll, onVerify, utils} = renderOffCard();
102
+ const {getByTestId, queryByTestId} = utils;
103
+
104
+ await press(getByTestId("two-factor-settings-card-enable"));
105
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 1 of 4");
106
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
107
+ await press(getByTestId("two-factor-settings-card-password-submit"));
108
+ expect(onStartEnroll).toHaveBeenCalledWith("hunter22");
109
+
110
+ // Step 2: scan the QR code, or type the secret.
111
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 2 of 4");
112
+ expect(getByTestId("two-factor-settings-card-qr")).toBeTruthy();
113
+ expect(getByTestId("two-factor-settings-card-secret").props.children).toBe(
114
+ "JBSW Y3DP EHPK 3PXP JBSW Y3DP EHPK 3PXP"
115
+ );
116
+ expect(queryByTestId("two-factor-settings-card-otp")).toBeNull();
117
+ await press(getByTestId("two-factor-settings-card-next"));
118
+
119
+ // Step 3: a full code submits itself.
120
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 3 of 4");
121
+ await typeText(getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
122
+ expect(onVerify).toHaveBeenCalledWith("123456");
123
+
124
+ // Step 4: the backup codes.
125
+ await waitFor(() => {
126
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 4 of 4");
127
+ });
128
+ ENROLLMENT.backupCodes.forEach((code, index) => {
129
+ expect(
130
+ getByTestId(`two-factor-settings-card-backup-code-${index + 1}`).props.children
131
+ ).toBe(code);
132
+ });
133
+ });
134
+
135
+ it("gates Finish on 'I saved these' and returns to the card when finished", async () => {
136
+ const {utils} = renderOffCard();
137
+ const {getByTestId, queryByTestId} = utils;
138
+ await press(getByTestId("two-factor-settings-card-enable"));
139
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
140
+ await press(getByTestId("two-factor-settings-card-password-submit"));
141
+ await press(getByTestId("two-factor-settings-card-next"));
142
+ await typeText(getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
143
+ await waitFor(() => {
144
+ expect(getByTestId("two-factor-settings-card-finish")).toBeTruthy();
145
+ });
146
+
147
+ const saved = getByTestId("two-factor-settings-card-saved");
148
+ expect(saved.props.role).toBe("checkbox");
149
+ expect(saved.props.accessibilityLabel).toBe("I saved these backup codes");
150
+ expect(isDisabled(getByTestId("two-factor-settings-card-finish"))).toBe(true);
151
+ // The step offers no way out but Finish, so the codes can't be skipped.
152
+ expect(queryByTestId("two-factor-settings-card-cancel")).toBeNull();
153
+
154
+ await press(saved);
155
+ expect(isDisabled(getByTestId("two-factor-settings-card-finish"))).toBe(false);
156
+ await press(saved);
157
+ expect(isDisabled(getByTestId("two-factor-settings-card-finish"))).toBe(true);
158
+ await press(saved);
159
+ await press(getByTestId("two-factor-settings-card-finish"));
160
+ expect(queryByTestId("two-factor-settings-card-backup-code-1")).toBeNull();
161
+ expect(getByTestId("two-factor-settings-card-enable")).toBeTruthy();
162
+ });
163
+
164
+ it("keeps showing the backup codes after enabled flips to true", async () => {
165
+ const onStartEnroll = mock(() => Promise.resolve(ENROLLMENT));
166
+ const onVerify = mock(() => Promise.resolve());
167
+ const {getByTestId, rerender} = renderWithTheme(
168
+ <TwoFactorSettingsCard enabled={false} onStartEnroll={onStartEnroll} onVerify={onVerify} />
169
+ );
170
+ await press(getByTestId("two-factor-settings-card-enable"));
171
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
172
+ await press(getByTestId("two-factor-settings-card-password-submit"));
173
+ await press(getByTestId("two-factor-settings-card-next"));
174
+ await typeText(getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
175
+ await waitFor(() => {
176
+ expect(getByTestId("two-factor-settings-card-backup-code-1")).toBeTruthy();
177
+ });
178
+ rerender(<TwoFactorSettingsCard enabled onStartEnroll={onStartEnroll} onVerify={onVerify} />);
179
+ expect(getByTestId("two-factor-settings-card-backup-code-1")).toBeTruthy();
180
+ });
181
+
182
+ it("puts the password step in a form on web with the account's username", async () => {
183
+ const originalOS = Platform.OS;
184
+ Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
185
+ try {
186
+ const {onStartEnroll, utils} = renderOffCard({username: "ada@example.com"});
187
+ const {UNSAFE_getByType, getByTestId} = utils;
188
+ await press(getByTestId("two-factor-settings-card-enable"));
189
+ const form = UNSAFE_getByType("form" as never);
190
+ expect(form.props["data-testid"]).toBe("two-factor-settings-card-password-form");
191
+ expect(UNSAFE_getByType("input" as never).props.value).toBe("ada@example.com");
192
+ const password = getByTestId("two-factor-settings-card-password");
193
+ expect(password.props.autoComplete).toBe("current-password");
194
+ expect(password.props["aria-label"]).toBe("Password");
195
+ await typeText(password, "hunter22");
196
+ await act(async () => {
197
+ fireEvent(form, "submit", {preventDefault: () => {}});
198
+ });
199
+ await waitFor(() => {
200
+ expect(onStartEnroll).toHaveBeenCalledWith("hunter22");
201
+ });
202
+ } finally {
203
+ Object.defineProperty(Platform, "OS", {configurable: true, value: originalOS});
204
+ }
205
+ });
206
+
207
+ it("asks for the password before calling onStartEnroll", async () => {
208
+ const {onStartEnroll, utils} = renderOffCard();
209
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
210
+ await press(utils.getByTestId("two-factor-settings-card-password-submit"));
211
+ expect(onStartEnroll).not.toHaveBeenCalled();
212
+ expect(utils.getByText("Enter your password.")).toBeTruthy();
213
+ });
214
+
215
+ it("shows a rejected enrollment on the password step", async () => {
216
+ const {utils} = renderOffCard({
217
+ onStartEnroll: mock(() => Promise.reject(new Error("Invalid password"))),
218
+ });
219
+ const {getByTestId} = utils;
220
+ await press(getByTestId("two-factor-settings-card-enable"));
221
+ await typeText(getByTestId("two-factor-settings-card-password"), "wrong");
222
+ await press(getByTestId("two-factor-settings-card-password-submit"));
223
+ await waitFor(() => {
224
+ expect(getByTestId("two-factor-settings-card-error")).toBeTruthy();
225
+ });
226
+ expect(getByTestId("two-factor-settings-card-error").props.role).toBe("alert");
227
+ expect(utils.getByText("Invalid password")).toBeTruthy();
228
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 1 of 4");
229
+ });
230
+
231
+ it("shows a rejected code, clears it, and stays on the verify step", async () => {
232
+ const onVerify = mock(() => Promise.reject(new Error("Invalid code")));
233
+ const {utils} = renderOffCard({onVerify});
234
+ const {getByTestId} = utils;
235
+ await press(getByTestId("two-factor-settings-card-enable"));
236
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
237
+ await press(getByTestId("two-factor-settings-card-password-submit"));
238
+ await press(getByTestId("two-factor-settings-card-next"));
239
+ await typeText(getByTestId("two-factor-settings-card-otp-cell-1"), "999999");
240
+ await waitFor(() => {
241
+ expect(utils.getByText("Invalid code")).toBeTruthy();
242
+ });
243
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 3 of 4");
244
+ expect(getByTestId("two-factor-settings-card-otp-cell-1").props.value).toBe("");
245
+ expect(isDisabled(getByTestId("two-factor-settings-card-verify"))).toBe(true);
246
+ });
247
+
248
+ it("cancels enrollment from any step before the codes without verifying", async () => {
249
+ const {onStartEnroll, onVerify, utils} = renderOffCard();
250
+ const {getByTestId, queryByTestId} = utils;
251
+ await press(getByTestId("two-factor-settings-card-enable"));
252
+ await press(getByTestId("two-factor-settings-card-cancel"));
253
+ expect(queryByTestId("two-factor-settings-card-step")).toBeNull();
254
+ expect(onStartEnroll).not.toHaveBeenCalled();
255
+
256
+ await press(getByTestId("two-factor-settings-card-enable"));
257
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
258
+ await press(getByTestId("two-factor-settings-card-password-submit"));
259
+ await press(getByTestId("two-factor-settings-card-next"));
260
+ await press(getByTestId("two-factor-settings-card-back"));
261
+ expect(getByTestId("two-factor-settings-card-step").props.children).toBe("Step 2 of 4");
262
+ await press(getByTestId("two-factor-settings-card-cancel"));
263
+ expect(queryByTestId("two-factor-settings-card-step")).toBeNull();
264
+ expect(onVerify).not.toHaveBeenCalled();
265
+
266
+ // Starting again asks for the password again.
267
+ await press(getByTestId("two-factor-settings-card-enable"));
268
+ expect(getByTestId("two-factor-settings-card-password").props.value).toBe("");
269
+ });
270
+
271
+ it("skips the password step when requirePassword is false", async () => {
272
+ const {onStartEnroll, utils} = renderOffCard({requirePassword: false});
273
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
274
+ expect(onStartEnroll).toHaveBeenCalledWith(undefined);
275
+ expect(utils.queryByTestId("two-factor-settings-card-password")).toBeNull();
276
+ expect(utils.getByTestId("two-factor-settings-card-step").props.children).toBe("Step 1 of 3");
277
+ expect(utils.getByTestId("two-factor-settings-card-qr")).toBeTruthy();
278
+ });
279
+
280
+ it("asks a user without a password to set one first", () => {
281
+ const {utils} = renderOffCard({hasPassword: false});
282
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-enable"))).toBe(true);
283
+ expect(utils.getByTestId("two-factor-settings-card-needs-password")).toBeTruthy();
284
+ });
285
+
286
+ it("lets a user without a password enroll when the server allows passwordless", () => {
287
+ const {utils} = renderOffCard({hasPassword: false, requirePassword: false});
288
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-enable"))).toBe(false);
289
+ expect(utils.queryByTestId("two-factor-settings-card-needs-password")).toBeNull();
290
+ });
291
+
292
+ it("takes the code length from the totpURI when digits is not passed", async () => {
293
+ const {onVerify, utils} = renderOffCard({
294
+ onStartEnroll: mock(() =>
295
+ Promise.resolve({
296
+ backupCodes: ENROLLMENT.backupCodes,
297
+ totpURI: `otpauth://totp/x?secret=${SECRET}&digits=8`,
298
+ })
299
+ ),
300
+ requirePassword: false,
301
+ });
302
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
303
+ await press(utils.getByTestId("two-factor-settings-card-next"));
304
+ expect(utils.getByTestId("two-factor-settings-card-otp-cell-8")).toBeTruthy();
305
+ expect(utils.getByText("Enter the 8-digit code your authenticator app shows.")).toBeTruthy();
306
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
307
+ expect(onVerify).not.toHaveBeenCalled();
308
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "12345678");
309
+ expect(onVerify).toHaveBeenCalledWith("12345678");
310
+ });
311
+
312
+ it("uses 6 digits when the totpURI has no digits parameter", async () => {
313
+ const {utils} = renderOffCard({
314
+ onStartEnroll: mock(() =>
315
+ Promise.resolve({backupCodes: [], totpURI: `otpauth://totp/x?secret=${SECRET}`})
316
+ ),
317
+ requirePassword: false,
318
+ });
319
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
320
+ await press(utils.getByTestId("two-factor-settings-card-next"));
321
+ expect(utils.getByTestId("two-factor-settings-card-otp-cell-6")).toBeTruthy();
322
+ expect(utils.queryByTestId("two-factor-settings-card-otp-cell-7")).toBeNull();
323
+ });
324
+
325
+ it("does not verify a partial code, and Verify submits a full one", async () => {
326
+ const {onVerify, utils} = renderOffCard({requirePassword: false});
327
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
328
+ await press(utils.getByTestId("two-factor-settings-card-next"));
329
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123");
330
+ expect(onVerify).not.toHaveBeenCalled();
331
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-verify"))).toBe(true);
332
+ await press(utils.getByTestId("two-factor-settings-card-verify"));
333
+ expect(onVerify).not.toHaveBeenCalled();
334
+
335
+ // A code that fills the field without onComplete (for example, a value set by the app)
336
+ // is submitted by the Verify button.
337
+ await act(async () => {
338
+ utils.UNSAFE_getByType(OtpField).props.onChange("654321");
339
+ });
340
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-verify"))).toBe(false);
341
+ await press(utils.getByTestId("two-factor-settings-card-verify"));
342
+ expect(onVerify).toHaveBeenCalledTimes(1);
343
+ expect(onVerify).toHaveBeenCalledWith("654321");
344
+ await waitFor(() => {
345
+ expect(utils.getByTestId("two-factor-settings-card-backup-code-1")).toBeTruthy();
346
+ });
347
+ });
348
+
349
+ it("announces each step on native, and not on first render", async () => {
350
+ announce.mockClear();
351
+ const {utils} = renderOffCard();
352
+ expect(announce).not.toHaveBeenCalled();
353
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
354
+ expect(announce).toHaveBeenLastCalledWith(
355
+ "Step 1 of 4. Enter your password to turn on two-factor authentication."
356
+ );
357
+ await typeText(utils.getByTestId("two-factor-settings-card-password"), "hunter22");
358
+ await press(utils.getByTestId("two-factor-settings-card-password-submit"));
359
+ expect(announce.mock.calls.at(-1)?.[0]).toStartWith("Step 2 of 4. Scan this QR code");
360
+ await press(utils.getByTestId("two-factor-settings-card-next"));
361
+ expect(announce).toHaveBeenLastCalledWith(
362
+ "Step 3 of 4. Enter the 6-digit code your authenticator app shows."
363
+ );
364
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
365
+ await waitFor(() => {
366
+ expect(announce).toHaveBeenLastCalledWith(
367
+ "Two-factor authentication is on. Save your backup codes."
368
+ );
369
+ });
370
+ await press(utils.getByTestId("two-factor-settings-card-saved"));
371
+ await press(utils.getByTestId("two-factor-settings-card-finish"));
372
+ expect(announce).toHaveBeenLastCalledWith("Two-factor authentication is on.");
373
+ });
374
+
375
+ it("moves focus to each step's heading on web, and to the field on input steps", async () => {
376
+ const originalOS = Platform.OS;
377
+ Platform.OS = "web";
378
+ announce.mockClear();
379
+ const focused: string[] = [];
380
+ const createNodeMock = (element: ReactElement): {focus: () => void} => ({
381
+ focus: (): void => {
382
+ focused.push((element.props as {testID?: string}).testID ?? "");
383
+ },
384
+ });
385
+ try {
386
+ const {getByTestId} = render(
387
+ <TwoFactorSettingsCard
388
+ enabled={false}
389
+ onStartEnroll={mock(() => Promise.resolve(ENROLLMENT))}
390
+ onVerify={mock(() => Promise.resolve())}
391
+ />,
392
+ {createNodeMock, wrapper: ThemeProvider}
393
+ );
394
+ expect(focused).toEqual([]);
395
+ await press(getByTestId("two-factor-settings-card-enable"));
396
+ expect(focused.at(-1)).toBe("two-factor-settings-card-password");
397
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
398
+ await press(getByTestId("two-factor-settings-card-password-submit"));
399
+ expect(focused.at(-1)).toBe("two-factor-settings-card-step-heading");
400
+ expect(getByTestId("two-factor-settings-card-step-heading").props.tabIndex).toBe(-1);
401
+ await press(getByTestId("two-factor-settings-card-next"));
402
+ expect(focused.at(-1)).toBe("two-factor-settings-card-otp-cell-1");
403
+ await typeText(getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
404
+ await waitFor(() => {
405
+ expect(focused.at(-1)).toBe("two-factor-settings-card-step-heading");
406
+ });
407
+ expect(getByTestId("two-factor-settings-card-backup-code-1")).toBeTruthy();
408
+ // Web moves focus instead; react-native-web's announceForAccessibility does nothing.
409
+ expect(announce).not.toHaveBeenCalled();
410
+ } finally {
411
+ Platform.OS = originalOS;
412
+ }
413
+ });
414
+
415
+ it("honors a custom code length", async () => {
416
+ const {onVerify, utils} = renderOffCard({digits: 8, requirePassword: false});
417
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
418
+ await press(utils.getByTestId("two-factor-settings-card-next"));
419
+ expect(utils.getByTestId("two-factor-settings-card-otp-cell-8")).toBeTruthy();
420
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "12345678");
421
+ expect(onVerify).toHaveBeenCalledWith("12345678");
422
+ });
423
+ });
424
+
425
+ describe("copy and download", () => {
426
+ const originalOS = Platform.OS;
427
+
428
+ beforeEach(() => {
429
+ (setStringAsync as ReturnType<typeof mock>).mockClear();
430
+ (Share.share as ReturnType<typeof mock>).mockClear();
431
+ });
432
+
433
+ afterEach(() => {
434
+ Platform.OS = originalOS;
435
+ });
436
+
437
+ const openCodes = async (): Promise<ReturnType<typeof renderWithTheme>> => {
438
+ const {utils} = renderOffCard({requirePassword: false});
439
+ await press(utils.getByTestId("two-factor-settings-card-enable"));
440
+ return utils;
441
+ };
442
+
443
+ it("copies the secret and announces it", async () => {
444
+ const utils = await openCodes();
445
+ announce.mockClear();
446
+ await press(utils.getByTestId("two-factor-settings-card-copy-secret"));
447
+ expect(setStringAsync).toHaveBeenCalledWith(SECRET);
448
+ await waitFor(() => {
449
+ expect(utils.getByText("Copied")).toBeTruthy();
450
+ });
451
+ const status = utils.getByTestId("two-factor-settings-card-copy-secret-status");
452
+ expect(status.props["aria-live"]).toBe("polite");
453
+ expect(status.props.accessibilityLiveRegion).toBe("polite");
454
+ // VoiceOver ignores live regions, so iOS (the test platform) announces it too.
455
+ expect(announce).toHaveBeenCalledWith("Copied");
456
+ });
457
+
458
+ it("copies every backup code, one per line", async () => {
459
+ const utils = await openCodes();
460
+ await press(utils.getByTestId("two-factor-settings-card-next"));
461
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
462
+ await waitFor(() => {
463
+ expect(utils.getByTestId("two-factor-settings-card-copy-codes")).toBeTruthy();
464
+ });
465
+ await press(utils.getByTestId("two-factor-settings-card-copy-codes"));
466
+ expect(setStringAsync).toHaveBeenCalledWith(ENROLLMENT.backupCodes.join("\n"));
467
+ });
468
+
469
+ it("reports a failed copy", async () => {
470
+ (setStringAsync as ReturnType<typeof mock>).mockImplementationOnce(() =>
471
+ Promise.reject(new Error("denied"))
472
+ );
473
+ const utils = await openCodes();
474
+ await press(utils.getByTestId("two-factor-settings-card-copy-secret"));
475
+ await waitFor(() => {
476
+ expect(utils.getByText("Couldn't copy")).toBeTruthy();
477
+ });
478
+ expect(announce).toHaveBeenLastCalledWith("Couldn't copy");
479
+ });
480
+
481
+ it("shares the backup codes on native", async () => {
482
+ const utils = await openCodes();
483
+ await press(utils.getByTestId("two-factor-settings-card-next"));
484
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
485
+ await waitFor(() => {
486
+ expect(utils.getByTestId("two-factor-settings-card-download-codes")).toBeTruthy();
487
+ });
488
+ expect(utils.getByText("Share")).toBeTruthy();
489
+ expect(utils.queryByText("Download")).toBeNull();
490
+ await press(utils.getByTestId("two-factor-settings-card-download-codes"));
491
+ const [content] = (Share.share as ReturnType<typeof mock>).mock.calls[0] as [
492
+ {message: string},
493
+ ];
494
+ expect(content.message).toContain(ENROLLMENT.backupCodes.join("\n"));
495
+ });
496
+
497
+ it("downloads the backup codes as a text file on web", async () => {
498
+ const clicked: {download?: string; href?: string}[] = [];
499
+ const originalDocument = globalThis.document;
500
+ const originalCreate = URL.createObjectURL;
501
+ const originalRevoke = URL.revokeObjectURL;
502
+ const blobs: Blob[] = [];
503
+ URL.createObjectURL = ((blob: Blob): string => {
504
+ blobs.push(blob);
505
+ return "blob:codes";
506
+ }) as typeof URL.createObjectURL;
507
+ // The card revokes the URL later, once the browser has read the blob.
508
+ URL.revokeObjectURL = ((): void => {}) as typeof URL.revokeObjectURL;
509
+ const anchor = {
510
+ click(): void {
511
+ clicked.push({download: anchor.download, href: anchor.href});
512
+ },
513
+ download: "",
514
+ href: "",
515
+ remove: mock(() => {}),
516
+ style: {} as Record<string, string>,
517
+ };
518
+ (globalThis as {document?: unknown}).document = {
519
+ body: {appendChild: mock(() => {})},
520
+ createElement: mock(() => anchor),
521
+ };
522
+ try {
523
+ Platform.OS = "web";
524
+ const utils = await openCodes();
525
+ await press(utils.getByTestId("two-factor-settings-card-next"));
526
+ await typeText(utils.getByTestId("two-factor-settings-card-otp-cell-1"), "123456");
527
+ await waitFor(() => {
528
+ expect(utils.getByTestId("two-factor-settings-card-download-codes")).toBeTruthy();
529
+ });
530
+ expect(utils.getByText("Download")).toBeTruthy();
531
+ expect(
532
+ utils.getByTestId("two-factor-settings-card-download-codes").props.accessibilityLabel
533
+ ).toBe("Download backup codes");
534
+ await press(utils.getByTestId("two-factor-settings-card-download-codes"));
535
+ expect(clicked).toEqual([{download: "two-factor-backup-codes.txt", href: "blob:codes"}]);
536
+ expect(await blobs[0].text()).toContain(ENROLLMENT.backupCodes.join("\n"));
537
+ expect(Share.share).not.toHaveBeenCalled();
538
+ } finally {
539
+ (globalThis as {document?: unknown}).document = originalDocument;
540
+ URL.createObjectURL = originalCreate;
541
+ URL.revokeObjectURL = originalRevoke;
542
+ }
543
+ });
544
+ });
545
+
546
+ describe("when two-factor is on", () => {
547
+ const renderOnCard = (
548
+ overrides: Partial<Parameters<typeof TwoFactorSettingsCard>[0]> = {}
549
+ ): {
550
+ onDisable: ReturnType<typeof mock>;
551
+ onRegenerateBackupCodes: ReturnType<typeof mock>;
552
+ utils: ReturnType<typeof renderWithTheme>;
553
+ } => {
554
+ const onDisable = mock((_password?: string) => Promise.resolve());
555
+ const onRegenerateBackupCodes = mock((_password?: string) => Promise.resolve(NEW_CODES));
556
+ const utils = renderWithTheme(
557
+ <TwoFactorSettingsCard
558
+ enabled
559
+ onDisable={onDisable}
560
+ onRegenerateBackupCodes={onRegenerateBackupCodes}
561
+ onStartEnroll={mock(() => Promise.resolve(ENROLLMENT))}
562
+ onVerify={mock(() => Promise.resolve())}
563
+ {...overrides}
564
+ />
565
+ );
566
+ return {onDisable, onRegenerateBackupCodes, utils};
567
+ };
568
+
569
+ it("shows On with new-codes and turn-off actions instead of Turn on", () => {
570
+ const {utils} = renderOnCard();
571
+ expect(utils.getByTestId("two-factor-settings-card-status").props.children).toBe("On");
572
+ expect(utils.queryByTestId("two-factor-settings-card-enable")).toBeNull();
573
+ expect(utils.getByTestId("two-factor-settings-card-regenerate")).toBeTruthy();
574
+ expect(utils.getByTestId("two-factor-settings-card-disable")).toBeTruthy();
575
+ });
576
+
577
+ it("regenerates backup codes after the password and shows the new ones", async () => {
578
+ const {onRegenerateBackupCodes, utils} = renderOnCard();
579
+ const {getByTestId, queryByTestId} = utils;
580
+ await press(getByTestId("two-factor-settings-card-regenerate"));
581
+ await typeText(getByTestId("two-factor-settings-card-password"), "hunter22");
582
+ await press(getByTestId("two-factor-settings-card-password-submit"));
583
+ expect(onRegenerateBackupCodes).toHaveBeenCalledWith("hunter22");
584
+ await waitFor(() => {
585
+ expect(getByTestId("two-factor-settings-card-backup-code-1").props.children).toBe(
586
+ NEW_CODES[0]
587
+ );
588
+ });
589
+ expect(getByTestId("two-factor-settings-card-backup-code-2").props.children).toBe(
590
+ NEW_CODES[1]
591
+ );
592
+ expect(queryByTestId("two-factor-settings-card-backup-code-3")).toBeNull();
593
+ expect(isDisabled(getByTestId("two-factor-settings-card-finish"))).toBe(true);
594
+ await press(getByTestId("two-factor-settings-card-saved"));
595
+ await press(getByTestId("two-factor-settings-card-finish"));
596
+ expect(queryByTestId("two-factor-settings-card-backup-code-1")).toBeNull();
597
+ expect(getByTestId("two-factor-settings-card-regenerate")).toBeTruthy();
598
+ });
599
+
600
+ it("shows a rejected regenerate on the password step", async () => {
601
+ const {utils} = renderOnCard({
602
+ onRegenerateBackupCodes: mock(() => Promise.reject({message: "Invalid password"})),
603
+ });
604
+ await press(utils.getByTestId("two-factor-settings-card-regenerate"));
605
+ await typeText(utils.getByTestId("two-factor-settings-card-password"), "wrong");
606
+ await press(utils.getByTestId("two-factor-settings-card-password-submit"));
607
+ await waitFor(() => {
608
+ expect(utils.getByText("Invalid password")).toBeTruthy();
609
+ });
610
+ expect(utils.getByTestId("two-factor-settings-card-password")).toBeTruthy();
611
+ });
612
+
613
+ const confirmTurnOff = async (utils: ReturnType<typeof renderWithTheme>): Promise<void> => {
614
+ await press(utils.getByTestId("two-factor-settings-card-disable"));
615
+ await typeText(utils.getByTestId("two-factor-settings-card-password"), "hunter22");
616
+ await press(utils.getByTestId("two-factor-settings-card-password-submit"));
617
+ };
618
+
619
+ it("turns off only after the password and a destructive confirm", async () => {
620
+ const {onDisable, utils} = renderOnCard();
621
+ await confirmTurnOff(utils);
622
+ expect(utils.getByTestId("two-factor-settings-card-confirm")).toBeTruthy();
623
+ expect(onDisable).not.toHaveBeenCalled();
624
+
625
+ await press(utils.getByTestId("two-factor-settings-card-confirm-submit"));
626
+ expect(onDisable).toHaveBeenCalledWith("hunter22");
627
+ await waitFor(() => {
628
+ expect(utils.queryByTestId("two-factor-settings-card-password")).toBeNull();
629
+ });
630
+ });
631
+
632
+ it("keeps two-factor on when the turn-off confirm is cancelled", async () => {
633
+ const {onDisable, utils} = renderOnCard();
634
+ await confirmTurnOff(utils);
635
+ await press(utils.getByTestId("two-factor-settings-card-confirm-cancel"));
636
+ expect(utils.queryByTestId("two-factor-settings-card-confirm")).toBeNull();
637
+ expect(onDisable).not.toHaveBeenCalled();
638
+ expect(utils.getByTestId("two-factor-settings-card-password")).toBeTruthy();
639
+
640
+ await press(utils.getByTestId("two-factor-settings-card-cancel"));
641
+ expect(utils.getByTestId("two-factor-settings-card-status").props.children).toBe("On");
642
+ });
643
+
644
+ it("shows a rejected turn-off and keeps the password step", async () => {
645
+ const {utils} = renderOnCard({
646
+ onDisable: mock(() => Promise.reject(new Error("Invalid password"))),
647
+ });
648
+ await press(utils.getByTestId("two-factor-settings-card-disable"));
649
+ await typeText(utils.getByTestId("two-factor-settings-card-password"), "wrong");
650
+ await press(utils.getByTestId("two-factor-settings-card-password-submit"));
651
+ await press(utils.getByTestId("two-factor-settings-card-confirm-submit"));
652
+ await waitFor(() => {
653
+ expect(utils.getByText("Invalid password")).toBeTruthy();
654
+ });
655
+ expect(utils.getByTestId("two-factor-settings-card-password")).toBeTruthy();
656
+ });
657
+
658
+ it("goes straight to the confirm when requirePassword is false", async () => {
659
+ const {onDisable, utils} = renderOnCard({requirePassword: false});
660
+ await press(utils.getByTestId("two-factor-settings-card-disable"));
661
+ expect(utils.queryByTestId("two-factor-settings-card-password")).toBeNull();
662
+ await press(utils.getByTestId("two-factor-settings-card-confirm-submit"));
663
+ expect(onDisable).toHaveBeenCalledWith(undefined);
664
+ });
665
+
666
+ it("shows how many backup codes are left, with a warning when fewer than 3", () => {
667
+ const {utils} = renderOnCard({backupCodesRemaining: 8});
668
+ expect(
669
+ utils.getByTestId("two-factor-settings-card-backup-codes-remaining").props.children
670
+ ).toBe("8 backup codes left");
671
+ expect(utils.queryByTestId("two-factor-settings-card-backup-codes-low")).toBeNull();
672
+
673
+ const low = renderOnCard({backupCodesRemaining: 1}).utils;
674
+ expect(
675
+ low.getByTestId("two-factor-settings-card-backup-codes-remaining").props.children
676
+ ).toBe("1 backup code left");
677
+ expect(low.getByTestId("two-factor-settings-card-backup-codes-low")).toBeTruthy();
678
+ });
679
+
680
+ it("disables new codes and turn off for a user without a password", () => {
681
+ const {utils} = renderOnCard({hasPassword: false});
682
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-regenerate"))).toBe(true);
683
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-disable"))).toBe(true);
684
+ expect(utils.getByTestId("two-factor-settings-card-needs-password")).toBeTruthy();
685
+ });
686
+
687
+ it("hides actions whose callbacks are not passed", () => {
688
+ const {utils} = renderOnCard({onDisable: undefined, onRegenerateBackupCodes: undefined});
689
+ expect(utils.queryByTestId("two-factor-settings-card-regenerate")).toBeNull();
690
+ expect(utils.queryByTestId("two-factor-settings-card-disable")).toBeNull();
691
+ });
692
+
693
+ it("shows the error prop as an alert and disables actions when disabled", () => {
694
+ const {utils} = renderOnCard({disabled: true, error: "Couldn't load your account."});
695
+ expect(utils.getByTestId("two-factor-settings-card-error").props.role).toBe("alert");
696
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-regenerate"))).toBe(true);
697
+ expect(isDisabled(utils.getByTestId("two-factor-settings-card-disable"))).toBe(true);
698
+ });
699
+ });
700
+ });