Skip to content

Commit

Permalink
feat: include unit tests for custom convention
Browse files Browse the repository at this point in the history
  • Loading branch information
maikvortx committed Jan 3, 2022
1 parent 6ea3118 commit 2532ebb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit/convention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ describe('Convetion', () => {
//then
assert.deepStrictEqual(dbField, "field_name")
})

})
88 changes: 88 additions & 0 deletions test/unit/queries/customConvention.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const assert = require('assert')

describe('Custom Convention', () => {
context('Find all with custom convention', () => {
const retFromDeb = [
{ id: 1, string_test: 'john', boolean_test: true },
{ id: 2, string_test: 'clare', boolean_test: false }
]

const givenAnEntity = () => {
const ParentEntity = entity('A Parent Entity', {})

return entity('A entity', {
id: field(Number),
stringTest: field(String),
booleanTest: field(Boolean),
entityTest: field(ParentEntity),
entitiesTest: field([ParentEntity])
})
}

const givenAnRepositoryClass = () => {
return class ItemRepositoryBase extends Repository {
constructor (options) {
super(options)
}
}
}

const knex = ret => () => ({
select: () => {
return ret
}
})

it('should return entities using custom conventions', async () => {
//given
let spy = []
const anEntity = givenAnEntity()
const ItemRepository = givenAnRepositoryClass()
const itemRepo = new ItemRepository({
entity: anEntity,
table: 'aTable',
ids: ['id'],
knex: knex(retFromDeb),
convention: {
toTableFieldName: fieldName => spy.push(`${fieldName}_custom`)
}
})

//when
const ret = await itemRepo.find()

//then
assert.strictEqual(ret.length, 2)
assert.deepEqual(spy, [
'id_custom',
'stringTest_custom',
'booleanTest_custom',
'entityTest_custom',
'entitiesTest_custom'
])
})

it('should throw a error when convention failed', async () => {
//given
const anEntity = givenAnEntity()
const ItemRepository = givenAnRepositoryClass()

//when & then
assert.throws(() => {
new ItemRepository({
entity: anEntity,
table: 'aTable',
ids: ['id'],
knex: knex(retFromDeb),
convention: {
toTableFieldName: () => {
throw new Error('Custom convention failed')
}
}
})
})
})
})
})

0 comments on commit 2532ebb

Please sign in to comment.