Skip to content

Commit

Permalink
day five - a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
christyanbrayan committed Mar 31, 2020
1 parent 651ce4f commit ada6891
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backend/src/controllers/OngController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const crypto = require('crypto')
const generateUniqueId = require('../utils/generateUniqueId')
const connection = require('../database/connection')

module.exports = {
Expand All @@ -11,7 +11,7 @@ module.exports = {
async create(request, response) {
const { name, email, whatsapp, city, uf } = request.body

const id = crypto.randomBytes(4).toString('HEX')
const id = generateUniqueId()

await connection('ongs').insert({
id,
Expand All @@ -24,4 +24,4 @@ module.exports = {

return response.json({ id })
}
}
}
5 changes: 5 additions & 0 deletions backend/src/utils/generateUniqueId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const crypto = require('crypto')

module.exports = function generateUniqueId() {
return crypto.randomBytes(4).toString('HEX')
}
9 changes: 9 additions & 0 deletions backend/tests/unit/generateUniqueId.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const generateUniqueId = require('../../src/utils/generateUniqueId')

describe('Generate Unique ID', () => {
it('should generate an unique ID', () => {
const id = generateUniqueId()

expect(id).toHaveLength(8)
})
})

0 comments on commit ada6891

Please sign in to comment.