Skip to content

Commit c34937c

Browse files
committed
Make tests more flexible and fix documentation
- Make optional properties test accept any bio string value - Add note that reduce() is covered later with for-loop alternative - Make formatUserCard test flexible for any format with required fields - Fix incorrect callout (spread/rest was Exercise 3, not "next exercise") - Clarify tuple type alias is optional - Make enum tests flexible for order values and status messages
1 parent b985946 commit c34937c

File tree

6 files changed

+97
-50
lines changed

6 files changed

+97
-50
lines changed

exercises/01.objects/03.solution.optional-properties/index.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ await test('User with optional properties should work', () => {
5050
5151
'🚨 bob.email should be "[email protected]" - make sure you set the required email property',
5252
)
53-
assert.strictEqual(
54-
solution.bob.bio,
55-
'Software developer and TypeScript enthusiast',
56-
'🚨 bob.bio should be "Software developer and TypeScript enthusiast" - optional properties can be included when provided',
53+
assert.ok(
54+
typeof solution.bob.bio === 'string' && solution.bob.bio.length > 0,
55+
'🚨 bob.bio should be a non-empty string - include a bio for bob to demonstrate optional properties',
5756
)
58-
assert.strictEqual(
59-
solution.bob.website,
60-
'https://bob.dev',
61-
'🚨 bob.website should be "https://bob.dev" - optional properties can be included when provided',
57+
assert.ok(
58+
typeof solution.bob.website === 'string' &&
59+
solution.bob.website.startsWith('http'),
60+
'🚨 bob.website should be a valid URL string - include a website for bob to demonstrate optional properties',
6261
)
6362
})

exercises/03.spread-rest/02.problem.rest-parameters/README.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ sum(1, 2) // 3
1212
sum(1, 2, 3, 4, 5) // 15
1313
```
1414

15+
<callout-info>
16+
The `.reduce()` method shown above is covered later in Exercise 6. For now,
17+
you can use a simple `for` loop instead:
18+
19+
```ts
20+
function sum(...numbers: Array<number>): number {
21+
let total = 0
22+
for (const n of numbers) {
23+
total += n
24+
}
25+
return total
26+
}
27+
```
28+
29+
</callout-info>
30+
1531
## Rest vs Spread
1632

1733
The `...` syntax means different things in different contexts:

exercises/04.destructuring/01.solution.object-destructuring/index.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ await test('bio should have a default value', () => {
7070
})
7171

7272
await test('formatUserCard should use parameter destructuring', () => {
73-
assert.strictEqual(
74-
solution.formatUserCard({
75-
id: 'test',
76-
name: 'Bob',
77-
78-
role: 'user',
79-
}),
80-
'Bob (user) - [email protected]',
81-
'🚨 formatUserCard should return formatted string with name, role, and email',
73+
const result = solution.formatUserCard({
74+
id: 'test',
75+
name: 'Bob',
76+
77+
role: 'user',
78+
})
79+
assert.ok(
80+
typeof result === 'string' &&
81+
result.includes('Bob') &&
82+
result.includes('user') &&
83+
result.includes('[email protected]'),
84+
'🚨 formatUserCard should return a string containing the name, role, and email',
8285
)
8386
})

exercises/04.destructuring/02.problem.array-destructuring/README.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const [head, ...rest] = [1, 2, 3, 4, 5]
2727
```
2828

2929
<callout-info>
30-
We will cover the full spread/rest syntax in the next exercise. For now, just
31-
use the pattern as shown.
30+
The spread/rest syntax was covered in Exercise 3. The `...rest` pattern here
31+
collects remaining array elements into a new array.
3232
</callout-info>
3333

3434
## Swapping Variables

exercises/05.tuples/01.problem.fixed-length/README.mdx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ of numbers: latitude and longitude.
55

66
🐨 Open <InlineFile file="index.ts" /> and:
77

8-
1. Create a `Coordinate` tuple type `[number, number]`
9-
2. Create several coordinate values
10-
3. Write a function that calculates distance between coordinates
8+
1. Create coordinate values typed as `[number, number]` tuples
9+
2. Write a `formatCoordinate` function that takes a coordinate tuple
10+
3. Write a `getDistance` function that calculates distance between two coordinates
11+
12+
💰 You can use inline tuple types like `[number, number]` directly, or create a
13+
type alias if you prefer:
14+
15+
```ts
16+
type Coordinate = [number, number]
17+
```
1118
1219
💰 Tuple syntax:
1320

exercises/07.enums/01.solution.string-enums/index.test.ts

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,64 @@ await test('OrderStatus enum should have correct values', () => {
4747
})
4848

4949
await test('Order object should use OrderStatus enum correctly', () => {
50-
assert.strictEqual(
51-
solution.order.status,
50+
const validStatuses = [
51+
solution.OrderStatus.Pending,
52+
solution.OrderStatus.Processing,
5253
solution.OrderStatus.Shipped,
53-
'🚨 order.status should be OrderStatus.Shipped - use the enum value when assigning to the status property',
54+
solution.OrderStatus.Delivered,
55+
]
56+
assert.ok(
57+
validStatuses.includes(solution.order.status),
58+
'🚨 order.status should be one of the OrderStatus enum values',
5459
)
55-
assert.strictEqual(
56-
solution.order.id,
57-
'ORD-001',
58-
'🚨 order.id should be "ORD-001"',
60+
assert.ok(
61+
typeof solution.order.id === 'string' && solution.order.id.length > 0,
62+
'🚨 order.id should be a non-empty string',
5963
)
60-
assert.strictEqual(
61-
solution.order.customerName,
62-
'Alice Johnson',
63-
'🚨 order.customerName should be "Alice Johnson"',
64+
assert.ok(
65+
typeof solution.order.customerName === 'string' &&
66+
solution.order.customerName.length > 0,
67+
'🚨 order.customerName should be a non-empty string',
6468
)
6569
})
6670

67-
await test('getStatusMessage should return correct messages', () => {
68-
assert.strictEqual(
69-
solution.getStatusMessage(solution.OrderStatus.Pending),
70-
'Your order is pending confirmation',
71-
'🚨 getStatusMessage(OrderStatus.Pending) should return the correct message - use a switch statement to handle each enum case',
71+
await test('getStatusMessage should return a message for each status', () => {
72+
// Each status should return a non-empty string message
73+
const pendingMsg = solution.getStatusMessage(solution.OrderStatus.Pending)
74+
assert.ok(
75+
typeof pendingMsg === 'string' && pendingMsg.length > 0,
76+
'🚨 getStatusMessage(OrderStatus.Pending) should return a message string',
7277
)
73-
assert.strictEqual(
74-
solution.getStatusMessage(solution.OrderStatus.Processing),
75-
'Your order is being processed',
76-
'🚨 getStatusMessage(OrderStatus.Processing) should return the correct message - handle the Processing case in your switch',
78+
79+
const processingMsg = solution.getStatusMessage(
80+
solution.OrderStatus.Processing,
7781
)
78-
assert.strictEqual(
79-
solution.getStatusMessage(solution.OrderStatus.Shipped),
80-
'Your order has been shipped!',
81-
'🚨 getStatusMessage(OrderStatus.Shipped) should return the correct message - handle the Shipped case in your switch',
82+
assert.ok(
83+
typeof processingMsg === 'string' && processingMsg.length > 0,
84+
'🚨 getStatusMessage(OrderStatus.Processing) should return a message string',
8285
)
83-
assert.strictEqual(
84-
solution.getStatusMessage(solution.OrderStatus.Delivered),
85-
'Your order has been delivered',
86-
'🚨 getStatusMessage(OrderStatus.Delivered) should return the correct message - handle the Delivered case in your switch',
86+
87+
const shippedMsg = solution.getStatusMessage(solution.OrderStatus.Shipped)
88+
assert.ok(
89+
typeof shippedMsg === 'string' && shippedMsg.length > 0,
90+
'🚨 getStatusMessage(OrderStatus.Shipped) should return a message string',
91+
)
92+
93+
const deliveredMsg = solution.getStatusMessage(solution.OrderStatus.Delivered)
94+
assert.ok(
95+
typeof deliveredMsg === 'string' && deliveredMsg.length > 0,
96+
'🚨 getStatusMessage(OrderStatus.Delivered) should return a message string',
97+
)
98+
99+
// Messages should be different for different statuses
100+
const messages = new Set([
101+
pendingMsg,
102+
processingMsg,
103+
shippedMsg,
104+
deliveredMsg,
105+
])
106+
assert.ok(
107+
messages.size === 4,
108+
'🚨 Each status should return a unique message',
87109
)
88110
})

0 commit comments

Comments
 (0)