Skip to content

Commit bd31819

Browse files
committed
Add initial core testing #3
Need to test "validate" core module. Also, need to test each validator.
1 parent c69137c commit bd31819

File tree

5 files changed

+380
-2
lines changed

5 files changed

+380
-2
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A simple data modeling tool for NodeJS",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "node ./node_modules/jasmine/bin/jasmine.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -27,5 +27,8 @@
2727
"bugs": {
2828
"url": "https://github.com/CodingCarlos/modelate/issues"
2929
},
30-
"homepage": "https://github.com/CodingCarlos/modelate#readme"
30+
"homepage": "https://github.com/CodingCarlos/modelate#readme",
31+
"devDependencies": {
32+
"jasmine": "^2.8.0"
33+
}
3134
}

spec/index.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var Modelate = require('../index');
2+
3+
const modelName = 'User';
4+
const modelExample = {
5+
property: {}
6+
};
7+
const dataExample = {
8+
property: 'hello'
9+
};
10+
11+
describe('Modulate exported', () => {
12+
it('shall be a function (a constructor)', () => {
13+
expect(typeof Modelate).toEqual('function');
14+
});
15+
16+
it('shall create objects', () => {
17+
const User = Modelate(modelName);
18+
expect(User).toBeDefined();
19+
});
20+
21+
it('objects shall have a valid modelName', () => {
22+
const User = Modelate(modelName);
23+
expect(User.modelName).toBeDefined();
24+
expect(User.modelName).toEqual(modelName);
25+
});
26+
27+
it('objects shall have a valid model getter', () => {
28+
const User = Modelate(modelName);
29+
expect(User.model).toBeDefined();
30+
expect(typeof User.model).toEqual('function');
31+
});
32+
33+
it('objects shall have a valid model setter', () => {
34+
const User = Modelate(modelName);
35+
expect(User.set).toBeDefined();
36+
expect(typeof User.set).toEqual('function');
37+
});
38+
39+
it('objects shall have a valid property to modelate', () => {
40+
const User = Modelate(modelName);
41+
expect(User.modelate).toBeDefined();
42+
expect(typeof User.modelate).toEqual('function');
43+
});
44+
45+
46+
describe('getter', () => {
47+
it('shall return an empty object when nothing set', () => {
48+
const User = Modelate(modelName);
49+
const model = User.model();
50+
expect(model).toBeDefined();
51+
expect(model).toEqual({});
52+
});
53+
});
54+
55+
describe('getter/setter', () => {
56+
it('shall return a valid model when setted', () => {
57+
const User = Modelate(modelName).set(modelExample);
58+
const model = User.model();
59+
expect(model).toBeDefined();
60+
expect(Object.assign({}, model)).toEqual(Object.assign({}, modelExample));
61+
});
62+
});
63+
64+
describe('modelate', () => {
65+
it('shall return an empty object if called with no data', () => {
66+
const User = Modelate(modelName).set({modelExample});
67+
const data = User.modelate();
68+
expect(data).toBeDefined();
69+
expect(Object.assign({}, data)).toEqual(Object.assign({}, {}));
70+
});
71+
72+
it('shall return an empty object if called without an object', () => {
73+
const User = Modelate(modelName).set({modelExample});
74+
const data = User.modelate(1);
75+
expect(data).toBeDefined();
76+
expect(Object.assign({}, data)).toEqual(Object.assign({}, {}));
77+
});
78+
79+
it('shall return the same object if no model set', () => {
80+
const User = Modelate(modelName).set(modelExample);
81+
const data = User.modelate(dataExample);
82+
expect(data).toBeDefined();
83+
expect(Object.assign({}, data)).toEqual(Object.assign({}, dataExample));
84+
});
85+
});
86+
});

