Skip to content

Commit

Permalink
chore: add test to look into issue #53
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodconnolly committed Jul 27, 2021
1 parent c87675d commit 105d4f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions jest-tests/slug-generation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,34 @@ describe('slug generation', () => {
const user = await User.create(userData);
expect(user.slug).toBe(null);
});

it('should create a slug from a VIRTUAL field', async () => {
const uniqueTableId = `TestUser-${process.pid}-VIRTUAL`;
const User = global.sequelize.define(uniqueTableId, {
slug: {
type: global.DataTypes.STRING,
unique: true,
},
givenName: {
type: global.DataTypes.STRING,
allowNull: false,
},
_givenName: {
type: global.DataTypes.VIRTUAL,
get() {
return `VIRTUAL-${this.get('givenName')}`;
}
}
});
await global.sequelize.sync({force: true});
SequelizeSlugify.slugifyModel(User, {source: ['_givenName']});
const user = await User.create(userData);

// Unable to detect if VIRTUAL field has changed
// regenerateSlug manually :(
await user.regenerateSlug();

const expectedValue = `virtual-${userData.givenName.toLowerCase()}`;
expect(user.slug).toBe(expectedValue);
});
});
5 changes: 3 additions & 2 deletions jest/sequelize-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SequelizeEnvironment extends NodeEnvironment {
this.modelOptions = {};
this.modelCounter = 0;

if (this.dialect === 'sqlite') {
if (!this.dialect || this.dialect === 'sqlite') {
this.sequelize = new Sequelize({
dialect: 'sqlite',
storage: ':memory:',
Expand Down Expand Up @@ -74,6 +74,7 @@ class SequelizeEnvironment extends NodeEnvironment {

faker.seed(42);
this.global.sequelize = this.sequelize;
this.global.DataTypes = Sequelize.DataTypes;
this.global.generateModel = this.generateModel.bind(this);
this.global.createUser = this.createUser;
this.global.generateGivenName = () => {
Expand Down Expand Up @@ -135,7 +136,7 @@ class SequelizeEnvironment extends NodeEnvironment {
givenName: faker.name.firstName(),
familyName: faker.name.lastName(),
nickName: faker.lorem.word(),
age: faker.random.number()
age: faker.datatype.number()
};
}
}
Expand Down

0 comments on commit 105d4f8

Please sign in to comment.