File: test.js

package info (click to toggle)
node-has-value 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112 kB
  • ctags: 18
  • sloc: makefile: 4; sh: 2
file content (186 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*!
 * has-value <https://github.com/jonschlinkert/has-value>
 *
 * Copyright (c) 2014-2015, Jon Schlinkert.
 * Licensed under the MIT License.
 */

'use strict';

require('mocha');
var assert = require('assert');
var hasValue = require('./');

function isEmpty(o, isZero) {
  return !hasValue.apply(hasValue, arguments);
}

var o = {a: null, b: undefined, c: true, d: false, e: '', f: 'string', g: 0, h: 1, i: 0, j: {}, k: {a: 'b'}, l: [], m: ['a', 'b'], n: function(foo) {}, o: function() {}};

var a = {b: {c: o}};

describe('object properties', function() {
  it('should work for nulls', function() {
    assert(!hasValue(o, 'a'));
    assert(!hasValue(o, 'b'));
  });

  it('should work for booleans', function() {
    assert(hasValue(o, 'c'));
    assert(hasValue(o, 'd'));
  });

  it('shold work for strings', function() {
    assert(!hasValue(o, 'e'));
    assert(hasValue(o, 'f'));
  });

  it('should work for numbers', function() {
    assert(hasValue(o, 'g'));
    assert(hasValue(o, 'h'));
  });

  it('should treat zero as null when `noZero` is set', function() {
    assert(!hasValue(o, 'i', true));
  });

  it('should work for objects', function() {
    assert(!hasValue(o, 'j'));
    assert(hasValue(o, 'k'));
  });

  it('should work for arrays', function() {
    assert(!hasValue(o, 'l'));
    assert(hasValue(o, 'm'));
  });

  it('should work for functions', function() {
    assert(hasValue(o, 'n'));
    assert(!hasValue(o, 'o'));
    assert(!hasValue(o, 'o'));
  });
});

describe('nested object properties', function() {
  it('should work for nulls', function() {
    assert(!hasValue(a, 'b.c.a'));
    assert(!hasValue(a, 'b.c.b'));
  });

  it('should work for booleans', function() {
    assert(hasValue(a, 'b.c.c'));
    assert(hasValue(a, 'b.c.d'));
  });

  it('shold work for strings', function() {
    assert(!hasValue(a, 'b.c.e'));
    assert(hasValue(a, 'b.c.f'));
  });

  it('should work for numbers', function() {
    assert(hasValue(a, 'b.c.g'));
    assert(hasValue(a, 'b.c.h'));
  });

  it('should treat zero as null when `noZero` is set', function() {
    assert(!hasValue(a, 'b.c.i', true));
  });

  it('should work for objects', function() {
    assert(!hasValue(a, 'b.c.j'));
    assert(hasValue(a, 'b.c.k'));
  });

  it('should work for arrays', function() {
    assert(!hasValue(a, 'b.c.l'));
    assert(hasValue(a, 'b.c.m'));
  });

  it('should work for functions', function() {
    assert(hasValue(a, 'b.c.n'));
    assert(!hasValue(a, 'b.c.o'));
    assert(!hasValue(a, 'b.c.o'));
  });
});

describe('single values', function() {
  it('should work for nulls', function() {
    assert(!hasValue(null));
    assert(!hasValue(undefined));
  });

  it('should work for booleans', function() {
    assert(hasValue(true));
    assert(hasValue(false));
  });

  it('shold work for strings', function() {
    assert(!hasValue(''));
    assert(hasValue('string'));
  });

  it('should work for numbers', function() {
    assert(hasValue(0));
    assert(hasValue(1));
  });

  it('should treat zero as null when `noZero` is set', function() {
    assert(!hasValue(0, true));
  });

  it('should work for objects', function() {
    assert(!hasValue({}));
    assert(hasValue({a: 'b'}));
  });

  it('should work for arrays', function() {
    assert(!hasValue([]));
    assert(hasValue(['a', 'b']));
  });

  it('should work for functions', function() {
    assert(hasValue(function(foo) {}));
    assert(!hasValue(function() {}));
  });
});

describe('isEmpty', function() {
  it('should work for nulls', function() {
    assert(isEmpty(null));
    assert(isEmpty(undefined));
  });

  it('should work for booleans', function() {
    assert(!isEmpty(false));
    assert(!isEmpty(true));
  });

  it('shold work for strings', function() {
    assert(isEmpty(''));
    assert(!isEmpty('string'));
  });

  it('should work for numbers', function() {
    assert(!isEmpty(0));
    assert(!isEmpty(1));
  });

  it('should treat zero as null when `noZero` is set', function() {
    assert(isEmpty(0, true));
  });

  it('should work for objects', function() {
    assert(isEmpty({}));
    assert(!isEmpty({a: 'b'}));
  });

  it('should work for arrays', function() {
    assert(isEmpty([]));
    assert(!isEmpty(['a', 'b']));
  });

  it('should work for functions', function() {
    assert(!isEmpty(function(foo) {}));
    assert(isEmpty(function() {}));
  });
});