diff --git a/tests/blank-code.test.js b/tests/blank-code.test.js deleted file mode 100644 index 2670186..0000000 --- a/tests/blank-code.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { applyCoupon, total } from '../src/lib/pricing.js'; - -// Duplicate of empty-coupon.test.js -test('blank coupon does nothing', () => { - assert.equal(applyCoupon(50, ''), 0); - assert.equal(total([{ price: 10, qty: 1 }], { coupon: '' }), total([{ price: 10, qty: 1 }])); -}); - -test('spaces only is also no discount', () => { - assert.equal(applyCoupon(50, ' '), 0); -}); diff --git a/tests/coupons-save10.test.js b/tests/coupons-save10.test.js deleted file mode 100644 index 77a32b1..0000000 --- a/tests/coupons-save10.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { total } from '../src/lib/pricing.js'; - -test('SAVE10 makes the order cheaper', () => { - const items = [{ price: 40, qty: 1 }]; - assert.ok(total(items, { coupon: 'SAVE10' }) < total(items)); -}); - -test('save10 lowercase also makes it cheaper', () => { - const items = [{ price: 40, qty: 1 }]; - assert.ok(total(items, { coupon: 'save10' }) < total(items)); -}); diff --git a/tests/dead-helper.test.js b/tests/dead-helper.test.js deleted file mode 100644 index 0831a9e..0000000 --- a/tests/dead-helper.test.js +++ /dev/null @@ -1,8 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -// legacyComputeDiscount was removed when applyCoupon landed; this file never got deleted. -import { legacyComputeDiscount } from '../src/lib/pricing.js'; - -test('legacy helper still works', () => { - assert.equal(legacyComputeDiscount(100, 'SAVE10'), 10); -}); diff --git a/tests/discount-percent.test.js b/tests/discount-percent.test.js deleted file mode 100644 index 2f2c111..0000000 --- a/tests/discount-percent.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { total } from '../src/lib/pricing.js'; - -test('percent off coupon reduces the bill', () => { - const items = [{ price: 40, qty: 1 }]; - assert.ok(total(items, { coupon: 'SAVE10' }) < total(items)); -}); - -test('coupon works when written in lowercase', () => { - const items = [{ price: 40, qty: 1 }]; - assert.ok(total(items, { coupon: 'save10' }) < total(items)); -}); diff --git a/tests/flat5-again.test.js b/tests/flat5-again.test.js deleted file mode 100644 index 846494e..0000000 --- a/tests/flat5-again.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { total, applyCoupon, subtotal } from '../src/lib/pricing.js'; -import { roundCents } from '../src/lib/money.js'; -import { TAX_RATE } from '../src/generated/rates.js'; - -test('flat five dollar coupon on the big order', () => { - const items = [ - { price: 12.5, qty: 2 }, - { price: 7, qty: 1 }, - ]; - const sub = subtotal(items); - assert.equal(applyCoupon(sub, 'FLAT5'), 5); - assert.equal(total(items, { coupon: 'FLAT5' }), roundCents((sub - 5) * (1 + TAX_RATE))); -}); - -test('FLAT5 also works in mixed case', () => { - const items = [ - { price: 12.5, qty: 2 }, - { price: 7, qty: 1 }, - ]; - const sub = subtotal(items); - assert.equal(applyCoupon(sub, ' flat5 '), 5); - assert.equal(total(items, { coupon: 'Flat5' }), roundCents((sub - 5) * (1 + TAX_RATE))); -}); diff --git a/tests/flat5.test.js b/tests/flat5.test.js index 4c4cffa..36f5cb5 100644 --- a/tests/flat5.test.js +++ b/tests/flat5.test.js @@ -4,13 +4,25 @@ import { total, applyCoupon, subtotal } from '../src/lib/pricing.js'; import { roundCents } from '../src/lib/money.js'; import { TAX_RATE } from '../src/generated/rates.js'; +const BIG_CART = [ + { price: 12.5, qty: 2 }, + { price: 7, qty: 1 }, +]; + test('FLAT5 takes five dollars off a large cart', () => { - const items = [ - { price: 12.5, qty: 2 }, - { price: 7, qty: 1 }, - ]; - const sub = subtotal(items); // 32 - const disc = applyCoupon(sub, 'FLAT5'); - assert.equal(disc, 5); - assert.equal(total(items, { coupon: 'FLAT5' }), roundCents((sub - 5) * (1 + TAX_RATE))); + const sub = subtotal(BIG_CART); // 32 + assert.equal(applyCoupon(sub, 'FLAT5'), 5); + assert.equal(total(BIG_CART, { coupon: 'FLAT5' }), roundCents((sub - 5) * (1 + TAX_RATE))); +}); + +test('FLAT5 ignores surrounding spaces and letter case', () => { + const sub = subtotal(BIG_CART); + assert.equal(applyCoupon(sub, ' flat5 '), 5); + assert.equal(total(BIG_CART, { coupon: 'Flat5' }), roundCents((sub - 5) * (1 + TAX_RATE))); +}); + +test('FLAT5 discount is capped at the subtotal on a small order', () => { + const items = [{ price: 3, qty: 1 }]; + assert.equal(applyCoupon(3, 'FLAT5'), 3); + assert.equal(total(items, { coupon: 'FLAT5' }), 0); }); diff --git a/tests/legacy-skipped.test.js b/tests/legacy-skipped.test.js deleted file mode 100644 index 0686b69..0000000 --- a/tests/legacy-skipped.test.js +++ /dev/null @@ -1,21 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { total } from '../src/lib/pricing.js'; - -test.skip('old seasonal coupon WINTER20 (removed from product)', () => { - const items = [{ price: 100, qty: 1 }]; - assert.equal(total(items, { coupon: 'WINTER20' }), 86.4); -}); - -test.skip('TODO: gift-card stacking', () => { - assert.ok(true); -}); - -// Was useful once; left commented out -// test('BLACKFRIDAY50', () => { -// assert.equal(total([{ price: 20, qty: 1 }], { coupon: 'BLACKFRIDAY50' }), 10.8); -// }); - -test('sanity: a plain cart still totals', () => { - assert.ok(total([{ price: 1, qty: 1 }]) > 0); -}); diff --git a/tests/noop-assert.test.js b/tests/noop-assert.test.js deleted file mode 100644 index f9e845a..0000000 --- a/tests/noop-assert.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { total } from '../src/lib/pricing.js'; - -test('total returns something', () => { - const r = total([{ price: 9.99, qty: 3 }], { coupon: 'SAVE10' }); - assert.ok(typeof r === 'number'); -}); - -test('coupon option is accepted', () => { - total([{ price: 50, qty: 1 }], { coupon: 'SAVE10' }); - assert.ok(true); -}); diff --git a/tests/save10-another-cart.test.js b/tests/save10.test.js similarity index 52% rename from tests/save10-another-cart.test.js rename to tests/save10.test.js index 43190bf..15d2de3 100644 --- a/tests/save10-another-cart.test.js +++ b/tests/save10.test.js @@ -1,10 +1,20 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { applyCoupon, total, subtotal } from '../src/lib/pricing.js'; +import { applyCoupon, subtotal, total } from '../src/lib/pricing.js'; import { roundCents } from '../src/lib/money.js'; import { TAX_RATE } from '../src/generated/rates.js'; -test('SAVE10 works on another cart', () => { +test('SAVE10 lowers the total', () => { + const items = [{ price: 40, qty: 1 }]; + assert.ok(total(items, { coupon: 'SAVE10' }) < total(items)); +}); + +test('lowercase save10 lowers the total', () => { + const items = [{ price: 40, qty: 1 }]; + assert.ok(total(items, { coupon: 'save10' }) < total(items)); +}); + +test('SAVE10 discount is rounded to the cent', () => { const items = [{ price: 10.05, qty: 1 }]; const sub = subtotal(items); assert.equal(sub, 10.05); diff --git a/tests/small-order-coupon.test.js b/tests/small-order-coupon.test.js deleted file mode 100644 index 3ca0fa4..0000000 --- a/tests/small-order-coupon.test.js +++ /dev/null @@ -1,9 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { applyCoupon, total } from '../src/lib/pricing.js'; - -test('coupon on a small order still makes it cheaper', () => { - const items = [{ price: 3, qty: 1 }]; - assert.equal(applyCoupon(3, 'FLAT5'), 3); - assert.equal(total(items, { coupon: 'FLAT5' }), 0); -}); diff --git a/tests/pricing-basic.test.js b/tests/subtotal-and-tax.test.js similarity index 77% rename from tests/pricing-basic.test.js rename to tests/subtotal-and-tax.test.js index 2364cb0..a42c556 100644 --- a/tests/pricing-basic.test.js +++ b/tests/subtotal-and-tax.test.js @@ -4,7 +4,7 @@ import { subtotal, total } from '../src/lib/pricing.js'; import { roundCents } from '../src/lib/money.js'; import { TAX_RATE } from '../src/generated/rates.js'; -test('subtotal adds price times qty', () => { +test('subtotal multiplies price by quantity and sums the lines', () => { const items = [ { price: 10, qty: 2 }, { price: 5, qty: 1 }, @@ -12,7 +12,7 @@ test('subtotal adds price times qty', () => { assert.equal(subtotal(items), 25); }); -test('total applies tax with no coupon', () => { +test('total applies tax when no coupon is given', () => { const items = [{ price: 10, qty: 1 }]; assert.equal(total(items), roundCents(10 * (1 + TAX_RATE))); }); diff --git a/tests/bad-code.test.js b/tests/unknown-coupon.test.js similarity index 70% rename from tests/bad-code.test.js rename to tests/unknown-coupon.test.js index 9de800d..504949e 100644 --- a/tests/bad-code.test.js +++ b/tests/unknown-coupon.test.js @@ -2,10 +2,10 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { applyCoupon } from '../src/lib/pricing.js'; -test('bad code fails', () => { +test('unknown coupon code throws naming the code', () => { assert.throws(() => applyCoupon(20, 'BOGUS'), { message: 'Unknown coupon: BOGUS' }); }); -test('bad code with spaces fails the same way', () => { +test('unknown coupon code is reported without surrounding spaces', () => { assert.throws(() => applyCoupon(20, ' nope '), { message: 'Unknown coupon: nope' }); });