Expo stretch integration
You build app/lib/stretch.ts and update one Expo screen to show inventory availability or simulate payment through the existing API client. The helpers must preserve server payment statuses and recognise only a 409 sold_out error.
Ticket
Build app/lib/stretch.ts as the boundary between your stretch feature and the Expo screens. It provides stock display state and sold-out error recognition for inventory, and payment requests and display state for payments. The tests cover all four helpers.
The item and order screens should use these helpers instead of implementing stretch-specific business logic themselves. Then update one screen, for whichever stretch you want to show in the app. The next exercise, Final pass: tests, lint, govulncheck and a k6 numbers table, verifies the completed capstone.
The tests call these exported functions directly:
import type { ApiClient } from './api';
export function stockLabel(onHand: number | null | undefined): {
text: string | null;
disabled: boolean;
};
export function isSoldOutError(err: unknown): boolean;
export function payOrder(
api: ApiClient,
orderId: number,
outcome: 'success' | 'fail'
): Promise<{ status: string }>;
export function payScreenState(status: string): {
showPay: boolean;
title: string;
};Acceptance criteria
stockLabel(null)andstockLabel(undefined)both return{ text: null, disabled: false }.stockLabel(6)returns{ text: null, disabled: false }.stockLabel(5)returns{ text: 'Only 5 left', disabled: false }, andstockLabel(1)returns{ text: 'Only 1 left', disabled: false }.stockLabel(0)returns{ text: 'Sold out', disabled: true }.isSoldOutErrorreturnstruefor anApiErrorwith status409and response body{ code: 'sold_out' }.isSoldOutErrorreturnsfalsefor anApiErrorwith status409and codein_flight, anApiErrorwith status500and codesold_out, a networkTypeError, andnull.payOrder(api, 42, 'success')sends aPOSTrequest tohttp://api/v1/payments/42/simulatewith body{ outcome: 'success' }and theAuthorizationheaderBearer A.- When the server responds with
{ status: 'CANCELLED' },payOrderresolves with{ status: 'CANCELLED' }. - When the payment request responds with status
404and body{ error: 'unknown order' },payOrderrejects with anApiError. payScreenState('PENDING_PAYMENT')returns{ showPay: true, title: 'Pay now' }.payScreenState('PLACED')returns{ showPay: false, title: 'Payment received' }.payScreenState('CANCELLED')returns{ showPay: false, title: 'Payment failed, order cancelled' }.payScreenState('PREPARING')returns{ showPay: false, title: 'Order in progress' }.
Constraints
- Keep the tested helpers in
app/lib/stretch.ts, which the tests import as@app/stretch. - Reuse the existing
ApiClientandApiErrorfromapp/lib/api.ts. The payment request must use the existing authenticated client. - Keep the implementation compatible with the strict TypeScript configuration.
- Do not modify the provided tests.
Design direction
Keep the four helper responsibilities in app/lib/stretch.ts. Let each function convert application data into a small result that a screen can render or act on.
Choose which stretch to show in the app:
- For inventory, use
stockLabelbeneath the item name and use itsdisabledvalue for the Add to cart action. UseisSoldOutErrorwhen order placement fails. - For payments, derive the order screen's title and payment-button visibility from
payScreenState. UsepayOrderfor both simulated outcomes, then refresh the order query after a successful request.
Keep the screen responsible for rendering and user interaction. Keep HTTP error inspection, payment request construction and status mapping in the library. The inventory label is only a customer-facing indication: the backend still decides whether stock can be reserved. For payments, refresh from the backend instead of assuming that a successful request means the order is already PLACED.
Any layout that meets the contract passes. Keep this change limited to one screen. You do not need to build another stretch screen or repeat the backend workflow in the app.
How to check your work
Run the app helper tests:
cd backend
make test W=8B3Then check TypeScript compilation:
cd app
npx tsc --noEmitRead the first failing assertion to identify which helper contract is broken before changing the implementation.
After the automated checks pass, manually verify only your selected integration:
- Inventory: Open an item with stock information. Confirm that
app/app/item/[id].tsxdisplays the stock label below the item name, disables Add to cart whendisabledis true, and displaysSold out, remove it from your cartwhen order placement returns a 409sold_outresponse. - Payments: Open an order in
PENDING_PAYMENT. Confirm thatapp/app/orders/[id].tsxdisplaysPay (success)andPay (fail). Confirm that each button callspayOrderwith its corresponding outcome and refreshes the order query after success. Neither button should appear for other order statuses.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
make test W=8B3passes all 13 stretch helper tests.- TypeScript compilation succeeds with
npx tsc --noEmit. - Stock labels show the correct availability state.
- Sold-out detection rejects unrelated errors.
- Payment requests and order screen states match the server contract.
- The selected Expo screen integration works.
Commands
cd backend && make test W=8B3cd app && npx tsc --noEmitTests
W8 B3 stretch screens (13 tests)inapp-tests/lib/stretch.test.ts