From e5d3548f4f89c4395870bfcd40c6b11287bd1b9f Mon Sep 17 00:00:00 2001 From: Maciej Matuszczak Date: Thu, 13 Feb 2020 18:01:34 +0100 Subject: [PATCH] [#121] Virtual field [WiP] --- src/test/factories/schoolFactory.js | 8 ++--- src/test/models/school_model.test.js | 36 +++++++++++++++++++ ...ype_model.js => school_type_model.test.js} | 0 src/test/models/test_school_model.js | 23 ------------ src/test/truncate.js | 11 ++++-- 5 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 src/test/models/school_model.test.js rename src/test/models/{test_school_type_model.js => school_type_model.test.js} (100%) delete mode 100644 src/test/models/test_school_model.js diff --git a/src/test/factories/schoolFactory.js b/src/test/factories/schoolFactory.js index 1be931e..996e701 100644 --- a/src/test/factories/schoolFactory.js +++ b/src/test/factories/schoolFactory.js @@ -2,8 +2,8 @@ const { School } = require("../../db/models"); const schoolTypeList = require("../../db/school_types"); const faker = require("faker/locale/pl"); -const schoolData = () => { - const schoolType = faker.random.arrayElement(schoolTypeList); +const schoolData = type => { + const schoolType = type || faker.random.arrayElement(schoolTypeList); return { type: schoolType, name: `${schoolType} nr. ${faker.random.number(200)}`, @@ -14,8 +14,8 @@ const schoolData = () => { }; const schoolFixture = async (props = {}) => { - const schoolData = schoolData(); - return await School.create({ schoolData, ...props }); + const data = schoolData(props.type); + return await School.create({ ...data, ...props }); }; module.exports = schoolFixture; diff --git a/src/test/models/school_model.test.js b/src/test/models/school_model.test.js new file mode 100644 index 0000000..5b4802d --- /dev/null +++ b/src/test/models/school_model.test.js @@ -0,0 +1,36 @@ +const { sequelize } = require("../../db/db_config"); +const schoolFactory = require("../factories/schoolFactory"); +const {schoolData} = require("../factories/schoolFactory"); +const schoolTypeFactory = require("../factories/schoolTypeFactory"); +const truncate = require("../truncate"); +const { School, SchoolType } = require("../../db/models"); + +describe("School model", () => { + + beforeEach(async () => { + await truncate([School, SchoolType]); + await schoolTypeFactory("przedszkole") + }); + + afterEach(async () => { + await truncate([School, SchoolType]); + }); + + it("should be created", async () => { + const school = await sequelize + .sync({ force: true }) + .then(() => schoolFactory({ name: "TEST", type: "przedszkole" })); + + expect(school).toBeTruthy(); + expect(school.dataValues.name).toBe("TEST"); + }); + + it("should return concatenated data by calling `address`", async () => { + const school = await sequelize + .sync({ force: true }) + .then(() => schoolFactory({ street: "TEST STREET", type: "przedszkole" })); + + expect(school).toBeTruthy(); + expect(school.dataValues.address).toBe("TEST STREET"); + }); +}); diff --git a/src/test/models/test_school_type_model.js b/src/test/models/school_type_model.test.js similarity index 100% rename from src/test/models/test_school_type_model.js rename to src/test/models/school_type_model.test.js diff --git a/src/test/models/test_school_model.js b/src/test/models/test_school_model.js deleted file mode 100644 index 723287d..0000000 --- a/src/test/models/test_school_model.js +++ /dev/null @@ -1,23 +0,0 @@ -const { sequelize } = require("../../db/db_config"); -const schoolFactory = require("../factories/schoolFactory"); -const truncate = require("../truncate"); -const { School } = require("../../db/models"); - -describe("School model", () => { - beforeEach(async () => { - await truncate(School); - }); - - afterEach(async () => { - await truncate(School); - }); - - it("should be created", async () => { - const school = await sequelize - .sync({ force: true }) - .then(() => schoolFactory({ name: "TEST" })); - - expect(school).toBeTruthy(); - expect(school.dataValues.name).toBe("TEST"); - }); -}); diff --git a/src/test/truncate.js b/src/test/truncate.js index 0c6723c..3dd1f93 100644 --- a/src/test/truncate.js +++ b/src/test/truncate.js @@ -1,3 +1,10 @@ -module.exports = async function truncate(model) { - model.destroy({ where: {}, force: true }); +module.exports = async function truncate(models) { + if (models instanceof Array){ + models.map( + model => model.destroy({ where: {}, force: true }) + ); + } + else { + models.destroy({ where: {}, force: true }); + } };