-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathstripe.mock.ts
More file actions
71 lines (66 loc) · 2.1 KB
/
Copy pathstripe.mock.ts
File metadata and controls
71 lines (66 loc) · 2.1 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
import type Stripe from 'stripe'
import { vi } from 'vitest'
/**
* Mock for `@/lib/billing/stripe-client`.
*
* @example
* ```ts
* import { stripeClientMock, stripeClientMockFns } from '@sim/testing'
* vi.mock('@/lib/billing/stripe-client', () => stripeClientMock)
*
* stripeClientMockFns.mockRequireStripeClient.mockReturnValue(fakeStripe)
* ```
*/
export const stripeClientMockFns = {
mockRequireStripeClient: vi.fn(),
mockGetStripeClient: vi.fn(),
mockHasValidStripeCredentials: vi.fn(() => true),
}
export const stripeClientMock = {
requireStripeClient: stripeClientMockFns.mockRequireStripeClient,
getStripeClient: stripeClientMockFns.mockGetStripeClient,
hasValidStripeCredentials: stripeClientMockFns.mockHasValidStripeCredentials,
}
/**
* Mock for `@/lib/billing/stripe-payment-method`.
*
* @example
* ```ts
* import { stripePaymentMethodMock, stripePaymentMethodMockFns } from '@sim/testing'
* vi.mock('@/lib/billing/stripe-payment-method', () => stripePaymentMethodMock)
* ```
*/
export const stripePaymentMethodMockFns = {
mockResolveDefaultPaymentMethod: vi.fn(async () => ({
paymentMethodId: undefined as string | undefined,
collectionMethod: 'charge_automatically' as 'charge_automatically' | 'send_invoice' | null,
})),
mockGetCustomerId: vi.fn(),
}
export const stripePaymentMethodMock = {
resolveDefaultPaymentMethod: stripePaymentMethodMockFns.mockResolveDefaultPaymentMethod,
getCustomerId: stripePaymentMethodMockFns.mockGetCustomerId,
}
/**
* Build a minimal `Stripe.Event` with the given type and object payload.
* Fills in a deterministic `id` (`evt_${type}`) and nests `object` under
* `data.object` as Stripe does.
*/
export function createMockStripeEvent<T = unknown>(
type: string,
object: T,
overrides: Partial<Stripe.Event> = {}
): Stripe.Event {
return {
id: `evt_${type}`,
object: 'event',
api_version: '2024-06-20',
created: Math.floor(Date.now() / 1000),
livemode: false,
pending_webhooks: 0,
request: null,
type,
data: { object: object as unknown as Stripe.Event.Data.Object },
...overrides,
} as Stripe.Event
}