Skip to content

Commit

Permalink
fix(body): update mandatory fields body (#265)
Browse files Browse the repository at this point in the history
* fix(body): update mandatory fields body

* chore(test): fix test

* fix(test): fix tests
  • Loading branch information
WikiRik committed Mar 31, 2021
1 parent 0840d9f commit e80a5a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
12 changes: 6 additions & 6 deletions models/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ const Body = sequelize.define('body', {
},
email: {
type: Sequelize.STRING,
allowNull: true,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Email should be set.' }
notEmpty: { msg: 'Email should be set.' },
isEmail: { msg: 'Email should be valid.' }
}
},
phone: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
allowNull: true,
validate: {
notEmpty: { msg: 'Phone should be set.' },
}
},
address: {
type: Sequelize.TEXT,
allowNull: false,
defaultValue: '',
allowNull: true,
validate: {
notEmpty: { msg: 'Address should be set.' },
}
Expand Down
39 changes: 35 additions & 4 deletions test/unit/bodies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,42 @@ describe('Bodies testing', () => {
}
});

test('should work with email not set', async () => {
const data = generator.generateBody({ email: null });
const body = await Body.create(data);
test('should fail with null email', async () => {
try {
await generator.createBody({ email: null });
expect(1).toEqual(0);
} catch (err) {
expect(err).toHaveProperty('errors');
expect(err.errors.length).toEqual(1);
expect(err.errors[0].path).toEqual('email');
}
});

expect(body.email).toEqual(null);
test('should fail with invalid email', async () => {
try {
await generator.createBody({ email: 'test' });
expect(1).toEqual(0);
} catch (err) {
expect(err).toHaveProperty('errors');
expect(err.errors.length).toEqual(1);
expect(err.errors[0].path).toEqual('email');
}
});

test('should fail with not set email', async () => {
try {
const body = generator.generateBody();
delete body.email;

await Body.create(body);

expect(1).toEqual(0);
} catch (err) {
expect(err).toHaveProperty('errors');
expect(err.errors.length).toEqual(2);
expect(err.errors[0].path).toEqual('email');
expect(err.errors[1].path).toEqual('email');
}
});

test('should normalize fields', async () => {
Expand Down

0 comments on commit e80a5a7

Please sign in to comment.