-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathlogin-state.test.js
More file actions
76 lines (68 loc) · 3.47 KB
/
login-state.test.js
File metadata and controls
76 lines (68 loc) · 3.47 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
/*
* Copyright (c) 2025, Salesforce, 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 LoginState from '@salesforce/retail-react-app/app/pages/checkout/partials/login-state'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {useForm} from 'react-hook-form'
const mockTogglePasswordField = jest.fn()
const idps = ['apple', 'google']
const WrapperComponent = ({...props}) => {
const form = useForm()
return <LoginState form={form} togglePasswordField={mockTogglePasswordField} {...props} />
}
describe('LoginState', () => {
test('shows login button when showPasswordField is false', async () => {
const {getByRole, user} = renderWithProviders(<WrapperComponent />)
const trigger = getByRole('button', {name: /Already have an account\? Log in/i})
await user.click(trigger)
expect(mockTogglePasswordField).toHaveBeenCalled()
})
test('shows checkout as guest button when showPasswordField is true', async () => {
const {getByRole, user} = renderWithProviders(<WrapperComponent showPasswordField={true} />)
const trigger = getByRole('button', {name: /Checkout as Guest/i})
await user.click(trigger)
expect(mockTogglePasswordField).toHaveBeenCalled()
})
test('shows passwordless login button if enabled', async () => {
const {getByRole, getByText, user} = renderWithProviders(
<WrapperComponent isPasswordlessEnabled={true} />
)
expect(getByText('Or Login With')).toBeInTheDocument()
expect(getByRole('button', {name: 'Secure Link'})).toBeInTheDocument()
const trigger = getByRole('button', {name: 'Password'})
await user.click(trigger)
expect(mockTogglePasswordField).toHaveBeenCalled()
expect(getByRole('button', {name: 'Back to Sign In Options'})).toBeInTheDocument()
})
test('does not show passwordless login button if disabled', () => {
const {queryByRole, queryByText} = renderWithProviders(
<WrapperComponent isPasswordlessEnabled={false} />
)
expect(queryByText('Or Login With')).not.toBeInTheDocument()
expect(queryByRole('button', {name: 'Secure Link'})).not.toBeInTheDocument()
})
test('shows social login buttons if enabled', async () => {
const {getByRole, getByText, user} = renderWithProviders(
<WrapperComponent isSocialEnabled={true} idps={idps} />
)
expect(getByText('Or Login With')).toBeInTheDocument()
expect(getByRole('button', {name: /Google/i})).toBeInTheDocument()
expect(getByRole('button', {name: /Apple/i})).toBeInTheDocument()
const trigger = getByRole('button', {name: 'Password'})
await user.click(trigger)
expect(mockTogglePasswordField).toHaveBeenCalled()
expect(getByRole('button', {name: 'Back to Sign In Options'})).toBeInTheDocument()
})
test('does not show social login buttons if disabled', () => {
const {queryByRole, queryByText} = renderWithProviders(
<WrapperComponent isSocialEnabled={false} idps={idps} />
)
expect(queryByText('Or Login With')).not.toBeInTheDocument()
expect(queryByRole('button', {name: /Google/i})).not.toBeInTheDocument()
expect(queryByRole('button', {name: /Apple/i})).not.toBeInTheDocument()
})
})