spec/model.spec.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
const util = require('../lib/util');
2+
const model = require('../lib/model');
3+
4+
// Test data
5+
const modelName = 'Model';
6+
const modelExample = {
7+
property: {}
8+
};
9+
10+
// Start testing
11+
describe(' - Model', () => {
12+
it('shall be an object', () => {
13+
expect(typeof model).toEqual('object');
14+
});
15+
16+
it('shall have a "models" property', () => {
17+
expect(model.models).toBeDefined();
18+
});
19+
20+
it('shall have a "add" property', () => {
21+
expect(model.add).toBeDefined();
22+
});
23+
24+
it('shall have a "get" property', () => {
25+
expect(model.get).toBeDefined();
26+
});
27+
28+
describe('models', () => {
29+
it('shall be an object', () => {
30+
expect(typeof model.models).toEqual('object');
31+
});
32+
33+
/*it('shall be empty by default', () => {
34+
// This test does not works because of models set in other tests
35+
expect(model.models).toEqual({});
36+
});*/
37+
});
38+
39+
describe('getter', () => {
40+
it('shall be a function', () => {
41+
expect(typeof model.get).toEqual('function');
42+
});
43+
44+
describe('by param name', () => {
45+
it('shall return defined models', () => {
46+
model.models[modelName] = modelExample;
47+
48+
const result = model.get(modelName);
49+
50+
expect(result).toEqual(modelExample);
51+
delete model.models[modelName];
52+
});
53+
54+
it('shall return empty object on unsetted model name', () => {
55+
const result = model.get('undefined' + modelName);
56+
expect(result).toEqual({});
57+
});
58+
59+
it('shall return empty object on undefined model name', () => {
60+
const result = model.get();
61+
expect(result).toEqual({});
62+
});
63+
});
64+
65+
describe('by this.modelName', () => {
66+
it('shall return defined models', () => {
67+
// Set model
68+
model.models[modelName] = modelExample;
69+
// Prepare "this" context
70+
const self = {
71+
modelName: modelName,
72+
get: model.get
73+
}
74+
// Execute getter on "this" context
75+
const result = self.get();
76+
77+
expect(result).toEqual(modelExample);
78+
79+
// Reset models
80+
delete model.models[modelName];
81+
});
82+
83+
it('shall return empty object on unsetted model name', () => {
84+
// Prepare "this" context
85+
const self = {
86+
modelName: 'undefined' + modelName,
87+
get: model.get
88+
}
89+
// Execute getter on "this" context
90+
const result = self.get();
91+
92+
expect(result).toEqual({});
93+
});
94+
95+
it('shall return empty object on undefined model name', () => {
96+
// Prepare "this" context
97+
const self = {
98+
get: model.get
99+
}
100+
// Execute getter on "this" context
101+
const result = self.get();
102+
103+
expect(result).toEqual({});
104+
});
105+
});
106+
});
107+
108+
describe('adder', () => {
109+
it('shall be a function', () => {
110+
expect(typeof model.add).toEqual('function');
111+
});
112+
113+
describe('by param name', () => {
114+
it('shall add models', () => {
115+
model.add(modelExample, modelName);
116+
expect(model.models[modelName]).toEqual(modelExample);
117+
delete model.models[modelName];
118+
});
119+
120+
it('shall add nothing when no modelName', () => {
121+
const keys = Object.keys(model.models);
122+
model.add(modelExample);
123+
expect(Object.keys(model.models)).toEqual(keys);
124+
});
125+
126+
it('shall add nothing when no data', () => {
127+
const keys = Object.keys(model.models);
128+
model.add(undefined, modelName);
129+
expect(Object.keys(model.models)).toEqual(keys);
130+
});
131+
132+
it('shall add nothing when data is not an object', () => {
133+
const keys = Object.keys(model.models);
134+
model.add('undefined', modelName);
135+
expect(Object.keys(model.models)).toEqual(keys);
136+
});
137+
});
138+
139+
describe('by this.modelName', () => {
140+
it('shall add models', () => {
141+
// Prepare this context
142+
const self = {
143+
modelName: modelName,
144+
add: model.add
145+
};
146+
147+
const keys = Object.keys(model.models);
148+
self.add(modelExample);
149+
150+
expect(Object.keys(model.models).length).toEqual((keys.length + 1));
151+
expect(model.models[modelName]).toEqual(modelExample);
152+
153+
// Reset models
154+
delete model.models[modelName];
155+
});
156+
157+
it('shall add nothing when no modelName', () => {
158+
// Prepare this context
159+
const self = {
160+
add: model.add
161+
};
162+
163+
const keys = Object.keys(model.models);
164+
self.add(modelExample);
165+
166+
expect(Object.keys(model.models)).toEqual(keys);
167+
});
168+
169+
it('shall add nothing when no data', () => {
170+
// Prepare this context
171+
const self = {
172+
modelName: modelName,
173+
add: model.add
174+
};
175+
176+
const keys = Object.keys(model.models);
177+
self.add(undefined);
178+
179+
expect(Object.keys(model.models)).toEqual(keys);
180+
});
181+
182+
it('shall add nothing when data is not an object', () => {
183+
// Prepare this context
184+
const self = {
185+
modelName: modelName,
186+
add: model.add
187+
};
188+
189+
const keys = Object.keys(model.models);
190+
self.add('undefined');
191+
expect(Object.keys(model.models)).toEqual(keys);
192+
});
193+
});
194+
});
195+
});

spec/modelate.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const util = require('../lib/util');
2+
const models = require('../lib/model').models;
3+
const modelate = require('../lib/modelate');
4+
const Modelate = {
5+
// modelName: undefined,
6+
modelate: modelate
7+
};
8+
9+
// Test data
10+
const modelName = 'Test';
11+
const modelExample = {
12+
property: {}
13+
};
14+
const dataExample = {
15+
property: 'hello'
16+
};
17+
18+
// Model management
19+
const model = {
20+
add: function () {
21+
models[modelName] = modelExample;
22+
},
23+
rem: function () {
24+
delete models[modelName];
25+
}
26+
};
27+
28+
// Start testing
29+
describe(' - Modelate', () => {
30+
it('shall be a function (a constructor)', () => {
31+
expect(typeof modelate).toEqual('function');
32+
});
33+
34+
it('shall create objects', () => {
35+
const User = modelate();
36+
expect(User).toBeDefined();
37+
expect(typeof User).toEqual('object');
38+
});
39+
40+
describe('without params', () => {
41+
it('shall return an empty object if no modelName specified', () => {
42+
expect(Modelate.modelate(dataExample)).toEqual({});
43+
});
44+
45+
it('shall return an empty object if no modelName specified and no params', () => {
46+
expect(Modelate.modelate()).toEqual({});
47+
});
48+
49+
50+
it('shall return an empty object if modelName specified', () => {
51+
Modelate.modelName = modelName;
52+
expect(Modelate.modelate(dataExample)).toEqual({});
53+
delete Modelate.modelName;
54+
});
55+
56+
it('shall return an empty object if modelName specified and no params', () => {
57+
Modelate.modelName = modelName;
58+
expect(Modelate.modelate()).toEqual({});
59+
delete Modelate.modelName;
60+
});
61+
62+
describe('modelate data', () => {
63+
it('shall remove invalid properties', () => {
64+
// Prepare Modelate models
65+
Modelate.modelName = modelName;
66+
model.add(modelExample);
67+
68+
// Prepare data
69+
const data = util.clone(dataExample);
70+
data.invalidProp = 'shall be removed';
71+
72+
// Modelate response
73+
const response = Modelate.modelate(data);
74+
75+
expect(response).toEqual(dataExample);
76+
77+
// Reset Modelate
78+
delete Modelate.modelName;
79+
model.rem(modelExample);
80+
});
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)