-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae63c4e
commit a53d570
Showing
5 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
function typeCheck(name: string, value: unknown, expectedType: string) { | ||
const type = typeof value; | ||
if (type != expectedType) { | ||
return { | ||
pass: false, | ||
message: () => | ||
`expected ${name} (value: ${value}) to be ${expectedType}, but found ${type} instead.`, | ||
}; | ||
} | ||
|
||
return { pass: true, message: () => '' }; | ||
} | ||
|
||
function toBeUser(user: any) { | ||
let check = typeCheck('User.id', user?.id, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('User.email', user?.email, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('User.firstName', user?.firstName, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('User.lastName', user?.lastName, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('User.picture', user?.picture, 'string'); | ||
if (!check.pass) return check; | ||
|
||
return { | ||
pass: true, | ||
message: () => `expected ${user} not to match the shape of a User object`, | ||
}; | ||
} | ||
|
||
function toBePlanConfig(planConfig: any) { | ||
let check = typeCheck('PlanConfig.id', planConfig?.id, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'PlanConfig.stripeProductId', | ||
planConfig?.stripeProductId, | ||
'string' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'PlanConfig.maxQueryCount', | ||
planConfig?.maxQueryCount, | ||
'number' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'PlanConfig.maxTeamCount', | ||
planConfig?.maxTeamCount, | ||
'number' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'PlanConfig.maxTeamMemberCount', | ||
planConfig?.maxTeamMemberCount, | ||
'number' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'PlanConfig.allowMoreTeamMembers', | ||
planConfig?.allowMoreTeamMembers, | ||
'boolean' | ||
); | ||
if (!check.pass) return check; | ||
|
||
return { | ||
pass: true, | ||
message: () => | ||
`expected ${planConfig} not to match the shape of a PlanConfig object`, | ||
}; | ||
} | ||
|
||
function toBeSubscriptionItem(subItem: any) { | ||
let check = typeCheck('SubscriptionItem.id', subItem?.id, 'string'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.object', subItem?.object, 'object'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'SubscriptionItem.billing_thresholds', | ||
subItem?.billing_thresholds, | ||
'object' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.created', subItem?.created, 'number'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.metadata', subItem?.metadata, 'object'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.plan', subItem?.plan, 'object'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.price', subItem?.price, 'object'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'SubscriptionItem.subscription', | ||
subItem?.subscription, | ||
'string' | ||
); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck('SubscriptionItem.tax_rates', subItem?.tax_rates, 'object'); | ||
if (!check.pass) return check; | ||
|
||
check = typeCheck( | ||
'SubscriptionItem.lastResponse', | ||
subItem?.lastResponse, | ||
'object' | ||
); | ||
if (!check.pass) return check; | ||
|
||
return { | ||
pass: true, | ||
message: () => | ||
`expected ${subItem} not to match the shape of a SubscriptionItem object`, | ||
}; | ||
} | ||
|
||
expect.extend({ | ||
toBeUser, | ||
toBePlanConfig, | ||
toBeSubscriptionItem, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { PlanConfig, User, UserPlan } from '@altairgraphql/db'; | ||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; | ||
|
||
export function mockUser(): User { | ||
return { | ||
id: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
stripeCustomerId: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
email: 'john.doe@email.com', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
picture: 'asdf', | ||
} as User; | ||
} | ||
|
||
export function mockUserPlan(): UserPlan { | ||
return { | ||
userId: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
planRole: 'my role', | ||
quantity: 1, | ||
planConfig: mockPlanConfig(), | ||
} as UserPlan; | ||
} | ||
|
||
export function mockPlanConfig(): PlanConfig { | ||
return { | ||
id: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
stripeProductId: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
maxQueryCount: 1, | ||
maxTeamCount: 1, | ||
maxTeamMemberCount: 1, | ||
allowMoreTeamMembers: false, | ||
} as PlanConfig; | ||
} | ||
|
||
export function mockPrismaConflictError() { | ||
return new PrismaClientKnownRequestError('User already exists', { | ||
code: 'P2002', | ||
clientVersion: '1.0.0', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Stripe from 'stripe'; | ||
|
||
export function mockStripeCustomer(): Stripe.Customer { | ||
return { | ||
id: 'dad57297-637d-4598-862b-9dedd84121fe', | ||
} as Stripe.Customer; | ||
} | ||
|
||
export function mockSubscriptionItem(): Stripe.Response<Stripe.SubscriptionItem> { | ||
return { | ||
id: 'f7102dc9-4c0c-42b4-9a17-e2bd4af94d5a', | ||
object: {}, | ||
billing_thresholds: {}, | ||
created: 1, | ||
metadata: {}, | ||
plan: {}, | ||
price: {}, | ||
subscription: 'my sub', | ||
tax_rates: [], | ||
lastResponse: {}, | ||
} as Stripe.Response<Stripe.SubscriptionItem>; | ||
} |
Oops, something went wrong.