In opening I would like to say; I'm pretty new for programing. I'm working with JS for a year. Using Sencha's ExtJS and usign Bryntum's Siesta test tool for unit and integration(ui) tests.
Right now developing an application through Mastering Ext JS book. I've started to write tests only for a couple months. First of all read a lot about those topics;
- What is test & types?
- What is test design concepts: tdd-bdd?
- What is proper tools for test suite: test-runner, framework etc.?
well.. I keep going well and just finished the first portion of ui-test for my app =)and now sailing to unit-tests. The thing is still i'm not sure what to test/how to test for unit-test! So I would like to share a couple of code snippets and asking for your advice.
The project which code snippet will be sharing is build with ExtJS 6.5;
a Globals.js
file has been defined which aims to working through whole app.
Globals.js
Ext.define('MyApp.Globals', {
singleton: true,
...
config: {
webFont: 'FontAwesome',
add: 'xf067',
edit: 'xf040',
delete: 'xf1f8',
save: 'xf00c',
cancel: 'xf0e2'
},
STATUS_FUTURE: 'A',
STATUS_EXPECTED: 'E',
STATUS_CANCEL: 'X',
status: {
'A': "Future",
'E': "Expected",
'X': "Cancelled",
},
constructor: function (config) {
this.initConfig(config);
},
getStatusDesc: function (status) { // First method!
var me = this;
if (me.isEmptyStr(status)) {
status = me.STATUS_FUTURE;
}
return me.status[status];
},
getGlyph: function (glyph) { // Second method!
var me = this;
var font = me.getWebFont();
if (typeof me.config[glyph] === 'undefined') {
return false;
}
return me.config[glyph] + '@' + font;
},
isEmptyStr: function (aStr) { // Third method!
return Ext.isEmpty(aStr);
},
.....
and some unit-test sample I've wrote till now;
describe('MyApp Globals \'typeof\' & \'return\' unit-test', function(t) {
t.ok(MyApp.Globals, 'Found Globals.js');
t.describe('getStatusDesc() method', function(t) {
t.it('should be a function', function(t) {
t.expect(typeof MyApp.Globals.getStatusDesc).toEqual('function');
});
t.it('should return a string', function(t) {
t.expect(typeof MyApp.Globals.getStatusDesc()).toEqual('string');
});
});
t.describe('getGlyph() method', function(t) {
t.it('should be a function', function(t) {
t.expect(typeof MyApp.Globals.getGlyph).toEqual('function');
});
t.it('should return a boolean', function(t) {
t.expect(typeof MyApp.Globals.getGlyph()).toEqual('boolean'); //I've expected it will return as string but didn't not! and passed as green =|
});
});
t.describe('isEmptyStr() method', function(t) {
t.it('should be a function', function(t) {
t.expect(typeof MyApp.Globals.isEmptyStr).toEqual('function');
});
t.it('should return a boolean', function(t) {
t.expect(typeof MyApp.Globals.isEmptyStr()).toEqual('boolean');
});
});
as you see; there has been three method defined (getStatusDesc
, getGlyph
, isEmptyStr
) and I would like to walk through one-by-one to show you where/which methods has been effected with those three method.
1. getStatusDesc()
has usage in Grid.js
;
renderStatusCol: function (value, meta, record) {
... //Some code on here
return ... + MyApp.Globals.getStatusDesc(value) + ...;
},
2. getGlyph()
has usage in Data.js
;
getEditForm: function () {
return [
{
xtype: 'statusedits',
glyph: MyApp.Globals.getGlyph('edit')
}
]
},
3. isEmptyStr()
has usage in loginAuth.js
(checks if any string
type data returns within json);
tokenValidate: function () {
var me = this;
return !MyApp.Globals.isEmptyStr(me.tokenRead()) && !MyApp.Globals.dateIsPassed(me.tokenExpire());
},
So here will be my question;
- What I should to do more for unit-test through those sample methods above?
- Should i looking only for
return
as data-type or as expected value? (I mean for example getGlyph method returns asboolean
type but aims to check glyph's shortcode) - How i should planing for a robust unit-test structure/design?
Could you give ideas within sample code snippets. Thanks a lot!
UPDATE
New unit test snippet;
describe('MyApp Globals unit-test', function(t) {
t.ok(MyApp.Globals, 'Found Globals.js');
t.describe('getStatusDesc() method', function(t) {
t.it('should return \'A\' as status description', function(t) {
t.expect(MyApp.Globals.getStatusDesc('A')).toEqual('Future'); //Result: DONE! Expect "Future" to be equal to "Future"
});
});
t.describe('getGlyph() method', function(t) {
t.it('should return \'add\' ass webfont-code', function(t) {
t.expect(MyApp.Globals.getGlyph('add')).toEqual('xf067@FontAwesome'); //Result: DONE! Expect "xf067@FontAwesome" to be equal to "xf067@FontAwesome"
});
});
t.describe('isEmptyStr() method', function(t) {
t.it('should be false when it has string inside', function(t) {
t.expect(MyApp.Globals.isEmptyStr('somestring')).toEqual(false); //Result: DONE! Expect false to be equal to false
});
});