167 lines
4.1 KiB
JavaScript
167 lines
4.1 KiB
JavaScript
|
/* eslint-disable jest/no-test-callback */
|
||
|
import { createBrands, createCar, truncate } from "./utils/common";
|
||
|
|
||
|
import models from "../models";
|
||
|
import Brands from "../rules/Brands";
|
||
|
import Middelware from "../index";
|
||
|
|
||
|
const createdCars = [];
|
||
|
let createdBrand = {};
|
||
|
|
||
|
describe("getOne", () => {
|
||
|
beforeAll(done => {
|
||
|
createBrands(4, (err, res) => {
|
||
|
if (err) {
|
||
|
done(err);
|
||
|
} else {
|
||
|
// eslint-disable-next-line prefer-destructuring
|
||
|
createdBrand = res[1];
|
||
|
createCar(createdBrand.id, true, 2004, (errCar, car) => {
|
||
|
if (errCar) {
|
||
|
done(errCar);
|
||
|
}
|
||
|
createdCars.push(car);
|
||
|
createCar(createdBrand.id, false, 2004, (errCar2, car2) => {
|
||
|
if (errCar2) {
|
||
|
done(errCar2);
|
||
|
}
|
||
|
createdCars.push(car2);
|
||
|
createCar(createdBrand.id, true, 2003, (errCar3, car3) => {
|
||
|
if (errCar3) {
|
||
|
done(errCar3);
|
||
|
}
|
||
|
createdCars.push(car3);
|
||
|
createCar(createdBrand.id, true, 1998, (errCar4, car4) => {
|
||
|
if (errCar4) {
|
||
|
done(errCar4);
|
||
|
}
|
||
|
createdCars.push(car4);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
afterAll(done => {
|
||
|
truncate(["Brands", "Cars"], done);
|
||
|
});
|
||
|
|
||
|
test("It should return one item with missing part", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "user"
|
||
|
},
|
||
|
params: {
|
||
|
brandId: createdBrand.id
|
||
|
},
|
||
|
query: {},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getOne(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
|
||
|
expect(res.id).toBe(createdBrand.id);
|
||
|
expect(res.name).toBe(createdBrand.name);
|
||
|
expect(res).not.toHaveProperty("Cars");
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return one item with all part", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {
|
||
|
brandId: createdBrand.id
|
||
|
},
|
||
|
query: {},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getOne(req, (err, res) => {
|
||
|
expect(err).toBeNull();
|
||
|
|
||
|
expect(res.id).toBe(createdBrand.id);
|
||
|
expect(res.name).toBe(createdBrand.name);
|
||
|
expect(res).toHaveProperty("Cars");
|
||
|
expect(res.Cars.length).toBe(4);
|
||
|
expect(res.Cars[0]).toHaveProperty("id");
|
||
|
expect(res.Cars[0]).toHaveProperty("name");
|
||
|
expect(res.Cars[0]).toHaveProperty("year");
|
||
|
expect(res.Cars[0]).toHaveProperty("created");
|
||
|
expect(res.Cars[0]).toHaveProperty("updated");
|
||
|
expect(res.Cars[0]).not.toHaveProperty("createdAt");
|
||
|
expect(res.Cars[0]).not.toHaveProperty("updatedAt");
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return errorCode 406.1 when missing itemId", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {},
|
||
|
query: {},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getOne(req, (err, res) => {
|
||
|
expect(parseFloat(err.errorCode)).toBe(406.1);
|
||
|
expect(res).toBeUndefined();
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
test("It should return empty result when item not found", async done => {
|
||
|
const middleware = new Middelware(Brands, models);
|
||
|
const req = {
|
||
|
method: "GET",
|
||
|
user: {
|
||
|
role: "admin"
|
||
|
},
|
||
|
params: {
|
||
|
brandId: createdBrand.id + 666
|
||
|
},
|
||
|
query: {},
|
||
|
protocol: "http",
|
||
|
get: () => {
|
||
|
return "internal.test/";
|
||
|
},
|
||
|
originalUrl: "v1/"
|
||
|
};
|
||
|
|
||
|
middleware.getOne(req, (err, res) => {
|
||
|
expect(parseFloat(err.errorCode)).toBe(404.0);
|
||
|
expect(res).toBeUndefined();
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|