Skip to content

Commit 9fe9a5b

Browse files
committed
Improve 'default' modifier, and add more test cases
1 parent 0ecd056 commit 9fe9a5b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/modifiers/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function modify(data, model) {
1111
return data;
1212
}
1313

14-
if (!data) {
14+
if (!data && data !== false && data !== 0) {
1515
return model.default;
1616
}
1717

spec/modifiers/default.spec.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,43 @@ describe(' - Default modifier', () => {
1010
});
1111

1212
it('shall not modify data when no model set', () => {
13-
expect(valid('validData', {})).toEqual(null);
14-
expect(valid(undefined, {})).toEqual(null);
13+
expect(valid('validData', {})).toEqual('validData');
14+
expect(valid(undefined, {})).toEqual(undefined);
1515
});
1616

1717
it('shall not modify data if exists and model set', () => {
18-
expect(valid('validData', model)).toEqual(null);
18+
expect(valid('validData', model)).toEqual('validData');
1919
});
2020

21-
it('shall return the default value when not data and model set', () => {
21+
it('shall return the default value whith undefined as data and model set', () => {
2222
expect(valid(undefined, model)).toEqual(model.default);
2323
});
24+
25+
it('shall return "undefined" whith "undefined" (string) as data and model set', () => {
26+
expect(valid('undefined', model)).toEqual('undefined');
27+
});
28+
29+
it('shall return the default value with empty string (\'\') as data and model set', () => {
30+
expect(valid('', model)).toEqual(model.default);
31+
});
32+
33+
it('shall return the default value with null as data and model set', () => {
34+
expect(valid(null, model)).toEqual(model.default);
35+
});
36+
37+
it('shall return the default value with NaN as data and model set', () => {
38+
expect(valid(NaN, model)).toEqual(model.default);
39+
});
40+
41+
it('shall return false (boolean) with false (boolean) as data and model set', () => {
42+
expect(valid(false, model)).toEqual(false);
43+
});
44+
45+
it('shall return "false" (string) with "false" (string) as data and model set', () => {
46+
expect(valid("false", model)).toEqual("false");
47+
});
48+
49+
it('shall return 0 with 0 as data and model set', () => {
50+
expect(valid(0, model)).toEqual(0);
51+
});
2452
});

0 commit comments

Comments
 (0)