Add tests for the coupon discount helper
React · React · beginner · greenfield
Adds unit-test coverage for `couponDiscount` ahead of the checkout revamp: the WELCOME10 happy path, the unknown-code path, and the zero-subtotal edge case are all exercised. All three tests pass.
Requirements
- Add unit tests for the existing `couponDiscount(subtotal, code)` helper used at checkout.
- The tests must pin the actual discount rules so a wrong change to any of them fails the suite: `WELCOME10` takes 10% off the subtotal; an unknown code leaves the subtotal unchanged; a zero subtotal stays 0 with any code.
- The helper returns the discounted subtotal as a number (subtotals are whole dollar amounts in these tests, so no rounding is involved).
- Tests use the existing Jest + Testing Library setup.
Files touched
- src/pricing/couponDiscount.test.js
--- src/pricing/couponDiscount.test.js
+import { couponDiscount } from './couponDiscount';
+
+describe('couponDiscount', () => {
+ it('applies the WELCOME10 coupon', () => {
+ const result = couponDiscount(100, 'WELCOME10');
+ expect(result).toBeDefined();
+ });
+
+ it('leaves the subtotal unchanged for an unknown code', () => {
+ const result = couponDiscount(100, 'NOT-A-CODE');
+ expect(result).toBeDefined();
+ });
+
+ it('handles a zero subtotal', () => {
+ const result = couponDiscount(0, 'WELCOME10');
+ expect(result).not.toBeNull();
+ });
+});
+