Explorar el Código

Add underscored value field to category

sbkwgh hace 8 años
padre
commit
1b2abb1b94
Se han modificado 2 ficheros con 20 adiciones y 0 borrados
  1. 9 0
      models/category.js
  2. 11 0
      test/category.js

+ 9 - 0
models/category.js

@@ -1,6 +1,15 @@
 module.exports = (sequelize, DataTypes) => {
 	let Category = sequelize.define('Category', {
 		name: {
+			type: DataTypes.STRING,
+			unique: true,
+			set (val) {
+				let underscored = val.trim().replace(/\s/g, '_').toUpperCase()
+				this.setDataValue('name', val)
+				this.setDataValue('value', underscored)
+			}
+		},
+		value: {
 			type: DataTypes.STRING,
 			unique: true
 		}

+ 11 - 0
test/category.js

@@ -55,6 +55,17 @@ describe('Category', () => {
 			res.should.have.status(200)
 			res.body.should.have.property('name', 'category')
 		})
+		it('should have an "underscored" value field', async () => {
+			let res = await agent
+				.post('/api/v1/category')
+				.set('content-type', 'application/json')
+				.send({ name: ' 	another category here 	' })
+
+			res.should.be.json
+			res.should.have.status(200)
+			res.body.should.have.property('name', ' 	another category here 	')
+			res.body.should.have.property('value', 'ANOTHER_CATEGORY_HERE')
+		})
 		it('should return an error if category already exists', async () => {
 			try {
 				let res = await agent