Skip to content

Commit

Permalink
Merge pull request #4883 from OjasM:develop
Browse files Browse the repository at this point in the history
[bugfix] Invalid duration gets valid when wrapping/cloning (#4323)
  • Loading branch information
ichernev committed Apr 24, 2020
2 parents 9bd2dc7 + bd4950b commit 1f88f6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/duration/create.js
Expand Up @@ -74,6 +74,10 @@ export function createDuration (input, key) {
ret._locale = input._locale;
}

if (isDuration(input) && hasOwnProp(input, '_isValid')) {
ret._isValid = input._isValid;
}

return ret;
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/moment/duration_invalid.js
Expand Up @@ -15,6 +15,30 @@ test('valid duration', function (assert) {
assert.equal(m.valueOf(), 0);
});

test('invalid duration - clone of invalid duration', function (assert) {
var m = moment.duration.invalid().clone(); // should be invalid
assert.equal(m.isValid(), false);
assert.ok(isNaN(m.valueOf()));
});

test('valid duration - clone of valid duration', function (assert) {
var m = moment.duration({d: null}).clone(); // should be valid, for now
assert.equal(m.isValid(), true);
assert.equal(m.valueOf(), 0);
});

test('invalid duration - wrapper of invalid duration', function (assert) {
var m = moment.duration(moment.duration.invalid()); // should be invalid
assert.equal(m.isValid(), false);
assert.ok(isNaN(m.valueOf()));
});

test('valid duration - wrapper of valid duration', function (assert) {
var m = moment.duration(moment.duration({d: null})); // should be valid, for now
assert.equal(m.isValid(), true);
assert.equal(m.valueOf(), 0);
});

test('invalid duration - only smallest unit can have decimal', function (assert) {
var m = moment.duration({'days': 3.5, 'hours': 1.1}); // should be invalid
assert.equal(m.isValid(), false);
Expand Down

0 comments on commit 1f88f6d

Please sign in to comment.