Skip to content

Instantly share code, notes, and snippets.

describe('CheckoutService', () => {
test('checkout', async () => {
const customerRepository: ICustomerRepository = {
findOne: jest.fn(() => {
const customer: Customer = {
memberType: 'VIP',
};
return customer;
}),
};
describe('CheckoutService', () => {
test('checkout', async () => {
const customerRepository: ICustomerRepository = {
async findOne(customerId: string) {
expect(customerId).toEqual('customerId');
const customer: Customer = {
memberType: 'VIP',
};
return customer;
},
export class CheckoutService {
...
async checkout(customerId: string) {
const customer = await this.customerRepository.findOne(customerId);
if (customer === null) {
throw new Error('Customer not found');
}
const shoppingItems = await this.shoppingItemRepository.findMany({
router.post('/customer/:customerId/checkout', async (req, res) => {
const { customerId } = req.params;
const checkoutService = new CheckoutService(
Customer,
ShoppingItem,
StockItem,
CreditCardService,
);
const {
totalPrice,
class CheckoutService {
constructor(
private readonly customerRepository: ICustomerRepository,
private readonly shoppingItemRepository: IShoppingItemRepository,
private readonly stockItemRepository: IStockItemRepository,
private readonly creditCardService: ICreditCardService,
) {}
async checkout(customerId: string) {
const customer = await this.customerRepository.findOne(customerId);
class CheckoutService {
customerRepository: ICustomerRepository;
shoppingItemRepository: IShoppingItemRepository;
stockItemRepository: IStockItemRepository;
creditCardService: ICreditCardService;
constructor(
customerRepository: ICustomerRepository,
shoppingItemRepository: IShoppingItemRepository,
stockItemRepository: IStockItemRepository,
interface ICustomerRepository {
findOne(customerId: string): Promise<Customer>;
}
interface IShoppingItemRepository {
findMany(where: any): Promise<ShoppingItem[]>;
}
interface IStockItemRepository {
findMany(where: any): Promise<StockItem[]>;
update(where: any, value: any): Promise<void>;
}
class CheckoutService {
customerRepository: any;
shoppingItemRepository: any;
stockItemRepository: any;
creditCardService: any;
async checkout(customerId: string) {
const customer = await this.customerRepository.findOne(customerId);
if (customer === null) {
throw new Error('Customer not found');
class CheckoutService {
async checkout(customerId: string) {
const customer = await Customer.findOne(customerId);
if (customer === null) {
throw new Error('Customer not found');
}
const shoppingItems = await ShoppingItem.findMany(
{ customerId }
);
const itemIds = shoppingItems.map(item => item.itemId);
class CheckoutService {
async checkout(customerId: string) {
const customer = await Customer.findOne(customerId);
if (customer === null) {
throw new Error('Customer not found');
}
const shoppingItems = await ShoppingItem.findMany(
{ customerId }
);
const itemIds = shoppingItems.map(item => item.itemId);