Skip to content

Commit 868b0c9

Browse files
committed
03/05: adjust soft assertions exercise
1 parent dbfd4d8 commit 868b0c9

File tree

11 files changed

+110
-207
lines changed

11 files changed

+110
-207
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fetchPosts } from './fetch-posts'
2+
3+
test('returns a list of posts', async () => {
4+
const response = await fetchPosts()
5+
6+
expect(response.status).toBe(200)
7+
expect(response.headers.get('cache-control')).toBe(
8+
'public, max-age=31536000, immutable',
9+
)
10+
await expect(response.json()).resolves.toEqual([
11+
{
12+
id: expect.any(String),
13+
title: 'Soft Assertions',
14+
},
15+
{
16+
id: expect.any(String),
17+
title: 'Implicit Assertions',
18+
},
19+
])
20+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const allPosts = [
2+
{
3+
id: '01975925-8129-7655-b3a5-b9662afb5eec',
4+
title: 'Soft Assertions',
5+
},
6+
{
7+
id: '01975925-9f95-7414-bc6f-f5c7c2923012',
8+
title: 'Implicit Assertions',
9+
},
10+
]
11+
12+
export async function fetchPosts(postIds?: Array<string>): Promise<Response> {
13+
const matchingPosts = allPosts.filter((post) => {
14+
return postIds?.includes(post.id)
15+
})
16+
17+
if (matchingPosts.length === 0) {
18+
return Response.json(
19+
{
20+
message: `Failed to find posts with IDs: ${postIds?.join(', ')}`,
21+
},
22+
{
23+
status: 404,
24+
},
25+
)
26+
}
27+
28+
return Response.json(matchingPosts, {
29+
headers: {
30+
'cache-control': 'public, max-age=31536000, immutable',
31+
},
32+
})
33+
}

exercises/03.assertions/05.problem.soft-assertions/src/plans.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

exercises/03.assertions/05.problem.soft-assertions/src/user.test.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

exercises/03.assertions/05.problem.soft-assertions/src/user.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fetchPosts } from './fetch-posts'
2+
3+
test('returns a list of posts', async () => {
4+
const response = await fetchPosts()
5+
6+
expect.soft(response.status).toBe(200)
7+
expect
8+
.soft(response.headers.get('cache-control'))
9+
.toBe('public, max-age=31536000, immutable')
10+
await expect.soft(response.json()).resolves.toEqual([
11+
{
12+
id: expect.any(String),
13+
title: 'Soft Assertions',
14+
},
15+
{
16+
id: expect.any(String),
17+
title: 'Implicit Assertions',
18+
},
19+
])
20+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const allPosts = [
2+
{
3+
id: '01975925-8129-7655-b3a5-b9662afb5eec',
4+
title: 'Soft Assertions',
5+
},
6+
{
7+
id: '01975925-9f95-7414-bc6f-f5c7c2923012',
8+
title: 'Implicit Assertions',
9+
},
10+
]
11+
12+
export async function fetchPosts(postIds?: Array<string>): Promise<Response> {
13+
const matchingPosts =
14+
postIds && postIds.length > 0
15+
? allPosts.filter((post) => {
16+
return postIds.includes(post.id)
17+
})
18+
: allPosts
19+
20+
if (matchingPosts.length === 0) {
21+
return Response.json(
22+
{
23+
message: `Failed to find posts with IDs: ${postIds?.join(', ')}`,
24+
},
25+
{
26+
status: 404,
27+
},
28+
)
29+
}
30+
31+
return Response.json(matchingPosts, {
32+
headers: {
33+
'cache-control': 'public, max-age=31536000, immutable',
34+
},
35+
})
36+
}

exercises/03.assertions/05.solution.soft-assertions/src/plans.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

exercises/03.assertions/05.solution.soft-assertions/src/user.test.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

exercises/03.assertions/05.solution.soft-assertions/src/user.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)