Skip to content

Commit ac02f66

Browse files
committed
Test specs added for locale methods and parseKey method.
1 parent 23e10f1 commit ac02f66

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

js/lang.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
* @return {object} A key object with main and sub properties.
109109
*/
110110
Lang.prototype.parseKey = function(key) {
111+
if (typeof key !== 'string') {
112+
return null;
113+
}
111114
var segments = key.split('.');
112115
var subKey = segments.pop();
113116
segments.unshift(this.getLocale());

tests/spec/lang_locale_spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
var Lang = new(require('../../js/lang.js'))();
5+
var messages = require('./data/messages');
6+
7+
Lang.setMessages(messages);
8+
9+
describe('The Lang\'s locale methods', function() {
10+
11+
it('should have a getLocale', function() {
12+
expect(Lang.getLocale).toBeDefined();
13+
expect(typeof Lang.getLocale).toBe('function');
14+
});
15+
16+
it('should have a setLocale', function() {
17+
expect(Lang.setLocale).toBeDefined();
18+
expect(typeof Lang.setLocale).toBe('function');
19+
});
20+
21+
it('should return the default locale', function() {
22+
expect(Lang.getLocale()).toBe('en');
23+
});
24+
25+
it('should return the locale specified', function() {
26+
Lang.setLocale('es');
27+
expect(Lang.getLocale()).toBe('es');
28+
});
29+
30+
it('should affect messages', function() {
31+
Lang.setLocale('es');
32+
var es = Lang.get('messages.home');
33+
Lang.setLocale('en');
34+
var en = Lang.get('messages.home');
35+
expect(es).not.toBe(en);
36+
});
37+
38+
});

tests/spec/lang_parseKey_spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
var Lang = new(require('../../js/lang.js'))();
5+
var messages = require('./data/messages');
6+
7+
Lang.setMessages(messages);
8+
9+
describe('The Lang.parseKey() method', function() {
10+
11+
it('should exists', function() {
12+
expect(Lang.parseKey).toBeDefined();
13+
});
14+
15+
it('should be a function', function() {
16+
expect(typeof Lang.parseKey).toBe('function');
17+
});
18+
19+
it('should parse keys correctly', function() {
20+
expect(Lang.parseKey(null)).toBe(null);
21+
expect(Lang.parseKey('foo')).toEqual({
22+
main: 'en',
23+
sub: 'foo'
24+
});
25+
expect(Lang.parseKey('foo.bar')).toEqual({
26+
main: 'en.foo',
27+
sub: 'bar'
28+
});
29+
});
30+
31+
});

0 commit comments

Comments
 (0)