/* eslint-disable jest/no-test-callback */ import uuid from "uuid/v4"; import { truncate } from "./utils/common"; import models from "../models"; import Cars from "../rules/Cars"; import Middelware from "../index"; const { Op } = models.Sequelize; let colors = []; const colorsId = []; let car = {}; describe("createOne", () => { beforeAll(done => { models.Colors.findAll({ where: { [Op.or]: [ { name: "White" }, { name: "Miami blue" }, { name: "Sunflower yellow" } ] } }) .then(items => { colors = items; colorsId.push(items[0].id); colorsId.push(items[1].id); done(); }) .catch(done); }); afterAll(done => { truncate(["Cars"], done); }); test("It should return new item after creation", async done => { const middleware = new Middelware(Cars, models); const req = { method: "POST", user: { role: "admin" }, body: { name: uuid(), year: 2004, active: true, colorsId } }; middleware.createOne(req, (err, res) => { expect(err).toBeNull(); expect(res.name).toBe(req.body.name); expect(res.year).toBe(2004); expect(res.active).toBe(true); car = res; done(); }); }); test("It should return item with relations", async done => { const middleware = new Middelware(Cars, models); const req = { method: "POST", user: { role: "admin" }, params: { carId: car.id } }; middleware.getOne(req, (err, res) => { expect(err).toBeNull(); expect(res.name).toBe(car.name); expect(res.Colors.length).toBe(2); expect(res.year).toBe(2004); expect(res.Colors[0].id).toBe(colorsId[0]); expect(res.Colors[1].id).toBe(colorsId[1]); done(); }); }); test("It should return all items ", async done => { const middleware = new Middelware(Cars, models); const req = { method: "POST", user: { role: "admin" }, params: {}, query: {}, protocol: "http", get: () => { return "internal.test/"; }, originalUrl: "v1/" }; middleware.getAll(req, (err, res) => { expect(err).toBeNull(); expect(res.data[0].name).toBe(car.name); expect(res.data[0].Colors.length).toBe(2); expect(res.data[0].Colors[0].id).toBe(colorsId[0]); expect(res.data[0].Colors[1].id).toBe(colorsId[1]); done(); }); }); test("It should return ok when update item", async done => { const middleware = new Middelware(Cars, models); const req = { method: "PATCH", user: { role: "admin" }, params: { carId: car.id }, query: {}, body: { year: 1998, colorsId: [colors[2].id] }, protocol: "http", get: () => { return "internal.test/"; }, originalUrl: "v1/" }; middleware.patchOne(req, (err, res) => { expect(err).toBeNull(); expect(res.name).toBe(car.name); expect(res.year).toBe(1998); done(); }); }); test("It should return updated item", async done => { const middleware = new Middelware(Cars, models); const req = { method: "PATCH", user: { role: "admin" }, params: { carId: car.id }, query: {}, body: {}, protocol: "http", get: () => { return "internal.test/"; }, originalUrl: "v1/" }; middleware.getOne(req, (err, res) => { expect(err).toBeNull(); expect(res.name).toBe(car.name); expect(res.year).toBe(1998); expect(res.Colors.length).toBe(3); done(); }); }); test("It should return empty object after delete", async done => { const middleware = new Middelware(Cars, models); const req = { method: "DELETE", user: { role: "admin" }, params: { carId: car.id }, query: {}, body: {}, protocol: "http", get: () => { return "internal.test/"; }, originalUrl: "v1/" }; middleware.deleteOne(req, (err, res) => { expect(err).toBeNull(); expect(res).toEqual({}); models.CarsColors.findAll({ where: { CarId: car.id } }) .then(carsColors => { expect(carsColors.length).toBe(0); models.Cars.findAll({ where: { id: car.id } }).then(items => { expect(items.length).toBe(0); done(); }); }) .catch(done); }); }); test("It should return empty item when delete item with empty belongsTo collection", async done => { const middleware = new Middelware(Cars, models); const req = { method: "POST", user: { role: "admin" }, body: { name: uuid(), year: 2004, active: true } }; middleware.createOne(req, (err, newCar) => { expect(err).toBeNull(); expect(newCar.name).toBe(req.body.name); expect(newCar.year).toBe(2004); expect(newCar.active).toBe(true); const reqItem = { method: "PATCH", user: { role: "admin" }, params: { carId: newCar.id }, query: {}, body: { year: 1998 }, protocol: "http", get: () => { return "internal.test/"; }, originalUrl: "v1/" }; middleware.patchOne(reqItem, errPatch => { expect(errPatch).toBeNull(); reqItem.method = "DELETE"; middleware.deleteOne(reqItem, errDelete => { expect(errDelete).toBeNull(); done(); }); }); }); }); });