text: process.env.NODE_ENV = "test";
  
  const chai = require("chai");
  const expect = require("chai").expect;
  const request = require("supertest");
  
  const Server = require("../src/server");
  const mockData = require("../test-mock-data");
  
  describe("TESTING /api/chart", () => {
      const server = new Server();
      let adminToken = null;
      let customerToken = null;
  
      before((done) => {
          server
              .start()
              .then(() => done())
              .catch((err) => done("An error occurred when starting the server"));
      });
  
      // LOGIN BEFORE EVERY TEST
      beforeEach(async () => {
          try {
              let response = await request(server.app).post("/auth/login/test").send({ username: "admin", password: "root" });
              expect(response.status).to.satisfy((status) => status == 200 || status == 302);
              expect(response).to.contain.property("text");
  
              let responseJson = JSON.parse(response.text);
              expect(responseJson).to.be.an("object");
              expect(responseJson).to.contain.property("data");
              expect(responseJson.data).to.contain.property("user");
              expect(responseJson.data).to.contain.property("accessToken");
              expect(responseJson.data).to.contain.property("refreshToken");
  
              adminToken = responseJson.data;
  
              response = await request(server.app).post("/auth/login/test").send({ username: "customer1", password: "root" });
              expect(response.status).to.satisfy((status) => status == 200 || status == 302);
              expect(response).to.contain.property("text");
  
              responseJson = JSON.parse(response.text);
              expect(responseJson).to.be.an("object");
              expect(responseJson).to.contain.property("data");
              expect(responseJson.data).to.contain.property("user");
              expect(responseJson.data).to.contain.property("accessToken");
              expect(responseJson.data).to.contain.property("refreshToken");
  
              customerToken = responseJson.data;
          } catch (err) {
              console.log(err.message);
          }
      });
  
      const items = [
          {
              product: {
                  _id: mockData.products[0]._id.toString(),
                  name: mockData.products[0].name,
                  price: mockData.products[0].price,
              },
              qty: 4,
          },
          {
              product: {
                  _id: mockData.products[1]._id.toString(),
                  name: mockData.products[1].name,
                  price: mockData.products[1].price,
              },
              qty: 2,
          },
          {
              product: {
                  _id: mockData.products[2]._id.toString(),
                  name: mockData.products[2].name,
                  price: mockData.products[2].price,
              },
              qty: 1,
          },
      ];
  
      describe(`CHART TEST`, () => {
          it("ADD PRODUCT", async () => {
              const response = await request(server.app)
                  .put("/api/chart/add-product")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` })
                  .send({
                      product: items[0].product._id,
                  });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(1);
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.eql(items[0].product._id);
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(1);
          });
  
          it("GET CHART BY USER ID", async () => {
              const response = await request(server.app)
                  .get("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(1);
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.be.an("object");
              expect(chart.products[0].product).to.have.property("_id");
              expect(chart.products[0].product._id).to.eql(items[0].product._id);
              expect(chart.products[0].product).to.have.property("name");
              expect(chart.products[0].product).to.have.property("price");
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(1);
          });
  
          it("UPDATE CHART", async () => {
              const response = await request(server.app)
                  .put("/api/chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` })
                  .send({
                      products: [
                          {
                              product: items[0].product._id,
                              qty: items[0].qty,
                          },
                          {
                              product: items[1].product._id,
                              qty: items[1].qty,
                          },
                      ],
                  });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(2);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.eql(items[0].product._id);
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.eql(items[1].product._id);
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
          });
  
          it("GET CHART BY USER ID", async () => {
              const response = await request(server.app)
                  .get("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(2);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.be.an("object");
              expect(chart.products[0].product).to.have.property("_id");
              expect(chart.products[0].product._id).to.eql(items[0].product._id);
              expect(chart.products[0].product).to.have.property("name");
              expect(chart.products[0].product).to.have.property("price");
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.be.an("object");
              expect(chart.products[1].product).to.have.property("_id");
              expect(chart.products[1].product._id).to.eql(items[1].product._id);
              expect(chart.products[1].product).to.have.property("name");
              expect(chart.products[1].product).to.have.property("price");
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
          });
  
          it("ADD PRODUCT", async () => {
              const response = await request(server.app)
                  .put("/api/chart/add-product")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` })
                  .send({
                      product: items[2].product._id,
                      qty: items[2].qty,
                  });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(3);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.eql(items[0].product._id);
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.eql(items[1].product._id);
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
  
              expect(chart.products[2]).to.have.property("product");
              expect(chart.products[2].product).to.eql(items[2].product._id);
              expect(chart.products[2]).to.have.property("qty");
              expect(chart.products[2].qty).to.eql(items[2].qty);
          });
  
          it("GET CHART BY USER ID", async () => {
              const response = await request(server.app)
                  .get("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(3);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.be.an("object");
              expect(chart.products[0].product).to.have.property("_id");
              expect(chart.products[0].product._id).to.eql(items[0].product._id);
              expect(chart.products[0].product).to.have.property("name");
              expect(chart.products[0].product).to.have.property("price");
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.be.an("object");
              expect(chart.products[1].product).to.have.property("_id");
              expect(chart.products[1].product._id).to.eql(items[1].product._id);
              expect(chart.products[1].product).to.have.property("name");
              expect(chart.products[1].product).to.have.property("price");
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
  
              expect(chart.products[2]).to.have.property("product");
              expect(chart.products[2].product).to.be.an("object");
              expect(chart.products[2].product).to.have.property("_id");
              expect(chart.products[2].product._id).to.eql(items[2].product._id);
              expect(chart.products[2].product).to.have.property("name");
              expect(chart.products[2].product).to.have.property("price");
              expect(chart.products[2]).to.have.property("qty");
              expect(chart.products[2].qty).to.eql(items[2].qty);
          });
  
          it("REMOVE PRODUCT", async () => {
              const response = await request(server.app)
                  .put("/api/chart/remove-product")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` })
                  .send({
                      product: items[2].product._id,
                  });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(2);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.eql(items[0].product._id);
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.eql(items[1].product._id);
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
          });
  
          it("GET CHART BY USER ID", async () => {
              const response = await request(server.app)
                  .get("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const chart = body.data;
              expect(chart).to.be.an("object");
              expect(chart).to.have.property("_id");
              expect(chart).to.have.property("user");
              expect(chart).to.have.property("products");
              expect(chart.products).to.be.an("array");
              expect(chart.products.length).to.eql(2);
  
              expect(chart.products[0]).to.have.property("product");
              expect(chart.products[0].product).to.be.an("object");
              expect(chart.products[0].product).to.have.property("_id");
              expect(chart.products[0].product._id).to.eql(items[0].product._id);
              expect(chart.products[0].product).to.have.property("name");
              expect(chart.products[0].product).to.have.property("price");
              expect(chart.products[0]).to.have.property("qty");
              expect(chart.products[0].qty).to.eql(items[0].qty);
  
              expect(chart.products[1]).to.have.property("product");
              expect(chart.products[1].product).to.be.an("object");
              expect(chart.products[1].product).to.have.property("_id");
              expect(chart.products[1].product._id).to.eql(items[1].product._id);
              expect(chart.products[1].product).to.have.property("name");
              expect(chart.products[1].product).to.have.property("price");
              expect(chart.products[1]).to.have.property("qty");
              expect(chart.products[1].qty).to.eql(items[1].qty);
          });
  
          it("DELETE CHART BY USER ID", async () => {
              const response = await request(server.app)
                  .delete("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(200);
              expect(response).to.contain.property("text");
  
              const body = JSON.parse(response.text);
              expect(body).to.be.an("object");
              expect(body).to.contain.property("data");
  
              const deletedChartUserId = body.data;
              expect(deletedChartUserId).to.be.an("string");
              expect(deletedChartUserId).to.eql(customerToken.user._id);
          });
  
          it("GET CHART BY USER ID [NOT FOUND]", async () => {
              const response = await request(server.app)
                  .get("/api/chart/user/my-chart")
                  .set({ Authorization: `Bearer ${customerToken.accessToken}` });
              expect(response.status).to.equal(404);
          });
      });
  });
  