You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.0 KiB
TypeScript
90 lines
2.0 KiB
TypeScript
import supertest from "supertest";
|
|
import app from "../../server";
|
|
import { Product } from "../../models/product";
|
|
import { User } from "../../models/user";
|
|
import { Order, OrderProduct } from "../../models/order";
|
|
|
|
const token = process.env.TOKEN_SECRET_TEST as string;
|
|
const request = supertest(app);
|
|
|
|
const testProduct: Product = {
|
|
id: 3,
|
|
name: "metro",
|
|
price: 10,
|
|
};
|
|
|
|
const testUser: User = {
|
|
id: 3,
|
|
firstname: "John",
|
|
lastname: "Doe",
|
|
username: "Jd",
|
|
password: "password",
|
|
};
|
|
|
|
const testOrder: Order = {
|
|
status: "active",
|
|
user_id: 3,
|
|
};
|
|
|
|
const testOrderProduct: OrderProduct = {
|
|
id: 2,
|
|
quantity: 5,
|
|
order_id: 2,
|
|
product_id: 3,
|
|
};
|
|
|
|
describe("Order handler", () => {
|
|
beforeAll(async () => {
|
|
const product = await request
|
|
.post("/products")
|
|
.auth(token, { type: "bearer" })
|
|
.send(testProduct);
|
|
|
|
const user = await request
|
|
.post("/users")
|
|
.auth(token, { type: "bearer" })
|
|
.send(testUser);
|
|
});
|
|
|
|
it("Should create a new order", async () => {
|
|
const response = await request
|
|
.post("/orders")
|
|
.auth(token, { type: "bearer" })
|
|
.send(testOrder);
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it("Should index orders", async () => {
|
|
const response = await request
|
|
.get("/orders")
|
|
.auth(token, { type: "bearer" });
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it("Should get order by id", async () => {
|
|
const response = await request
|
|
.get("/orders/2")
|
|
.auth(token, { type: "bearer" });
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
it("Should add a new product to order", async () => {
|
|
const response = await request
|
|
.post("/orders/2/products")
|
|
.auth(token, { type: "bearer" })
|
|
.send(testOrderProduct);
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it("Should delete order", async () => {
|
|
const response = await request
|
|
.delete("/orders/2/products")
|
|
.auth(token, { type: "bearer" });
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|