-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
67 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
describe("Library `mobx-model`", function() { | ||
require('./base_model'); | ||
describe('Library `mobx-model`', function() { | ||
require('./mobx_model'); | ||
}); |
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,5 @@ | ||
describe('MobxModel', () => { | ||
require('./relations'); | ||
require('./to_json'); | ||
require('./model_name_prop'); | ||
}); |
71 changes: 28 additions & 43 deletions
71
test/base_model/model_name_prop.js → test/mobx_model/model_name_prop.js
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 |
---|---|---|
@@ -1,145 +1,130 @@ | ||
import { expect } from 'chai'; | ||
import { isFunction } from 'lodash'; | ||
import { BaseModel } from '../../lib/index'; | ||
|
||
|
||
class AModel extends BaseModel { | ||
import MobxModel from '../../lib/index'; | ||
|
||
class AModel extends MobxModel { | ||
static modelName = 'AlphaModel'; | ||
|
||
static attributes = { | ||
value: null | ||
value: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'BettaModel', | ||
reverseRelation: true | ||
reverseRelation: true, | ||
}, | ||
{ | ||
type: 'hasMany', | ||
relatedModel: 'OmegaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
class BModel extends BaseModel { | ||
|
||
class BModel extends MobxModel { | ||
static modelName = 'BettaModel'; | ||
|
||
static attributes = { | ||
name: null | ||
name: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'AlphaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
class OModel extends BaseModel { | ||
|
||
class OModel extends MobxModel { | ||
static modelName = 'OmegaModel'; | ||
|
||
static attributes = { | ||
name: null | ||
name: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'AlphaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
const models = { AlphaModel: AModel, BettaModel: BModel, OmegaModel: OModel }; | ||
|
||
BaseModel.getModel = function(modelName) { | ||
MobxModel.getModel = function(modelName) { | ||
return models[modelName]; | ||
}; | ||
|
||
const topLevelJson = { | ||
|
||
model: { | ||
id: 1, | ||
omega_model_ids: [ | ||
11, | ||
12, | ||
13 // not existed | ||
13, // not existed | ||
], | ||
value: 'foo', | ||
betta_model: { id: 2, name: 'bar' }, | ||
}, | ||
|
||
omega_models: [ | ||
{ id: 11, name: 'Omega bar 11' }, | ||
{ id: 12, name: 'Omega bar 12' } | ||
] | ||
|
||
{ id: 12, name: 'Omega bar 12' }, | ||
], | ||
}; | ||
|
||
const modelJson = topLevelJson.model; | ||
const model = AModel.set({ modelJson, topLevelJson }); | ||
|
||
|
||
describe('Use property modelName first insead constructor.name', () => { | ||
|
||
it("should have different constructor.name and model name", function() { | ||
expect(model.constructor.name !== model.constructor.modelName).to.equal(true); | ||
it('should have different constructor.name and model name', function() { | ||
expect(model.constructor.name !== model.constructor.modelName).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
describe('urlRoot should calculated by `modelName`', () => { | ||
|
||
it("for model class", function() { | ||
it('for model class', function() { | ||
expect(AModel.urlRoot).to.equal('/alpha_models'); | ||
expect(model.constructor.urlRoot).to.equal('/alpha_models'); | ||
}); | ||
|
||
it("for model item", function() { | ||
it('for model item', function() { | ||
expect(model.urlRoot).to.equal('/alpha_models'); | ||
}); | ||
|
||
}); | ||
|
||
describe('hasOne', () => { | ||
|
||
it("should set `hasOne`-type related model data", function() { | ||
it('should set `hasOne`-type related model data', function() { | ||
expect(model.bettaModel.id).to.equal(2); | ||
expect(model.bettaModel.name).to.equal('bar'); | ||
}); | ||
|
||
it("should have reverse related model attribute", function() { | ||
it('should have reverse related model attribute', function() { | ||
expect(model.bettaModel.alphaModel.id).to.equal(1); | ||
expect(model.bettaModel.alphaModel.value).to.equal('foo'); | ||
}); | ||
|
||
}); | ||
|
||
describe('hasMany', () => { | ||
|
||
it("should set `hasMany`-type related models data", function() { | ||
it('should set `hasMany`-type related models data', function() { | ||
expect(!!model.omegaModels).to.equal(true); | ||
expect(model.omegaModels[0].id).to.equal(11); | ||
expect(model.omegaModels[0].name).to.equal('Omega bar 11'); | ||
expect(model.omegaModels[1].id).to.equal(12); | ||
expect(model.omegaModels[1].name).to.equal('Omega bar 12'); | ||
}); | ||
|
||
it("should have reverse related model attribute", function() { | ||
it('should have reverse related model attribute', function() { | ||
expect(model.omegaModels[0].alphaModel.id).to.equal(1); | ||
expect(model.omegaModels[0].alphaModel.value).to.equal('foo'); | ||
expect(model.omegaModels[1].alphaModel.id).to.equal(1); | ||
expect(model.omegaModels[1].alphaModel.value).to.equal('foo'); | ||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
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
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 |
---|---|---|
@@ -1,100 +1,92 @@ | ||
import { expect } from 'chai'; | ||
import { isFunction } from 'lodash'; | ||
import { BaseModel } from '../../lib/index'; | ||
import MobxModel from '../../lib/index'; | ||
|
||
|
||
class AlphaModel extends BaseModel { | ||
class AlphaModel extends MobxModel { | ||
static attributes = { | ||
value: null | ||
value: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'BettaModel', | ||
reverseRelation: true | ||
reverseRelation: true, | ||
}, | ||
{ | ||
type: 'hasMany', | ||
relatedModel: 'OmegaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
class BettaModel extends BaseModel { | ||
class BettaModel extends MobxModel { | ||
static attributes = { | ||
name: null | ||
name: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'AlphaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
class OmegaModel extends BaseModel { | ||
class OmegaModel extends MobxModel { | ||
static attributes = { | ||
name: null | ||
name: null, | ||
}; | ||
|
||
static relations = [ | ||
{ | ||
type: 'hasOne', | ||
relatedModel: 'AlphaModel', | ||
reverseRelation: true | ||
} | ||
reverseRelation: true, | ||
}, | ||
]; | ||
} | ||
|
||
const models = { AlphaModel, BettaModel, OmegaModel }; | ||
|
||
BaseModel.getModel = function(modelName) { | ||
MobxModel.getModel = function(modelName) { | ||
return models[modelName]; | ||
}; | ||
|
||
|
||
describe('toJSON()', () => { | ||
|
||
it("method should exist", function() { | ||
expect(isFunction(BaseModel.prototype.toJSON)).to.equal(true); | ||
it('method should exist', function() { | ||
expect(isFunction(MobxModel.prototype.toJSON)).to.equal(true); | ||
}); | ||
|
||
it("should serialize attributes and related models ID`s", function() { | ||
|
||
it('should serialize attributes and related models ID`s', function() { | ||
const topLevelJson = { | ||
|
||
model: { | ||
id: 1, | ||
omega_model_ids: [ | ||
11, | ||
12, | ||
13 // not existed | ||
13, // not existed | ||
], | ||
value: 'foo', | ||
betta_model: { id: 2, name: 'bar' }, | ||
}, | ||
|
||
omega_models: [ | ||
{ id: 11, name: 'Omega bar 11' }, | ||
{ id: 12, name: 'Omega bar 12' } | ||
] | ||
|
||
{ id: 12, name: 'Omega bar 12' }, | ||
], | ||
}; | ||
|
||
const modelJson = topLevelJson.model; | ||
const model = AlphaModel.set({ modelJson, topLevelJson }); | ||
|
||
expect(model.toJSON()).to.deep.equal({ | ||
id: 1, | ||
value: 'foo', | ||
bettaModelId: 2, | ||
omegaModelIds: [ 11, 12 ] | ||
}); | ||
|
||
expect(model.toJSON()).to.deep.equal({ | ||
id: 1, | ||
value: 'foo', | ||
bettaModelId: 2, | ||
omegaModelIds: [11, 12], | ||
}); | ||
}); | ||
|
||
}); |