From 2ba3b4590a80069cef65a94e5af965ba46244348 Mon Sep 17 00:00:00 2001 From: typicode Date: Sat, 30 Dec 2023 01:32:39 +0100 Subject: [PATCH] Refactor code and update tests --- README.md | 19 ++++++++++----- src/app.test.ts | 18 +++++++------- src/service.test.ts | 57 +++++++++++++++++++++++++++++++++------------ 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2a75be3..98c5d65 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ npm install json-server@alpha ## Usage -Create a `db.json` or `db.json5` file +Create a `db.json` (or `db.json5`) file ```json { @@ -28,13 +28,13 @@ Create a `db.json` or `db.json5` file Pass it to JSON Server CLI ```shell -json-server db.json +$ json-server db.json ``` Get a REST API ```shell -curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1 +$ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1 { "id": "1", "title": "a title" @@ -58,7 +58,7 @@ DELETE /posts/:id ### Comparison -- ` ` →`==` +- ` ` → `==` - `lt` → `<` - `lte` → `<=` - `gt` → `>` @@ -97,9 +97,16 @@ GET /posts?_page=1&_per_page=25 GET /posts?_sort=id,-views ``` -### Nested fields +### Nested and array fields -- `x.y.z` +- `x.y.z_...` +- `x.y.z[i]_...` + +``` +GET /posts?author.name=foo +GET /posts?author.email=foo +GET /posts?names[0]=foo +``` ### Include diff --git a/src/app.test.ts b/src/app.test.ts index 157f7d2..03f1af6 100644 --- a/src/app.test.ts +++ b/src/app.test.ts @@ -50,7 +50,7 @@ await new Promise((resolve, reject) => { } }) -await test('createApp', async () => { +await test('createApp', async (t) => { // URLs const POSTS = '/posts' const POST_1 = '/posts/1' @@ -98,13 +98,15 @@ await test('createApp', async () => { ] for (const tc of arr) { - const response = await fetch(`http://localhost:${port}${tc.url}`, { - method: tc.method, + await t.test(`${tc.method} ${tc.url}`, async () => { + const response = await fetch(`http://localhost:${port}${tc.url}`, { + method: tc.method, + }) + assert.equal( + response.status, + tc.statusCode, + `${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`, + ) }) - assert.equal( - response.status, - tc.statusCode, - `${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`, - ) } }) diff --git a/src/service.test.ts b/src/service.test.ts index f26d6d5..d76785d 100644 --- a/src/service.test.ts +++ b/src/service.test.ts @@ -10,21 +10,41 @@ const defaultData = { posts: [] } const adapter = new Memory() const db = new Low(adapter, defaultData) const service = new Service(db) + const POSTS = 'posts' const COMMENTS = 'comments' const UNKNOWN_RESOURCE = 'xxx' const UNKNOWN_ID = 'xxx' -const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } } -const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } } -const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } } + +const post1 = { + id: '1', + title: 'a', + views: 100, + author: { name: 'foo' }, + tags: ['foo', 'bar'], +} +const post2 = { + id: '2', + title: 'b', + views: 200, + author: { name: 'bar' }, + tags: ['bar'], +} +const post3 = { + id: '3', + title: 'c', + views: 300, + author: { name: 'baz' }, + tags: ['foo'], +} const comment1 = { id: '1', title: 'a', postId: '1' } const items = 3 + function reset() { - const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } } - const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } } - const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } } - const comment1 = { id: '1', title: 'a', postId: '1' } - db.data = { posts: [post1, post2, post3], comments: [comment1] } + db.data = structuredClone({ + posts: [post1, post2, post3], + comments: [comment1], + }) } type Test = { @@ -50,7 +70,7 @@ await test('findById', () => { assert.equal(service.findById(UNKNOWN_RESOURCE, '1', {}), undefined) }) -await test('find', () => { +await test('find', async (t) => { const arr: Test[] = [ { name: POSTS, @@ -76,6 +96,11 @@ await test('find', () => { params: { 'author.name': post1.author.name }, res: [post1], }, + { + name: POSTS, + params: { 'tags[0]': 'foo' }, + res: [post1, post3], + }, { name: POSTS, params: { id: UNKNOWN_ID, views: post1.views.toString() }, @@ -213,13 +238,15 @@ await test('find', () => { }, ] for (const tc of arr) { - if (tc.data) { - db.data = tc.data - } else { - reset() - } + await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => { + if (tc.data) { + db.data = tc.data + } else { + reset() + } - assert.deepEqual(service.find(tc.name, tc.params), tc.res) + assert.deepEqual(service.find(tc.name, tc.params), tc.res) + }) } })