-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include unit tests for custom convention
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ describe('Convetion', () => { | |
//then | ||
assert.deepStrictEqual(dbField, "field_name") | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |