Skip to content

Commit 23e10f1

Browse files
committed
Test specs created for Lang.has() method.
1 parent 20ae442 commit 23e10f1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

js/lang.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*!
22
* Lang.js for Laravel localization in JavaScript.
33
*
4+
* @version 1.0.0
45
* @license MIT
56
* @site https://github.com/rmariuzzo/Laravel-JS-Localization
67
* @author rmariuzzo
@@ -9,6 +10,7 @@
910
'use strict';
1011

1112
(function(root, factory) {
13+
1214
if (typeof define === 'function' && define.amd) {
1315
// AMD support.
1416
define([], factory);
@@ -19,6 +21,7 @@
1921
// Browser global support.
2022
root.Lang = factory();
2123
}
24+
2225
}(this, function() {
2326

2427
// Default options //
@@ -70,7 +73,7 @@
7073
* @return {boolean} true if the given key is defined on the messages source, otherwise false.
7174
*/
7275
Lang.prototype.has = function(key) {
73-
if (!this.messages) {
76+
if (typeof key !== 'string' || !this.messages) {
7477
return false;
7578
}
7679
key = this.parseKey(key);

tests/spec/lang_has_spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.has() method', function() {
10+
11+
it('should exists', function() {
12+
expect(Lang.has).toBeDefined();
13+
});
14+
15+
it('should be a function', function() {
16+
expect(typeof Lang.has).toBe('function');
17+
});
18+
19+
it('should return false when the given key is no defined', function() {
20+
expect(Lang.has('foo.bar')).toBe(false);
21+
expect(Lang.has(null)).toBe(false);
22+
});
23+
24+
it('should return true when the given key is defined', function() {
25+
expect(Lang.has('messages.home')).toBe(true);
26+
expect(Lang.has('validation.accepted')).toBe(true);
27+
});
28+
29+
});

0 commit comments

Comments
 (0)