Skip to content

Commit

Permalink
Fix issue where the unit is in the middle of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
Rycochet committed Dec 5, 2017
2 parents 19dade2 + 19e6f97 commit 3b08564
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 46 deletions.
7 changes: 7 additions & 0 deletions src/Velocity/tweens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ namespace VelocityStatic {

indexStart += unitStart.length;
indexEnd += unitEnd.length;
if (unitEnd.length === 0) {
// This order as it's most common for the user supplied
// value to be a number.
unitEnd = unitStart;
} else if (unitStart.length === 0) {
unitStart = unitEnd;
}
if (unitStart === unitEnd) {
// Same units
if (tempStart === tempEnd) {
Expand Down
98 changes: 69 additions & 29 deletions test/src/1. Core/End Value Calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,73 @@
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

QUnit.todo("End Value Calculation", function(assert) {
// /* Standard properties without operators. */
// var $target1 = getTarget(),
// done = assert.async(2);
//
// Velocity($target1, defaultProperties);
// setTimeout(function() {
// assert.equal(Data($target1).style.width.endValue, defaultProperties.width, "Standard end value #1 was calculated.");
// assert.equal(Data($target1).style.opacity.endValue, defaultProperties.opacity, "Standard end value #2 was calculated.");
// done();
// }, asyncCheckDuration);
//
// /* Standard properties with operators. */
// var testIncrementWidth = "5px",
// testDecrementOpacity = 0.25,
// testMultiplyMarginBottom = 4,
// testDivideHeight = 2;
//
// var $target2 = getTarget();
// Velocity($target2, {width: "+=" + testIncrementWidth, opacity: "-=" + testDecrementOpacity, marginBottom: "*=" + testMultiplyMarginBottom, height: "/=" + testDivideHeight});
// setTimeout(function() {
//
// assert.equal(Data($target2).style.width.endValue, defaultStyles.width + parseFloat(testIncrementWidth), "Incremented end value was calculated.");
// assert.equal(Data($target2).style.opacity.endValue, defaultStyles.opacity - testDecrementOpacity, "Decremented end value was calculated.");
// assert.equal(Data($target2).style.marginBottom.endValue, defaultStyles.marginBottom * testMultiplyMarginBottom, "Multiplied end value was calculated.");
// assert.equal(Data($target2).style.height.endValue, defaultStyles.height / testDivideHeight, "Divided end value was calculated.");
//
// done();
// }, asyncCheckDuration);
QUnit.test("End Value Calculation", function(assert) {
// TODO: Add code and tests for operators - probably using calc() internally
// /* Standard properties with operators. */
// var testIncrementWidth = "5px",
// testDecrementOpacity = 0.25,
// testMultiplyMarginBottom = 4,
// testDivideHeight = 2;
//
// var $target2 = getTarget();
// Velocity($target2, {width: "+=" + testIncrementWidth, opacity: "-=" + testDecrementOpacity, marginBottom: "*=" + testMultiplyMarginBottom, height: "/=" + testDivideHeight});
// setTimeout(function() {
//
// assert.equal(Data($target2).style.width.endValue, defaultStyles.width + parseFloat(testIncrementWidth), "Incremented end value was calculated.");
// assert.equal(Data($target2).style.opacity.endValue, defaultStyles.opacity - testDecrementOpacity, "Decremented end value was calculated.");
// assert.equal(Data($target2).style.marginBottom.endValue, defaultStyles.marginBottom * testMultiplyMarginBottom, "Multiplied end value was calculated.");
// assert.equal(Data($target2).style.height.endValue, defaultStyles.height / testDivideHeight, "Divided end value was calculated.");
//
// done();
// }, asyncCheckDuration);

async(assert, 2, async function(done) {
const $target = getTarget();

Velocity($target, {left: "500px"}, {duration: 10});
await sleep(100);
assert.equal(getPropertyValue($target, "left"), "500px", "Finished animated value with given pixels should be the same.");
Velocity($target, {left: "0px"}, {duration: 10});
await sleep(100);
assert.equal(getPropertyValue($target, "left"), "0px", "Finished animated value with 0px should be the same.");

done();
});

async(assert, 1, async function(done) {
const $target = getTarget();

Velocity($target, {left: "500px"}, {duration: 10});
await sleep(100);
Velocity($target, {left: "0"}, {duration: 10});
await sleep(100);
assert.equal(getPropertyValue($target, "left"), "0px", "Finished animated value without giving px, but only number as a string should be the same.");

done();
});

async(assert, 1, async function(done) {
const $target = getTarget();

Velocity($target, {left: "500px"}, {duration: 10});
await sleep(100);
Velocity($target, {left: 0}, {duration: 10});
await sleep(1000);
assert.equal(getPropertyValue($target, "left"), "0px", "Finished animated value given as number 0 should be the same as 0px.");

done();
});

async(assert, 2, async function(done) {
const $target = getTarget();

Velocity($target, {left: 500}, {duration: 10});
await sleep(100);
assert.equal(getPropertyValue($target, "left"), "500px", "Finished animated value with given pixels should be the same.");
Velocity($target, {left: 0}, {duration: 10});
await sleep(100);
assert.equal(getPropertyValue($target, "left"), "0px", "Omitted pixels (px) when given animation should run properly.");

done();
});
});
104 changes: 92 additions & 12 deletions test/test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/test.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,13 @@ var VelocityStatic;
// temporary unit type
indexStart += unitStart.length;
indexEnd += unitEnd.length;
if (unitEnd.length === 0) {
// This order as it's most common for the user supplied
// value to be a number.
unitEnd = unitStart;
} else if (unitStart.length === 0) {
unitStart = unitEnd;
}
if (unitStart === unitEnd) {
// Same units
if (tempStart === tempEnd) {
Expand Down
2 changes: 1 addition & 1 deletion velocity.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions velocity.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 3b08564

Please sign in to comment.