Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion apps/pr-reviewer/fixtures/demo-pr/cart-total.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { cartTotal } from './cart-total.ts';
import { cartTotal, checkoutTotal } from './cart-total.ts';

describe('cartTotal', () => {
it('returns 0 for an empty cart', () => {
Expand Down Expand Up @@ -64,3 +64,15 @@ describe('cartTotal', () => {
expect(cartTotal([{ price: 3, qty: 1_000_000 }])).toBe(3_000_000);
});
});

describe('checkoutTotal', () => {
it('ships standard orders over the threshold for free', () => {
expect(checkoutTotal({ items: [{ price: 60, qty: 2 }], weightKg: 1, taxRate: 0 })).toBe(120);
});

it('still charges for express shipping over the threshold', () => {
expect(
checkoutTotal({ items: [{ price: 60, qty: 2 }], weightKg: 1, express: true, taxRate: 0 }),
).toBe(132);
});
});
4 changes: 3 additions & 1 deletion apps/pr-reviewer/fixtures/demo-pr/cart-total.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ export type Cart = {
discount?: { code: string; percent: number };
};

const FREE_SHIPPING_THRESHOLD = 100;

function roundCents(amount: number): number {
return Math.round(amount * 100) / 100;
}

export function checkoutTotal(cart: Cart): number {
const subtotal = cartTotal(cart.items);
const shipping = shippingCost(cart.weightKg, cart.express);
const freeShipping = subtotal >= 100;
const freeShipping = subtotal >= FREE_SHIPPING_THRESHOLD && !cart.express;
const shippingCharge = freeShipping ? 0 : shipping;
const taxable = subtotal + shippingCharge;
const tax = roundCents(taxable * cart.taxRate);
Expand Down
Loading