Skip to content

Commit 455f129

Browse files
wip
1 parent 3f335c5 commit 455f129

File tree

10 files changed

+124
-48
lines changed

10 files changed

+124
-48
lines changed

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressCard.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('AddressCard', () => {
224224
{ wrapper }
225225
)
226226

227-
expect(screen.getByText('Cardholder name')).toBeInTheDocument()
227+
expect(screen.getByText('Full name')).toBeInTheDocument()
228228
expect(screen.getByText('N/A')).toBeInTheDocument()
229229
expect(screen.getByText('Billing address')).toBeInTheDocument()
230230
expect(screen.queryByText(/null/)).not.toBeInTheDocument()
@@ -241,7 +241,7 @@ describe('AddressCard', () => {
241241
{ wrapper }
242242
)
243243

244-
expect(screen.getByText(/Cardholder name/)).toBeInTheDocument()
244+
expect(screen.getByText(/Full name/)).toBeInTheDocument()
245245
expect(screen.getByText(/Bob Smith/)).toBeInTheDocument()
246246
})
247247
})

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function AddressCard({
4040
{!isFormOpen && (
4141
<>
4242
<div className="flex justify-between">
43-
<h4 className="font-semibold">Cardholder name</h4>
43+
<h4 className="font-semibold">Full name</h4>
4444
<A
4545
variant="semibold"
4646
onClick={() => setIsFormOpen(true)}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { z } from 'zod'
2+
3+
import bankLogo from 'assets/billing/bank.svg'
4+
import { SubscriptionDetailSchema } from 'services/account'
5+
6+
interface BankInformationProps {
7+
subscriptionDetail: z.infer<typeof SubscriptionDetailSchema>
8+
}
9+
function BankInformation({ subscriptionDetail }: BankInformationProps) {
10+
return (
11+
<div className="flex flex-col gap-2">
12+
<div className="flex gap-1">
13+
<img src={bankLogo} alt="bank logo" />
14+
<div className="ml-1 flex flex-col self-center">
15+
<b>
16+
{subscriptionDetail?.defaultPaymentMethod?.usBankAccount?.bankName}
17+
&nbsp;••••&nbsp;
18+
{subscriptionDetail?.defaultPaymentMethod?.usBankAccount?.last4}
19+
</b>
20+
</div>
21+
</div>
22+
</div>
23+
)
24+
}
25+
26+
export default BankInformation

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentCard/PaymentCard.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import A from 'ui/A'
66
import Button from 'ui/Button'
77
import Icon from 'ui/Icon'
88

9+
import BankInformation from './BankInformation'
910
import CardInformation from './CardInformation'
1011
import PaymentMethodForm from './PaymentMethodForm'
1112
function PaymentCard({ subscriptionDetail, provider, owner }) {
1213
const [isFormOpen, setIsFormOpen] = useState(false)
1314
const card = subscriptionDetail?.defaultPaymentMethod?.card
15+
const usBankAccount = subscriptionDetail?.defaultPaymentMethod?.usBankAccount
1416

1517
return (
1618
<div className="flex flex-col gap-2 border-t p-4">
@@ -20,7 +22,7 @@ function PaymentCard({ subscriptionDetail, provider, owner }) {
2022
<A
2123
variant="semibold"
2224
onClick={() => setIsFormOpen(true)}
23-
hook="edit-card"
25+
hook="edit-payment-method"
2426
>
2527
Edit <Icon name="chevronRight" size="sm" variant="solid" />
2628
</A>
@@ -31,14 +33,17 @@ function PaymentCard({ subscriptionDetail, provider, owner }) {
3133
provider={provider}
3234
owner={owner}
3335
closeForm={() => setIsFormOpen(false)}
36+
subscriptionDetail={subscriptionDetail}
3437
/>
3538
) : card ? (
3639
<CardInformation card={card} subscriptionDetail={subscriptionDetail} />
40+
) : usBankAccount ? (
41+
<BankInformation subscriptionDetail={subscriptionDetail} />
3742
) : (
3843
<div className="flex flex-col gap-4 text-ds-gray-quinary">
3944
<p className="mt-4">
40-
No credit card set. Please contact support if you think it’s an
41-
error or set it yourself.
45+
No payment method set. Please contact support if you think it&apos;s
46+
an error or set it yourself.
4247
</p>
4348
<div className="flex self-start">
4449
<Button

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentCard/PaymentCard.test.jsx

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
12
import { render, screen } from '@testing-library/react'
23
import userEvent from '@testing-library/user-event'
34

@@ -6,18 +7,26 @@ import { Plans } from 'shared/utils/billing'
67

78
import PaymentCard from './PaymentCard'
89

10+
const queryClient = new QueryClient()
11+
912
const mocks = vi.hoisted(() => ({
10-
useUpdateCard: vi.fn(),
13+
useUpdatePaymentMethod: vi.fn(),
14+
useCreateStripeSetupIntent: vi.fn(),
1115
}))
1216

1317
vi.mock('services/account', async () => {
1418
const actual = await vi.importActual('services/account')
1519
return {
1620
...actual,
17-
useUpdateCard: mocks.useUpdateCard,
21+
useUpdatePaymentMethod: mocks.useUpdatePaymentMethod,
22+
useCreateStripeSetupIntent: mocks.useCreateStripeSetupIntent,
1823
}
1924
})
2025

26+
afterEach(() => {
27+
vi.clearAllMocks()
28+
})
29+
2130
const subscriptionDetail = {
2231
defaultPaymentMethod: {
2332
card: {
@@ -35,7 +44,9 @@ const subscriptionDetail = {
3544
}
3645

3746
const wrapper = ({ children }) => (
38-
<ThemeContextProvider>{children}</ThemeContextProvider>
47+
<QueryClientProvider client={queryClient}>
48+
<ThemeContextProvider>{children}</ThemeContextProvider>
49+
</QueryClientProvider>
3950
)
4051

4152
// mocking all the stripe components; and trusting the library :)
@@ -48,9 +59,10 @@ vi.mock('@stripe/react-stripe-js', () => {
4859
return {
4960
useElements: () => ({
5061
getElement: vi.fn(),
62+
submit: vi.fn(),
5163
}),
5264
useStripe: () => ({}),
53-
CardElement: makeFakeComponent('CardElement'),
65+
PaymentElement: makeFakeComponent('PaymentElement'),
5466
}
5567
})
5668

@@ -64,20 +76,20 @@ describe('PaymentCard', () => {
6476
describe(`when the user doesn't have any subscriptionDetail`, () => {
6577
// NOTE: This test is misleading because we hide this component from a higher level in
6678
// BillingDetails.tsx if there is no subscriptionDetail
67-
it('renders the set card message', () => {
79+
it('renders the set payment method message', () => {
6880
render(
6981
<PaymentCard subscriptionDetail={null} provider="gh" owner="codecov" />
7082
)
7183

7284
expect(
7385
screen.getByText(
74-
/No credit card set. Please contact support if you think its an error or set it yourself./
86+
/No payment method set. Please contact support if you think it's an error or set it yourself./
7587
)
7688
).toBeInTheDocument()
7789
})
7890
})
7991

80-
describe(`when the user doesn't have any card`, () => {
92+
describe(`when the user doesn't have any payment method`, () => {
8193
it('renders an error message', () => {
8294
render(
8395
<PaymentCard
@@ -93,7 +105,7 @@ describe('PaymentCard', () => {
93105

94106
expect(
95107
screen.getByText(
96-
/No credit card set. Please contact support if you think its an error or set it yourself./
108+
/No payment method set. Please contact support if you think it's an error or set it yourself./
97109
)
98110
).toBeInTheDocument()
99111
})
@@ -113,7 +125,7 @@ describe('PaymentCard', () => {
113125
{ wrapper }
114126
)
115127

116-
mocks.useUpdateCard.mockReturnValue({
128+
mocks.useUpdatePaymentMethod.mockReturnValue({
117129
mutate: () => null,
118130
isLoading: false,
119131
})
@@ -136,14 +148,14 @@ describe('PaymentCard', () => {
136148
{ wrapper }
137149
)
138150

139-
mocks.useUpdateCard.mockReturnValue({
151+
mocks.useUpdatePaymentMethod.mockReturnValue({
140152
mutate: () => null,
141153
isLoading: false,
142154
})
143155
await user.click(screen.getByTestId('open-modal'))
144156

145157
expect(
146-
screen.getByRole('button', { name: /update/i })
158+
screen.getByRole('button', { name: /save/i })
147159
).toBeInTheDocument()
148160
})
149161
})
@@ -199,9 +211,9 @@ describe('PaymentCard', () => {
199211
describe('when the user clicks on Edit card', () => {
200212
it(`doesn't render the card anymore`, async () => {
201213
const { user } = setup()
202-
const updateCard = vi.fn()
203-
mocks.useUpdateCard.mockReturnValue({
204-
mutate: updateCard,
214+
const updatePaymentMethod = vi.fn()
215+
mocks.useUpdatePaymentMethod.mockReturnValue({
216+
mutate: updatePaymentMethod,
205217
isLoading: false,
206218
})
207219

@@ -213,16 +225,16 @@ describe('PaymentCard', () => {
213225
/>,
214226
{ wrapper }
215227
)
216-
await user.click(screen.getByTestId('edit-card'))
228+
await user.click(screen.getByTestId('edit-payment-method'))
217229

218230
expect(screen.queryByText(/Visa/)).not.toBeInTheDocument()
219231
})
220232

221233
it('renders the form', async () => {
222234
const { user } = setup()
223-
const updateCard = vi.fn()
224-
mocks.useUpdateCard.mockReturnValue({
225-
mutate: updateCard,
235+
const updatePaymentMethod = vi.fn()
236+
mocks.useUpdatePaymentMethod.mockReturnValue({
237+
mutate: updatePaymentMethod,
226238
isLoading: false,
227239
})
228240
render(
@@ -233,21 +245,23 @@ describe('PaymentCard', () => {
233245
/>,
234246
{ wrapper }
235247
)
236-
await user.click(screen.getByTestId('edit-card'))
248+
await user.click(screen.getByTestId('edit-payment-method'))
237249

238-
expect(
239-
screen.getByRole('button', { name: /update/i })
240-
).toBeInTheDocument()
250+
expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument()
241251
})
242252

243253
describe('when submitting', () => {
244254
it('calls the service to update the card', async () => {
245255
const { user } = setup()
246-
const updateCard = vi.fn()
247-
mocks.useUpdateCard.mockReturnValue({
248-
mutate: updateCard,
256+
const updatePaymentMethod = vi.fn()
257+
mocks.useUpdatePaymentMethod.mockReturnValue({
258+
mutate: updatePaymentMethod,
249259
isLoading: false,
250260
})
261+
mocks.useCreateStripeSetupIntent.mockReturnValue({
262+
data: { clientSecret: 'test-secret' },
263+
})
264+
251265
render(
252266
<PaymentCard
253267
subscriptionDetail={subscriptionDetail}
@@ -256,17 +270,17 @@ describe('PaymentCard', () => {
256270
/>,
257271
{ wrapper }
258272
)
259-
await user.click(screen.getByTestId('edit-card'))
260-
await user.click(screen.queryByRole('button', { name: /update/i }))
273+
await user.click(screen.getByTestId('edit-payment-method'))
274+
await user.click(screen.getByRole('button', { name: /save/i }))
261275

262-
expect(updateCard).toHaveBeenCalled()
276+
expect(updatePaymentMethod).toHaveBeenCalled()
263277
})
264278
})
265279

266280
describe('when the user clicks on cancel', () => {
267281
it(`doesn't render the form anymore`, async () => {
268282
const { user } = setup()
269-
mocks.useUpdateCard.mockReturnValue({
283+
mocks.useUpdatePaymentMethod.mockReturnValue({
270284
mutate: vi.fn(),
271285
isLoading: false,
272286
})
@@ -279,11 +293,11 @@ describe('PaymentCard', () => {
279293
{ wrapper }
280294
)
281295

282-
await user.click(screen.getByTestId('edit-card'))
296+
await user.click(screen.getByTestId('edit-payment-method'))
283297
await user.click(screen.getByRole('button', { name: /Cancel/ }))
284298

285299
expect(
286-
screen.queryByRole('button', { name: /save/i })
300+
screen.queryByTestId('update-payment-method')
287301
).not.toBeInTheDocument()
288302
})
289303
})
@@ -293,7 +307,7 @@ describe('PaymentCard', () => {
293307
it('renders the error', async () => {
294308
const { user } = setup()
295309
const randomError = 'not rich enough'
296-
mocks.useUpdateCard.mockReturnValue({
310+
mocks.useUpdatePaymentMethod.mockReturnValue({
297311
mutate: vi.fn(),
298312
error: { message: randomError },
299313
})
@@ -306,7 +320,7 @@ describe('PaymentCard', () => {
306320
{ wrapper }
307321
)
308322

309-
await user.click(screen.getByTestId('edit-card'))
323+
await user.click(screen.getByTestId('edit-payment-method'))
310324

311325
expect(screen.getByText(randomError)).toBeInTheDocument()
312326
})
@@ -315,7 +329,7 @@ describe('PaymentCard', () => {
315329
describe('when the form is loading', () => {
316330
it('has the error and save button disabled', async () => {
317331
const { user } = setup()
318-
mocks.useUpdateCard.mockReturnValue({
332+
mocks.useUpdatePaymentMethod.mockReturnValue({
319333
mutate: vi.fn(),
320334
isLoading: true,
321335
})
@@ -327,9 +341,9 @@ describe('PaymentCard', () => {
327341
/>,
328342
{ wrapper }
329343
)
330-
await user.click(screen.getByTestId('edit-card'))
344+
await user.click(screen.getByTestId('edit-payment-method'))
331345

332-
expect(screen.queryByRole('button', { name: /update/i })).toBeDisabled()
346+
expect(screen.getByTestId('update-payment-method')).toBeDisabled()
333347
expect(screen.queryByRole('button', { name: /cancel/i })).toBeDisabled()
334348
})
335349
})

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentCard/PaymentMethodForm.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ afterEach(() => {
7979
})
8080

8181
describe('PaymentMethodForm', () => {
82-
describe('when the user clicks on Edit payment method', () => {
83-
it(`doesn't render the payment method anymore`, async () => {
82+
describe('when the user opens the Payment Method Form', () => {
83+
it(`doesn't render the View payment method anymore`, async () => {
8484
const user = userEvent.setup()
8585
const updatePaymentMethod = vi.fn()
8686
mocks.useUpdatePaymentMethod.mockReturnValue({

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/PaymentCard/PaymentMethodForm.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { PaymentElement, useElements } from '@stripe/react-stripe-js'
2+
import { StripePaymentElement } from '@stripe/stripe-js'
23
import cs from 'classnames'
34
import { useState } from 'react'
45
import { z } from 'zod'
56

6-
import { SubscriptionDetailSchema } from 'services/account'
7+
import { stripeAddress, SubscriptionDetailSchema } from 'services/account'
78
import { useUpdatePaymentMethod } from 'services/account/useUpdatePaymentMethod'
89
import { Provider } from 'shared/api/helpers'
910
import Button from 'ui/Button'
@@ -38,6 +39,9 @@ const PaymentMethodForm = ({
3839
email:
3940
subscriptionDetail?.defaultPaymentMethod?.billingDetails?.email ||
4041
undefined,
42+
address:
43+
stripeAddress(subscriptionDetail?.defaultPaymentMethod?.billingDetails) ||
44+
undefined,
4145
})
4246

4347
async function submit(e: React.FormEvent) {
@@ -49,7 +53,9 @@ const PaymentMethodForm = ({
4953

5054
elements.submit()
5155

52-
const paymentElement = elements.getElement(PaymentElement)
56+
const paymentElement = elements.getElement(
57+
PaymentElement
58+
) as StripePaymentElement
5359

5460
updatePaymentMethod(paymentElement, {
5561
onSuccess: async () => {

0 commit comments

Comments
 (0)