1+ import { QueryClient , QueryClientProvider } from '@tanstack/react-query'
12import { render , screen } from '@testing-library/react'
23import userEvent from '@testing-library/user-event'
34
@@ -6,18 +7,26 @@ import { Plans } from 'shared/utils/billing'
67
78import PaymentCard from './PaymentCard'
89
10+ const queryClient = new QueryClient ( )
11+
912const mocks = vi . hoisted ( ( ) => ( {
10- useUpdateCard : vi . fn ( ) ,
13+ useUpdatePaymentMethod : vi . fn ( ) ,
14+ useCreateStripeSetupIntent : vi . fn ( ) ,
1115} ) )
1216
1317vi . 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+
2130const subscriptionDetail = {
2231 defaultPaymentMethod : {
2332 card : {
@@ -35,7 +44,9 @@ const subscriptionDetail = {
3544}
3645
3746const 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- / N o c r e d i t c a r d s e t . P l e a s e c o n t a c t s u p p o r t i f y o u t h i n k i t ’ s a n e r r o r o r s e t i t y o u r s e l f ./
86+ / N o p a y m e n t m e t h o d s e t . P l e a s e c o n t a c t s u p p o r t i f y o u t h i n k i t ' s a n e r r o r o r s e t i t y o u r s e l f ./
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- / N o c r e d i t c a r d s e t . P l e a s e c o n t a c t s u p p o r t i f y o u t h i n k i t ’ s a n e r r o r o r s e t i t y o u r s e l f ./
108+ / N o p a y m e n t m e t h o d s e t . P l e a s e c o n t a c t s u p p o r t i f y o u t h i n k i t ' s a n e r r o r o r s e t i t y o u r s e l f ./
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 : / u p d a t e / i } )
158+ screen . getByRole ( 'button' , { name : / s a v e / 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 ( / V i s a / ) ) . 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 : / u p d a t e / i } )
240- ) . toBeInTheDocument ( )
250+ expect ( screen . getByRole ( 'button' , { name : / s a v e / 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 : / u p d a t e / i } ) )
273+ await user . click ( screen . getByTestId ( 'edit-payment-method ' ) )
274+ await user . click ( screen . getByRole ( 'button' , { name : / s a v e / 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 : / C a n c e l / } ) )
284298
285299 expect (
286- screen . queryByRole ( 'button' , { name : / s a v e / 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 : / u p d a t e / i } ) ) . toBeDisabled ( )
346+ expect ( screen . getByTestId ( ' update-payment-method' ) ) . toBeDisabled ( )
333347 expect ( screen . queryByRole ( 'button' , { name : / c a n c e l / i } ) ) . toBeDisabled ( )
334348 } )
335349 } )
0 commit comments