While building ShopPilot AI, I’ve been working on a function that checks availability for a requested product color and size.
“Out of stock” and “that variant doesn’t exist” need different answers.
I defined three cases to test:
• The variant exists and has stock.
• The variant exists but has zero stock.
• There’s no matching variant.
The interesting part was the third case.
JavaScript’s .find() returns undefined when nothing matches, so I need to handle that before reading the variant’s stock.
This is a small function, but it’s helping me connect JavaScript fundamentals, TypeScript, and testing to a real customer question.
I’m learning how to use TDD for this logic: define the expected behavior with a failing test, implement it, then refactor.
My main lesson so far: a useful test starts with a clear requirement.
Before checking whether the code works, I need to decide what it should do.
Top comments (0)