Skip to content

Commit

Permalink
Add unit tests for user service
Browse files Browse the repository at this point in the history
  • Loading branch information
horvathmarton committed Jul 12, 2023
1 parent ae63c4e commit a53d570
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 0 deletions.
136 changes: 136 additions & 0 deletions packages/altair-api/custom-matchers.ts
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,
});
1 change: 1 addition & 0 deletions packages/altair-api/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = {
collectCoverageFrom: ['**/*.(t|j)s'],
coverageDirectory: '../coverage',
testEnvironment: 'node',
setupFilesAfterEnv: ['./custom-matchers.ts'],
};
40 changes: 40 additions & 0 deletions packages/altair-api/src/auth/mocks/prisma-service.mock.ts
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',
});
}
22 changes: 22 additions & 0 deletions packages/altair-api/src/auth/mocks/stripe-service.mock.ts
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>;
}
Loading

0 comments on commit a53d570

Please sign in to comment.