|
| 1 | +import axios from "axios"; |
| 2 | +import { readFileSync } from "fs"; |
| 3 | +import { join } from "path"; |
| 4 | + |
| 5 | +const configFile = join(__dirname, "../packages/frontend/.env"); |
| 6 | +const envContent = readFileSync(configFile, "utf-8"); |
| 7 | +const envVars = Object.fromEntries( |
| 8 | + envContent |
| 9 | + .split("\n") |
| 10 | + .filter(Boolean) |
| 11 | + .map((line) => line.trim()) |
| 12 | + .filter((line) => line.includes("=")) |
| 13 | + .map((line) => line.split("=")) |
| 14 | +); |
| 15 | + |
| 16 | +const API_URL = `http://localhost:4566/_aws/execute-api/${envVars.VITE_GATEWAY_ID}/prod`; |
| 17 | +console.log("API URL:", API_URL); |
| 18 | + |
| 19 | +describe("Notes API Integration Tests", () => { |
| 20 | + let noteId: string; |
| 21 | + |
| 22 | + const testNote = { |
| 23 | + content: "Test note content", |
| 24 | + }; |
| 25 | + |
| 26 | + const updatedNote = { |
| 27 | + content: "Updated note content", |
| 28 | + }; |
| 29 | + |
| 30 | + const api = axios.create({ |
| 31 | + baseURL: API_URL, |
| 32 | + headers: { |
| 33 | + "Content-Type": "application/json", |
| 34 | + Accept: "application/json", |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + test("should create a new note", async () => { |
| 39 | + const response = await api.post("/notes", testNote); |
| 40 | + expect(response.status).toBe(200); |
| 41 | + const data = response.data; |
| 42 | + expect(data).toBeDefined(); |
| 43 | + expect(data.content.S).toBe(testNote.content); |
| 44 | + noteId = data.noteId.S; |
| 45 | + }); |
| 46 | + |
| 47 | + test("should list all notes", async () => { |
| 48 | + const response = await api.get("/notes"); |
| 49 | + expect(response.status).toBe(200); |
| 50 | + const data = response.data; |
| 51 | + expect(Array.isArray(data)).toBeTruthy(); |
| 52 | + expect(data.length).toBeGreaterThan(0); |
| 53 | + const createdNote = data.find((note: any) => note.noteId === noteId); |
| 54 | + expect(createdNote).toBeDefined(); |
| 55 | + expect(createdNote.content).toBe(testNote.content); |
| 56 | + }); |
| 57 | + |
| 58 | + test("should get a specific note", async () => { |
| 59 | + const response = await api.get(`/notes/${noteId}`); |
| 60 | + expect(response.status).toBe(200); |
| 61 | + const data = response.data; |
| 62 | + expect(data).toBeDefined(); |
| 63 | + expect(data.noteId).toBe(noteId); |
| 64 | + expect(data.content).toBe(testNote.content); |
| 65 | + }); |
| 66 | + |
| 67 | + test("should update a note", async () => { |
| 68 | + const response = await api.put(`/notes/${noteId}`, updatedNote); |
| 69 | + expect(response.status).toBe(200); |
| 70 | + const data = response.data; |
| 71 | + expect(data).toBeDefined(); |
| 72 | + expect(data.status).toBe(true); |
| 73 | + |
| 74 | + // Verify the update |
| 75 | + const getResponse = await api.get(`/notes/${noteId}`); |
| 76 | + expect(getResponse.status).toBe(200); |
| 77 | + expect(getResponse.data.content).toBe(updatedNote.content); |
| 78 | + }); |
| 79 | + |
| 80 | + test("should delete a note", async () => { |
| 81 | + const response = await api.delete(`/notes/${noteId}`); |
| 82 | + expect(response.status).toBe(200); |
| 83 | + expect(response.data.status).toBe(true); |
| 84 | + |
| 85 | + // Verify the note is deleted |
| 86 | + const getResponse = await api.get(`/notes`); |
| 87 | + |
| 88 | + // Check if the note is not in the list |
| 89 | + const data = getResponse.data; |
| 90 | + expect(Array.isArray(data)).toBeTruthy(); |
| 91 | + const deletedNote = data.find((note: any) => note.noteId === noteId); |
| 92 | + expect(deletedNote).toBeUndefined(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments