Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Staltec committed Aug 9, 2019
1 parent a04334b commit d5c8b8c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 119 deletions.
9 changes: 0 additions & 9 deletions test/base_model/index.js

This file was deleted.

24 changes: 0 additions & 24 deletions test/base_model/restful_actions.js

This file was deleted.

5 changes: 2 additions & 3 deletions test/index.js
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');
});
5 changes: 5 additions & 0 deletions test/mobx_model/index.js
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');
});
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');
});

});


});
10 changes: 5 additions & 5 deletions test/base_model/relations.js → test/mobx_model/relations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { BaseModel } from '../../lib/index';
import MobxModel from '../../lib/index';

class AlphaModel extends BaseModel {
class AlphaModel extends MobxModel {
static attributes = {
value: null,
};
Expand All @@ -20,7 +20,7 @@ class AlphaModel extends BaseModel {
];
}

class BettaModel extends BaseModel {
class BettaModel extends MobxModel {
static attributes = {
name: null,
};
Expand All @@ -34,7 +34,7 @@ class BettaModel extends BaseModel {
];
}

class OmegaModel extends BaseModel {
class OmegaModel extends MobxModel {
static attributes = {
name: null,
};
Expand All @@ -50,7 +50,7 @@ class OmegaModel extends BaseModel {

const models = { AlphaModel, BettaModel, OmegaModel };

BaseModel.getModel = function(modelName) {
MobxModel.getModel = function(modelName) {
return models[modelName];
};

Expand Down
62 changes: 27 additions & 35 deletions test/base_model/to_json.js → test/mobx_model/to_json.js
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],
});
});

});

0 comments on commit d5c8b8c

Please sign in to comment.