All files / src/mocks handlers.ts

7.69% Statements 1/13
0% Branches 0/2
0% Functions 0/5
7.69% Lines 1/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86    9x                                                                                                                                                                      
import { http, HttpResponse } from "msw";
 
export const handlers = [
  // --- AUTHENTICATION HANDLERS ---
  http.post("/api/auth/login", async ({ request }) => {
    // Access request body asynchronously using request.json() in MSW 2.x
    const { username, password } = (await request.json()) as any;
 
    if (username === "validuser" && password === "validpass123") {
      // Successful login (HTTP 200)
      return HttpResponse.json({
        success: true,
        message: "Login successful",
        token: "jwt-token",
      });
    } else {
      // Failed login (HTTP 401 Unauthorized)
      return HttpResponse.json(
        {
          success: false,
          message: "Invalid credentials",
        },
        { status: 401 }
      ); // Set the status code via the options object
    }
  }),
 
  // --- PRODUCT LIST HANDLER (GET /api/products) ---
  http.get("/api/products", () => {
    // Mocked list response
    return HttpResponse.json({
      content: [
        {
          id: 1,
          name: "Test Product",
          price: 100,
          quantity: 10,
          description: "Test Description",
          category: "Electronics",
        },
      ],
    });
  }),
 
  // --- GET PRODUCT BY ID HANDLER (GET /api/products/:id) ---
  http.get("/api/products/:id", ({ params }) => {
    const { id } = params;
    // Mocked single product response
    return HttpResponse.json({
      id: Number(id),
      name: "Test Product",
      price: 100,
      quantity: 10,
      description: "Test Description",
      category: "Electronics",
    });
  }),
 
  // --- CREATE PRODUCT HANDLER (POST /api/products) ---
  http.post("/api/products", async ({ request }) => {
    const product = (await request.json()) as any;
    // Mocked creation response with a new ID
    return HttpResponse.json({
      id: 1, // Mocked ID
      ...product,
    });
  }),
 
  // --- UPDATE PRODUCT HANDLER (PUT /api/products/:id) ---
  http.put("/api/products/:id", async ({ params, request }) => {
    const { id } = params;
    const product = (await request.json()) as any;
    // Mocked update response
    return HttpResponse.json({
      id: Number(id),
      ...product,
    });
  }),
 
  // --- DELETE PRODUCT HANDLER (DELETE /api/products/:id) ---
  http.delete("/api/products/:id", () => {
    // HTTP 204 No Content response
    return new HttpResponse(null, { status: 204 });
  }),
];