Skip to content

Commit 91a732a

Browse files
wip
1 parent 3f335c5 commit 91a732a

File tree

11 files changed

+144
-58
lines changed

11 files changed

+144
-58
lines changed

src/pages/PlanPage/PlanPage.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function PlanPage() {
5454
stripe={stripePromise}
5555
options={{
5656
...StripeAppearance(isDarkMode),
57+
// mode and currency are required for the PaymentElement
5758
mode: 'setup',
5859
currency: 'usd',
5960
}}

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: 66 additions & 42 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,37 @@ 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

13-
vi.mock('services/account', async () => {
14-
const actual = await vi.importActual('services/account')
17+
vi.mock('services/account/useUpdatePaymentMethod', async () => {
18+
const actual = await vi.importActual(
19+
'services/account/useUpdatePaymentMethod'
20+
)
1521
return {
1622
...actual,
17-
useUpdateCard: mocks.useUpdateCard,
23+
useUpdatePaymentMethod: mocks.useUpdatePaymentMethod,
1824
}
1925
})
2026

27+
vi.mock('services/account/useCreateStripeSetupIntent', async () => {
28+
const actual = await vi.importActual(
29+
'services/account/useCreateStripeSetupIntent'
30+
)
31+
return {
32+
...actual,
33+
useCreateStripeSetupIntent: mocks.useCreateStripeSetupIntent,
34+
}
35+
})
36+
37+
afterEach(() => {
38+
vi.clearAllMocks()
39+
})
40+
2141
const subscriptionDetail = {
2242
defaultPaymentMethod: {
2343
card: {
@@ -35,7 +55,9 @@ const subscriptionDetail = {
3555
}
3656

3757
const wrapper = ({ children }) => (
38-
<ThemeContextProvider>{children}</ThemeContextProvider>
58+
<QueryClientProvider client={queryClient}>
59+
<ThemeContextProvider>{children}</ThemeContextProvider>
60+
</QueryClientProvider>
3961
)
4062

4163
// mocking all the stripe components; and trusting the library :)
@@ -48,9 +70,11 @@ vi.mock('@stripe/react-stripe-js', () => {
4870
return {
4971
useElements: () => ({
5072
getElement: vi.fn(),
73+
submit: vi.fn(),
5174
}),
5275
useStripe: () => ({}),
53-
CardElement: makeFakeComponent('CardElement'),
76+
PaymentElement: makeFakeComponent('PaymentElement'),
77+
Elements: makeFakeComponent('Elements'),
5478
}
5579
})
5680

@@ -64,20 +88,20 @@ describe('PaymentCard', () => {
6488
describe(`when the user doesn't have any subscriptionDetail`, () => {
6589
// NOTE: This test is misleading because we hide this component from a higher level in
6690
// BillingDetails.tsx if there is no subscriptionDetail
67-
it('renders the set card message', () => {
91+
it('renders the set payment method message', () => {
6892
render(
6993
<PaymentCard subscriptionDetail={null} provider="gh" owner="codecov" />
7094
)
7195

7296
expect(
7397
screen.getByText(
74-
/No credit card set. Please contact support if you think its an error or set it yourself./
98+
/No payment method set. Please contact support if you think it's an error or set it yourself./
7599
)
76100
).toBeInTheDocument()
77101
})
78102
})
79103

80-
describe(`when the user doesn't have any card`, () => {
104+
describe(`when the user doesn't have any payment method`, () => {
81105
it('renders an error message', () => {
82106
render(
83107
<PaymentCard
@@ -93,7 +117,7 @@ describe('PaymentCard', () => {
93117

94118
expect(
95119
screen.getByText(
96-
/No credit card set. Please contact support if you think its an error or set it yourself./
120+
/No payment method set. Please contact support if you think it's an error or set it yourself./
97121
)
98122
).toBeInTheDocument()
99123
})
@@ -113,7 +137,7 @@ describe('PaymentCard', () => {
113137
{ wrapper }
114138
)
115139

116-
mocks.useUpdateCard.mockReturnValue({
140+
mocks.useUpdatePaymentMethod.mockReturnValue({
117141
mutate: () => null,
118142
isLoading: false,
119143
})
@@ -136,15 +160,13 @@ describe('PaymentCard', () => {
136160
{ wrapper }
137161
)
138162

139-
mocks.useUpdateCard.mockReturnValue({
163+
mocks.useUpdatePaymentMethod.mockReturnValue({
140164
mutate: () => null,
141165
isLoading: false,
142166
})
143167
await user.click(screen.getByTestId('open-modal'))
144168

145-
expect(
146-
screen.getByRole('button', { name: /update/i })
147-
).toBeInTheDocument()
169+
expect(screen.getByTestId('save-payment-method')).toBeInTheDocument()
148170
})
149171
})
150172
})
@@ -199,9 +221,9 @@ describe('PaymentCard', () => {
199221
describe('when the user clicks on Edit card', () => {
200222
it(`doesn't render the card anymore`, async () => {
201223
const { user } = setup()
202-
const updateCard = vi.fn()
203-
mocks.useUpdateCard.mockReturnValue({
204-
mutate: updateCard,
224+
const updatePaymentMethod = vi.fn()
225+
mocks.useUpdatePaymentMethod.mockReturnValue({
226+
mutate: updatePaymentMethod,
205227
isLoading: false,
206228
})
207229

@@ -213,16 +235,16 @@ describe('PaymentCard', () => {
213235
/>,
214236
{ wrapper }
215237
)
216-
await user.click(screen.getByTestId('edit-card'))
238+
await user.click(screen.getByTestId('edit-payment-method'))
217239

218240
expect(screen.queryByText(/Visa/)).not.toBeInTheDocument()
219241
})
220242

221243
it('renders the form', async () => {
222244
const { user } = setup()
223-
const updateCard = vi.fn()
224-
mocks.useUpdateCard.mockReturnValue({
225-
mutate: updateCard,
245+
const updatePaymentMethod = vi.fn()
246+
mocks.useUpdatePaymentMethod.mockReturnValue({
247+
mutate: updatePaymentMethod,
226248
isLoading: false,
227249
})
228250
render(
@@ -233,21 +255,23 @@ describe('PaymentCard', () => {
233255
/>,
234256
{ wrapper }
235257
)
236-
await user.click(screen.getByTestId('edit-card'))
258+
await user.click(screen.getByTestId('edit-payment-method'))
237259

238-
expect(
239-
screen.getByRole('button', { name: /update/i })
240-
).toBeInTheDocument()
260+
expect(screen.getByTestId('save-payment-method')).toBeInTheDocument()
241261
})
242262

243263
describe('when submitting', () => {
244264
it('calls the service to update the card', async () => {
245265
const { user } = setup()
246-
const updateCard = vi.fn()
247-
mocks.useUpdateCard.mockReturnValue({
248-
mutate: updateCard,
266+
const updatePaymentMethod = vi.fn()
267+
mocks.useUpdatePaymentMethod.mockReturnValue({
268+
mutate: updatePaymentMethod,
249269
isLoading: false,
250270
})
271+
mocks.useCreateStripeSetupIntent.mockReturnValue({
272+
data: { clientSecret: 'test-secret' },
273+
})
274+
251275
render(
252276
<PaymentCard
253277
subscriptionDetail={subscriptionDetail}
@@ -256,17 +280,17 @@ describe('PaymentCard', () => {
256280
/>,
257281
{ wrapper }
258282
)
259-
await user.click(screen.getByTestId('edit-card'))
260-
await user.click(screen.queryByRole('button', { name: /update/i }))
283+
await user.click(screen.getByTestId('edit-payment-method'))
284+
await user.click(screen.getByTestId('save-payment-method'))
261285

262-
expect(updateCard).toHaveBeenCalled()
286+
expect(updatePaymentMethod).toHaveBeenCalled()
263287
})
264288
})
265289

266290
describe('when the user clicks on cancel', () => {
267291
it(`doesn't render the form anymore`, async () => {
268292
const { user } = setup()
269-
mocks.useUpdateCard.mockReturnValue({
293+
mocks.useUpdatePaymentMethod.mockReturnValue({
270294
mutate: vi.fn(),
271295
isLoading: false,
272296
})
@@ -279,11 +303,11 @@ describe('PaymentCard', () => {
279303
{ wrapper }
280304
)
281305

282-
await user.click(screen.getByTestId('edit-card'))
283-
await user.click(screen.getByRole('button', { name: /Cancel/ }))
306+
await user.click(screen.getByTestId('edit-payment-method'))
307+
await user.click(screen.getByTestId('cancel-payment'))
284308

285309
expect(
286-
screen.queryByRole('button', { name: /save/i })
310+
screen.queryByTestId('update-payment-method')
287311
).not.toBeInTheDocument()
288312
})
289313
})
@@ -293,7 +317,7 @@ describe('PaymentCard', () => {
293317
it('renders the error', async () => {
294318
const { user } = setup()
295319
const randomError = 'not rich enough'
296-
mocks.useUpdateCard.mockReturnValue({
320+
mocks.useUpdatePaymentMethod.mockReturnValue({
297321
mutate: vi.fn(),
298322
error: { message: randomError },
299323
})
@@ -306,7 +330,7 @@ describe('PaymentCard', () => {
306330
{ wrapper }
307331
)
308332

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

311335
expect(screen.getByText(randomError)).toBeInTheDocument()
312336
})
@@ -315,7 +339,7 @@ describe('PaymentCard', () => {
315339
describe('when the form is loading', () => {
316340
it('has the error and save button disabled', async () => {
317341
const { user } = setup()
318-
mocks.useUpdateCard.mockReturnValue({
342+
mocks.useUpdatePaymentMethod.mockReturnValue({
319343
mutate: vi.fn(),
320344
isLoading: true,
321345
})
@@ -327,10 +351,10 @@ describe('PaymentCard', () => {
327351
/>,
328352
{ wrapper }
329353
)
330-
await user.click(screen.getByTestId('edit-card'))
354+
await user.click(screen.getByTestId('edit-payment-method'))
331355

332-
expect(screen.queryByRole('button', { name: /update/i })).toBeDisabled()
333-
expect(screen.queryByRole('button', { name: /cancel/i })).toBeDisabled()
356+
expect(screen.getByTestId('save-payment-method')).toBeDisabled()
357+
expect(screen.getByTestId('cancel-payment')).toBeDisabled()
334358
})
335359
})
336360
})

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({

0 commit comments

Comments
 (0)