Skip to content

Commit cfb00de

Browse files
committed
[chore] resolver tests
1 parent 78d798a commit cfb00de

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

lib/common/Model.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FabrixResolver } from './'
44
import { IllegalAccessError } from '../errors'
55
import { FabrixGeneric } from './Generic'
66

7+
78
/**
89
* Fabrix Model Class.
910
*/
@@ -64,12 +65,9 @@ export class FabrixModel extends FabrixGeneric {
6465
}
6566
this._app = app
6667

67-
if (!datastore) {
68-
this.app.log.warn(`${this.name} did not receive an instance of the datastore`)
69-
}
7068
this._datastore = datastore
7169

72-
this.resolver = new (<typeof FabrixModel>this.constructor).resolver(this)
70+
this.resolver = new (<typeof FabrixModel>this.constructor).resolver(this, datastore)
7371
this.app.emit(`model:${this.name}:constructed`, this)
7472
}
7573

@@ -78,11 +76,14 @@ export class FabrixModel extends FabrixGeneric {
7876
}
7977

8078
get datastore() {
79+
// if (!this._datastore) {
80+
// this.app.log.warn(`${this.name} did not receive an instance of the datastore`)
81+
// }
8182
return this._datastore
8283
}
8384

84-
set datastore(val) {
85-
this._datastore = val
85+
set datastore(datastore) {
86+
this._datastore = datastore
8687
}
8788

8889
/**
@@ -135,11 +136,11 @@ export class FabrixModel extends FabrixGeneric {
135136
*/
136137
get tableName (): string {
137138
const config = (<typeof FabrixModel>this.constructor).config(this.app, this.datastore)
138-
// const config = (<typeof FabrixModel>this.constructor).config || { tableName: null }
139139
return config.tableName || this.name
140140
}
141141

142142
/**
143+
* Bound Methods from the Resolver Class
143144
save (...args) {
144145
return this.resolver.save(...args)
145146
}

lib/common/Resolver.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import { FabrixModel } from './'
99
export class FabrixResolver {
1010
private _model: FabrixModel
1111

12-
/**
13-
* Model configuration
14-
*/
15-
static config () {
16-
}
17-
18-
constructor (model: FabrixModel) {
12+
constructor (model: FabrixModel, datastore?) {
1913
if (!model) {
2014
throw new RangeError('Resolver must be given a Model to bind to')
2115
}
@@ -37,29 +31,34 @@ export class FabrixResolver {
3731
return this.model.schema
3832
}
3933

34+
/**
35+
* Return the schema of the parent model
36+
*/
37+
get config (): FabrixModel['config'] {
38+
return this.model.config
39+
}
40+
4041
/**
4142
* Returns the instance of the parent model
4243
*/
4344
get app(): FabrixApp {
4445
return this.model.app
4546
}
4647

47-
/**
48-
public save(...args) {
49-
throw new Error('Orm for Save not defined')
50-
}
51-
52-
public update(...args) {
53-
throw new Error('Orm for Update not defined')
54-
}
55-
56-
public delete(...args) {
57-
throw new Error('Orm for Delete not defined')
58-
}
59-
60-
public get(...args) {
61-
throw new Error('Orm for Get not defined')
62-
}
63-
*/
48+
// public save(...args) {
49+
// throw new Error('Orm method for Save not defined')
50+
// }
51+
//
52+
// public update(...args) {
53+
// throw new Error('Orm method for Update not defined')
54+
// }
55+
//
56+
// public delete(...args) {
57+
// throw new Error('Orm method for Delete not defined')
58+
// }
59+
//
60+
// public get(...args) {
61+
// throw new Error('Orm method for Get not defined')
62+
// }
6463
}
6564

test/lib/Resolver.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const assert = require('assert')
24
const Model = require('../../dist/common').FabrixModel
35
const Resolver = require('../../dist/common').FabrixResolver
@@ -31,5 +33,22 @@ describe('lib/Resolver', () => {
3133
assert.throws(() => new Resolver(), RangeError)
3234
})
3335
})
36+
describe('Model Proxy', () => {
37+
it('should get a resolver method through model method', () => {
38+
const app = new Fabrix(testApp)
39+
const TestModel = class TestModel extends Model {
40+
static get resolver () {
41+
return class MyResolver extends Resolver {
42+
findFoo() {
43+
return 'foo'
44+
}
45+
}
46+
}
47+
}
48+
const initModel = new TestModel(app)
49+
assert.equal(initModel.resolver.findFoo(), 'foo')
50+
assert.throws(() => initModel.save(), Error)
51+
})
52+
})
3453
})
3554

0 commit comments

Comments
 (0)