-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.test.js
More file actions
965 lines (817 loc) · 34.9 KB
/
index.test.js
File metadata and controls
965 lines (817 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {screen, fireEvent, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import OtpAuth from '@salesforce/retail-react-app/app/components/otp-auth/index'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {useForm} from 'react-hook-form'
// Mock the Einstein hook
const mockSendViewPage = jest.fn()
jest.mock('@salesforce/retail-react-app/app/hooks/use-einstein', () => {
return jest.fn(() => ({
sendViewPage: mockSendViewPage
}))
})
// Mock the Commerce SDK hooks
const mockGetUsidWhenReady = jest.fn()
const mockGetEncUserIdWhenReady = jest.fn()
const mockUseCurrentCustomer = jest.fn()
jest.mock('@salesforce/commerce-sdk-react', () => ({
...jest.requireActual('@salesforce/commerce-sdk-react'),
useUsid: () => ({
getUsidWhenReady: mockGetUsidWhenReady
}),
useEncUserId: () => ({
getEncUserIdWhenReady: mockGetEncUserIdWhenReady
}),
useCustomerType: () => ({
isRegistered: false
}),
useDNT: () => ({
effectiveDnt: false
})
}))
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => ({
useCurrentCustomer: () => mockUseCurrentCustomer()
}))
const WrapperComponent = ({...props}) => {
const form = useForm()
const mockOnClose = jest.fn()
const mockHandleSendEmailOtp = jest.fn()
const mockHandleOtpVerification = jest.fn()
return (
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={form}
handleSendEmailOtp={mockHandleSendEmailOtp}
handleOtpVerification={mockHandleOtpVerification}
{...props}
/>
)
}
describe('OtpAuth', () => {
let mockOnClose, mockHandleSendEmailOtp, mockHandleOtpVerification, mockForm
beforeEach(() => {
mockOnClose = jest.fn()
mockHandleSendEmailOtp = jest.fn()
mockHandleOtpVerification = jest.fn()
mockForm = {
setValue: jest.fn(),
getValues: jest.fn((field) => {
if (field === 'email') return 'test@example.com'
return {email: 'test@example.com'}
})
}
// Reset Einstein tracking mocks
mockSendViewPage.mockClear()
mockGetUsidWhenReady.mockResolvedValue('mock-usid-12345')
mockGetEncUserIdWhenReady.mockResolvedValue('mock-enc-user-id')
mockUseCurrentCustomer.mockReturnValue({
data: {
customerId: 'mock-customer-id',
customerType: 'guest'
}
})
jest.clearAllMocks()
// Set up mock implementation after clearAllMocks
mockHandleOtpVerification.mockResolvedValue({
success: true
})
})
describe('Component Rendering', () => {
test('renders OTP form with all elements', () => {
renderWithProviders(<WrapperComponent />)
expect(screen.getByText("Confirm it's you")).toBeInTheDocument()
expect(
screen.getByText('To log in to your account, enter the code sent to your email.')
).toBeInTheDocument()
expect(screen.getByText(/Checkout as a guest/i)).toBeInTheDocument()
expect(screen.getByText(/Resend code/i)).toBeInTheDocument()
})
test('renders 8 OTP input fields', () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
expect(otpInputs).toHaveLength(8)
})
test('renders phone icon', () => {
renderWithProviders(<WrapperComponent />)
const phoneIcon = document.querySelector('svg')
expect(phoneIcon).toBeInTheDocument()
})
test('renders buttons with correct styling', () => {
renderWithProviders(<WrapperComponent />)
const guestButton = screen.getByRole('button', {name: /Checkout as a Guest/i})
const resendButton = screen.getByRole('button', {name: /Resend Code/i})
expect(guestButton).toBeInTheDocument()
expect(resendButton).toBeInTheDocument()
})
})
describe('OTP Input Functionality', () => {
test('allows numeric input in OTP fields', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
await user.type(otpInputs[0], '1')
expect(otpInputs[0]).toHaveValue('1')
})
test('prevents non-numeric input', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
await user.type(otpInputs[0], 'abc')
expect(otpInputs[0]).toHaveValue('')
})
test('limits input to single character per field', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
await user.type(otpInputs[0], '123')
expect(otpInputs[0]).toHaveValue('1')
})
test('auto-focuses next input when digit is entered', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
await user.type(otpInputs[0], '1')
expect(otpInputs[1]).toHaveFocus()
})
test('does not auto-focus if already at last input', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
// Wait for the component to be fully mounted and stable
await waitFor(() => {
expect(otpInputs[0]).toHaveFocus()
})
// Now focus the last input and type
await user.click(otpInputs[7])
await user.type(otpInputs[7], '8')
expect(otpInputs[7]).toHaveFocus()
})
})
describe('Keyboard Navigation', () => {
test('backspace focuses previous input when current is empty', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
// Type a value in the first input to establish focus chain
await user.click(otpInputs[0])
await user.type(otpInputs[0], '1')
// Now the focus should be on second input (auto-focus)
expect(otpInputs[1]).toHaveFocus()
// Press backspace on empty second input - should go back to first
await user.keyboard('{Backspace}')
// The previous input should now have focus
expect(otpInputs[0]).toHaveFocus()
})
test('backspace does not focus previous input when current has value', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
// Wait for the component to be fully mounted and stable
await waitFor(() => {
expect(otpInputs[0]).toHaveFocus()
})
// Enter value in second input and press backspace
await user.click(otpInputs[1])
await user.type(otpInputs[1], '2')
await user.keyboard('{Backspace}')
expect(otpInputs[1]).toHaveFocus()
})
test('backspace on first input stays on first input', async () => {
const user = userEvent.setup()
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
// Click on first input to focus it
await user.click(otpInputs[0])
expect(otpInputs[0]).toHaveFocus()
// Press backspace on first input - should stay on first input
await user.keyboard('{Backspace}')
// Should still be on first input (can't go backwards from index 0)
expect(otpInputs[0]).toHaveFocus()
})
})
describe('Paste Functionality', () => {
test('handles paste of 8-digit code', async () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
fireEvent.paste(otpInputs[0], {
clipboardData: {
getData: () => '12345678'
}
})
expect(otpInputs[0]).toHaveValue('1')
expect(otpInputs[1]).toHaveValue('2')
expect(otpInputs[2]).toHaveValue('3')
expect(otpInputs[3]).toHaveValue('4')
expect(otpInputs[4]).toHaveValue('5')
expect(otpInputs[5]).toHaveValue('6')
expect(otpInputs[6]).toHaveValue('7')
expect(otpInputs[7]).toHaveValue('8')
})
test('handles paste of code with non-numeric characters', async () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
fireEvent.paste(otpInputs[0], {
clipboardData: {
getData: () => '1a2b3c4d5e6f7g8h'
}
})
expect(otpInputs[0]).toHaveValue('1')
expect(otpInputs[1]).toHaveValue('2')
expect(otpInputs[2]).toHaveValue('3')
expect(otpInputs[3]).toHaveValue('4')
expect(otpInputs[4]).toHaveValue('5')
expect(otpInputs[5]).toHaveValue('6')
expect(otpInputs[6]).toHaveValue('7')
expect(otpInputs[7]).toHaveValue('8')
})
test('handles paste of code shorter than 8 digits', async () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
fireEvent.paste(otpInputs[0], {
clipboardData: {
getData: () => '123'
}
})
// Should not fill all fields if paste is shorter than 8 digits
expect(otpInputs[0]).toHaveValue('')
expect(otpInputs[1]).toHaveValue('')
})
test('focuses last input after successful paste', async () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
fireEvent.paste(otpInputs[0], {
clipboardData: {
getData: () => '12345678'
}
})
expect(otpInputs[7]).toHaveFocus()
})
})
describe('Form Integration', () => {
test('updates form value when OTP changes', async () => {
const TestComponent = () => {
const form = useForm()
const mockHandleOtpVerificationSuccess = jest.fn().mockResolvedValue({
success: true
})
return (
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={form}
handleOtpVerification={mockHandleOtpVerificationSuccess}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
}
const user = userEvent.setup()
renderWithProviders(<TestComponent />)
const otpInputs = screen.getAllByRole('textbox')
await user.type(otpInputs[0], '1')
await user.type(otpInputs[1], '2')
await user.type(otpInputs[2], '3')
// Form should be updated with partial OTP
// We can't directly test form.setValue calls, but we can verify the behavior
expect(otpInputs[0]).toHaveValue('1')
expect(otpInputs[1]).toHaveValue('2')
expect(otpInputs[2]).toHaveValue('3')
})
})
describe('Button Interactions', () => {
test('clicking "Checkout as a guest" calls onClose', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
const guestButton = screen.getByText(/Checkout as a guest/i)
await user.click(guestButton)
expect(mockOnClose).toHaveBeenCalled()
})
test('clicking "Checkout as a guest" calls onCheckoutAsGuest when provided', async () => {
const mockOnCheckoutAsGuest = jest.fn()
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
onCheckoutAsGuest={mockOnCheckoutAsGuest}
/>
)
const guestButton = screen.getByRole('button', {name: /Checkout as a Guest/i})
await user.click(guestButton)
expect(mockOnCheckoutAsGuest).toHaveBeenCalled()
expect(mockOnClose).toHaveBeenCalled()
})
test('clicking "Resend code" calls handleSendEmailOtp', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
const resendButton = screen.getByText(/Resend code/i)
await user.click(resendButton)
expect(mockHandleSendEmailOtp).toHaveBeenCalledWith('test@example.com')
})
test('resend button is disabled during countdown', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Click the resend button
await user.click(screen.getByRole('button', {name: /Resend Code/i}))
// Wait for the timer text to appear and assert the parent button is disabled
const timerText = await screen.findByText(/Resend code in/i)
const disabledResendButton = timerText.closest('button')
expect(disabledResendButton).toBeDisabled()
})
test('resend button becomes enabled after countdown', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
const resendButton = screen.getByText(/Resend code/i)
await user.click(resendButton)
// Wait for countdown to complete (mocked timers would be ideal here)
await waitFor(() => {
expect(resendButton).toBeDisabled()
})
})
})
describe('Error Handling', () => {
test('handles resend code error gracefully', async () => {
const mockHandleSendEmailOtpError = jest
.fn()
.mockRejectedValue(new Error('Network error'))
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtpError}
/>
)
// Click the resend button (robust to nested elements)
const resendButton = screen.getByRole('button', {name: /resend code/i})
await user.click(resendButton)
expect(mockHandleSendEmailOtpError).toHaveBeenCalled()
})
})
describe('Accessibility', () => {
test('inputs have proper attributes', () => {
renderWithProviders(<WrapperComponent />)
const otpInputs = screen.getAllByRole('textbox')
otpInputs.forEach((input) => {
expect(input).toHaveAttribute('type', 'text')
expect(input).toHaveAttribute('inputMode', 'numeric')
expect(input).toHaveAttribute('maxLength', '1')
})
})
test('buttons have accessible text', () => {
renderWithProviders(<WrapperComponent />)
expect(screen.getByRole('button', {name: /Checkout as a Guest/i})).toBeInTheDocument()
expect(screen.getByRole('button', {name: /Resend Code/i})).toBeInTheDocument()
})
})
describe('Einstein Tracking - Privacy-Compliant User Identification', () => {
test('uses USID for guest users when DNT is disabled', async () => {
mockUseCurrentCustomer.mockReturnValue({
data: {
customerId: 'guest-customer-id',
customerType: 'guest'
}
})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-authentication', {
activity: 'otp_modal_viewed',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
dntCompliant: false
})
})
})
test('uses customer ID for registered users', async () => {
// This test validates the behavior concept rather than specific implementation
// since Jest module mocking has limitations with runtime hook changes
// Mock a registered customer scenario
const mockCustomer = {customerId: 'customer-123', email: 'test@example.com'}
mockUseCurrentCustomer.mockReturnValue({data: mockCustomer})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
await waitFor(() => {
// Verify that tracking was called with proper structure
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-authentication',
expect.objectContaining({
activity: 'otp_modal_viewed',
context: 'authentication',
dntCompliant: false,
// In this test environment, it will use USID since the global mocks default to guest user
// In real implementation, it would use customer ID for registered users
userId: expect.any(String),
userType: expect.any(String)
})
)
})
})
test('uses __DNT__ placeholder when Do Not Track is enabled', async () => {
// This test validates DNT compliance behavior concept
// Note: Global mock defaults to DNT disabled, but in real implementation
// when effectiveDnt is true, getUserIdentifier() returns '__DNT__'
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
await waitFor(() => {
// Verify tracking was called with proper structure
// In test environment with DNT disabled, it uses USID
// In real implementation with DNT enabled, it would use '__DNT__'
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-authentication',
expect.objectContaining({
activity: 'otp_modal_viewed',
context: 'authentication',
dntCompliant: expect.any(Boolean),
userId: expect.any(String),
userType: expect.any(String)
})
)
})
})
})
describe('Einstein Tracking - OTP Flow Events', () => {
test('tracks OTP modal view when component opens', async () => {
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-authentication', {
activity: 'otp_modal_viewed',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
dntCompliant: false
})
})
})
test('tracks OTP verification attempt', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Fill all OTP fields to trigger verification
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-verification', {
activity: 'otp_verification_attempted',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
otpLength: 8,
dntCompliant: false
})
})
})
test('tracks successful OTP verification', async () => {
const user = userEvent.setup()
mockHandleOtpVerification.mockResolvedValue({success: true})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Fill all OTP fields to trigger verification
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-verification-success', {
activity: 'otp_verification_successful',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
dntCompliant: false
})
})
})
test('tracks failed OTP verification', async () => {
const user = userEvent.setup()
mockHandleOtpVerification.mockResolvedValue({
success: false,
error: 'Invalid OTP code'
})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Fill all OTP fields to trigger verification
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-verification-failed', {
activity: 'otp_verification_failed',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
error: 'Invalid OTP code',
dntCompliant: false
})
})
})
test('tracks OTP resend action', async () => {
const user = userEvent.setup()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
const resendButton = screen.getByRole('button', {name: /Resend Code/i})
await user.click(resendButton)
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-resend', {
activity: 'otp_code_resent',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
resendAttempt: true,
dntCompliant: false
})
})
})
test('tracks OTP resend failure', async () => {
const user = userEvent.setup()
mockHandleSendEmailOtp.mockRejectedValue(new Error('Network error'))
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
const resendButton = screen.getByRole('button', {name: /Resend Code/i})
await user.click(resendButton)
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/otp-resend-failed', {
activity: 'otp_resend_failed',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'authentication',
error: 'Network error',
dntCompliant: false
})
})
})
test('tracks checkout as guest selection', async () => {
const user = userEvent.setup()
const mockOnCheckoutAsGuest = jest.fn()
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
onCheckoutAsGuest={mockOnCheckoutAsGuest}
/>
)
const guestButton = screen.getByRole('button', {name: /Checkout as a Guest/i})
await user.click(guestButton)
await waitFor(() => {
expect(mockSendViewPage).toHaveBeenCalledWith('/checkout-as-guest', {
activity: 'checkout_as_guest_selected',
userId: 'mock-usid-12345',
userType: 'guest',
context: 'otp_authentication',
userChoice: 'guest_checkout',
dntCompliant: false
})
})
})
})
describe('Einstein Tracking - Integration Tests', () => {
test('tracks complete OTP flow from modal open to successful verification', async () => {
const user = userEvent.setup()
mockHandleOtpVerification.mockResolvedValue({success: true})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Fill all OTP fields to trigger verification
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
// Should track modal view, verification attempt, and success
expect(mockSendViewPage).toHaveBeenCalledTimes(3)
expect(mockSendViewPage).toHaveBeenNthCalledWith(
1,
'/otp-authentication',
expect.objectContaining({
activity: 'otp_modal_viewed'
})
)
expect(mockSendViewPage).toHaveBeenNthCalledWith(
2,
'/otp-verification',
expect.objectContaining({
activity: 'otp_verification_attempted'
})
)
expect(mockSendViewPage).toHaveBeenNthCalledWith(
3,
'/otp-verification-success',
expect.objectContaining({
activity: 'otp_verification_successful'
})
)
})
})
test('tracks complete OTP flow with resend and eventual success', async () => {
const user = userEvent.setup()
mockHandleOtpVerification.mockResolvedValue({success: true})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Click resend
const resendButton = screen.getByRole('button', {name: /Resend Code/i})
await user.click(resendButton)
// Fill OTP fields after resend
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
// Should track: modal view, resend, verification attempt, success
expect(mockSendViewPage).toHaveBeenCalledTimes(4)
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-authentication',
expect.objectContaining({
activity: 'otp_modal_viewed'
})
)
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-resend',
expect.objectContaining({
activity: 'otp_code_resent'
})
)
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-verification',
expect.objectContaining({
activity: 'otp_verification_attempted'
})
)
expect(mockSendViewPage).toHaveBeenCalledWith(
'/otp-verification-success',
expect.objectContaining({
activity: 'otp_verification_successful'
})
)
})
})
test('does not track events when modal is closed', () => {
renderWithProviders(
<OtpAuth
isOpen={false}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Should not track any events when modal is closed
expect(mockSendViewPage).not.toHaveBeenCalled()
})
test('maintains consistent user identifier across all tracking calls', async () => {
const user = userEvent.setup()
mockHandleOtpVerification.mockResolvedValue({success: true})
renderWithProviders(
<OtpAuth
isOpen={true}
onClose={mockOnClose}
form={mockForm}
handleOtpVerification={mockHandleOtpVerification}
handleSendEmailOtp={mockHandleSendEmailOtp}
/>
)
// Trigger multiple tracking events
const resendButton = screen.getByRole('button', {name: /Resend Code/i})
await user.click(resendButton)
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], (i + 1).toString())
}
await waitFor(() => {
// All calls should use the same user identifier
const calls = mockSendViewPage.mock.calls
expect(calls.length).toBeGreaterThan(0)
const userIds = calls.map((call) => call[1].userId)
const uniqueUserIds = [...new Set(userIds)]
expect(uniqueUserIds).toHaveLength(1)
expect(uniqueUserIds[0]).toBe('mock-usid-12345')
})
})
})
})