Skip to content

Commit

Permalink
[CodeForPoznan#121] Virtual field [WiP]
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciek246 committed Feb 13, 2020
1 parent f6f8749 commit e5d3548
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/test/factories/schoolFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
Expand All @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/test/models/school_model.test.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
File renamed without changes.
23 changes: 0 additions & 23 deletions src/test/models/test_school_model.js

This file was deleted.

11 changes: 9 additions & 2 deletions src/test/truncate.js
Original file line number Diff line number Diff line change
@@ -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 });
}
};

0 comments on commit e5d3548

Please sign in to comment.