All files / test/mocks handlers.js

85.18% Statements 23/27
85% Branches 17/20
66.66% Functions 2/3
88.46% Lines 23/26

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117    20x     9x 9x   9x 4x                       5x 2x                                 3x                       3x 2x                     1x 1x               4x 4x 4x   4x 1x               3x 1x           2x 1x               1x 1x                            
import { http, HttpResponse } from "msw";
 
export const handlers = [
  // GET requests via proxy.php
  http.get("/proxy.php", ({ request }) => {
    const url = new URL(request.url);
    const path = url.searchParams.get("path");
 
    if (path === "/api/v1/areas/list") {
      return HttpResponse.json({
        status: true,
        data: {
          areas: [
            { id: 1, type: "room", alias: "kitchen", display_name: "Kitchen", parent_id: 0 },
            { id: 2, type: "room", alias: "hall", display_name: "Hall", parent_id: 0 },
          ],
          total: 2,
        },
      });
    }
 
    if (path === "/api/v1/devices/scanning/setup") {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            {
              device_name: "New Device",
              device_type: "relay",
              ip_address: "192.168.1.50",
              mac_address: "A4:CF:12:9B:3F:D2",
              firmware_version: "1.0",
              status: "setup",
            },
          ],
        },
      });
    }
 
    Iif (path === "/api/v1/devices/list") {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            { id: 1, name: "Relay 1", alias: "relay_1", device_type: "relay", device_ip: "192.168.1.10", connection_status: "active" },
          ],
          total: 1,
        },
      });
    }
 
    if (path === "/api/v1/scripts/actions/list") {
      return HttpResponse.json({
        status: true,
        data: {
          scripts: [
            { alias: "kitchen_light", name: "Kitchen Light", icon: '<i class="ph ph-lightbulb"></i>', state: "enabled", author: "Test" },
          ],
          total: 1,
        },
      });
    }
 
    Eif (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/remove")) {
      return HttpResponse.json({ status: true });
    }
 
    return new HttpResponse(null, { status: 404 });
  }),
 
  // POST requests via proxy.php
  http.post("/proxy.php", async ({ request }) => {
    const url = new URL(request.url);
    const path = url.searchParams.get("path");
    const body = await request.json().catch(() => ({}));
 
    if (path === "/api/v1/areas/new-area") {
      return HttpResponse.json({
        status: true,
        data: {
          area: { id: 3, type: body.type, alias: body.alias, display_name: body.display_name, parent_id: 0 },
        },
      });
    }
 
    if (path === "/api/v1/areas/update-display-name") {
      return HttpResponse.json({
        status: true,
        data: { area_id: body.area_id, display_name: body.display_name },
      });
    }
 
    if (path === "/api/v1/devices/setup/new-device") {
      return HttpResponse.json({
        status: true,
        data: {
          device: { id: 2, name: body.name, alias: body.alias, device_type: "relay", device_ip: body.device_ip },
        },
      });
    }
 
    Eif (path === "/api/v1/scripts/actions/run") {
      return HttpResponse.json({
        status: true,
        data: {
          return: {
            result: { ok: true },
            exec_time: "0.042 seconds",
          },
        },
      });
    }
 
    return new HttpResponse(null, { status: 404 });
  }),
];