|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const tap = require("tap"); |
| 4 | +const fastify = require("./server"); |
| 5 | + |
| 6 | +tap.test("should test all API endpoint", async (t) => { |
| 7 | + let book = {}; |
| 8 | + |
| 9 | + const app = await fastify(); |
| 10 | + |
| 11 | + t.teardown(() => app.close()); |
| 12 | + // only to test error path |
| 13 | + t.test("POST / should success create item", async (t) => { |
| 14 | + const response = await app.inject({ |
| 15 | + method: "POST", |
| 16 | + url: "/", |
| 17 | + payload: { |
| 18 | + title: "Hello, World!", |
| 19 | + }, |
| 20 | + }); |
| 21 | + const json = response.json(); |
| 22 | + t.equal(response.statusCode, 200); |
| 23 | + t.equal(json[0].title, "Hello, World!"); |
| 24 | + // assign created to use as compare value |
| 25 | + book = json[0]; |
| 26 | + }); |
| 27 | + |
| 28 | + t.test("GET / should success return items", async (t) => { |
| 29 | + const response = await app.inject({ |
| 30 | + method: "GET", |
| 31 | + url: "/", |
| 32 | + }); |
| 33 | + const json = response.json(); |
| 34 | + t.equal(response.statusCode, 200); |
| 35 | + t.equal(json.length > 0, true); |
| 36 | + t.equal(json[0].title, "Hello, World!"); |
| 37 | + }); |
| 38 | + |
| 39 | + t.test("GET /:id should success return item", async (t) => { |
| 40 | + const response = await app.inject({ |
| 41 | + method: "GET", |
| 42 | + url: `/${book.id}`, |
| 43 | + }); |
| 44 | + const json = response.json(); |
| 45 | + t.equal(response.statusCode, 200); |
| 46 | + t.equal(json[0].title, "Hello, World!"); |
| 47 | + }); |
| 48 | + |
| 49 | + t.test("UPDATE /:id should success return item", async (t) => { |
| 50 | + const response = await app.inject({ |
| 51 | + method: "PATCH", |
| 52 | + url: `/${book.id}`, |
| 53 | + payload: { |
| 54 | + title: "Hello again, World!", |
| 55 | + }, |
| 56 | + }); |
| 57 | + const json = response.json(); |
| 58 | + t.equal(response.statusCode, 200); |
| 59 | + t.equal(json[0].title, "Hello again, World!"); |
| 60 | + }); |
| 61 | + |
| 62 | + t.test("DELETE /:id should success delete item", async (t) => { |
| 63 | + const response = await app.inject({ |
| 64 | + method: "DELETE", |
| 65 | + url: `/${book.id}`, |
| 66 | + }); |
| 67 | + const json = response.json(); |
| 68 | + t.equal(response.statusCode, 200); |
| 69 | + t.equal(json.rowCount, 1); |
| 70 | + }); |
| 71 | +}); |
0 commit comments