-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathprofile.test.js
More file actions
119 lines (100 loc) · 3.86 KB
/
profile.test.js
File metadata and controls
119 lines (100 loc) · 3.86 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
/*
* Copyright (c) 2023, 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 {screen, waitFor, within} from '@testing-library/react'
import {
createPathWithDefaults,
renderWithProviders
} from '@salesforce/retail-react-app/app/utils/test-utils'
import {rest} from 'msw'
import AccountDetail from '@salesforce/retail-react-app/app/pages/account/profile'
import {
mockedRegisteredCustomerWithNoNumber,
mockedRegisteredCustomer
} from '@salesforce/retail-react-app/app/mocks/mock-data'
import {Route, Switch} from 'react-router-dom'
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
import * as sdk from '@salesforce/commerce-sdk-react'
let mockCustomer = {}
const MockedComponent = () => {
return (
<Switch>
<Route path={createPathWithDefaults('/account')}>
<AccountDetail />
</Route>
</Switch>
)
}
jest.mock('@salesforce/commerce-sdk-react', () => ({
...jest.requireActual('@salesforce/commerce-sdk-react'),
useCustomerType: jest.fn()
}))
// Set up and clean up
beforeEach(() => {
jest.resetModules()
global.server.use(
rest.get('*/customers/:customerId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
),
rest.patch('*/customers/:customerId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(req.body))
})
)
window.history.pushState({}, 'Account', createPathWithDefaults('/account/addresses'))
})
afterEach(() => {
jest.resetModules()
localStorage.clear()
})
test('Allows customer to edit phone number', async () => {
sdk.useCustomerType.mockReturnValue({isRegistered: true, isExternal: false})
global.server.use(
rest.get('*/customers/:customerId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomerWithNoNumber))
)
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
await waitFor(() => {
expect(screen.getByText(/Account Details/i)).toBeInTheDocument()
})
const profileCard = screen.getByTestId('sf-toggle-card-my-profile')
// Change phone number
await user.click(within(profileCard).getByText(/edit/i))
// Profile Form must be present
expect(screen.getByLabelText('Profile Form')).toBeInTheDocument()
await user.type(screen.getByLabelText('Phone Number'), '7275551234')
global.server.use(
rest.get('*/customers/:customerId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
)
)
await user.click(screen.getByText(/^Save$/i))
await waitFor(() => {
expect(screen.getByText(/Profile updated/i)).toBeInTheDocument()
expect(screen.getByText(/555-1234/i)).toBeInTheDocument()
})
})
test('Non ECOM user cannot see the password card', async () => {
sdk.useCustomerType.mockReturnValue({isRegistered: true, isExternal: true})
global.server.use(
rest.get('*/customers/:customerId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomerWithNoNumber))
)
)
renderWithProviders(<MockedComponent />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
})
await waitFor(() => {
expect(screen.getByText(/Account Details/i)).toBeInTheDocument()
})
await screen.getByTestId('sf-toggle-card-my-profile')
// Edit functionality should NOT be available
expect(screen.queryByText(/edit/i)).not.toBeInTheDocument()
expect(screen.queryByText(/Password/i)).not.toBeInTheDocument()
})