Skip to content

Commit ff3f3ca

Browse files
fix: don't warn with 'Invalid repetitionCount value: 1. Using default: Infinity' for valid modelViewer.appendAnimation(animName, { repetitions: 1 }) (#5178)
It actually just emitted the warning and kept repetitionCount to 1 anyway, the else branch was added in 305e07c
1 parent 33e5562 commit ff3f3ca

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

packages/model-viewer/src/test/three-components/ModelScene-spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import '../renderer-gate.js';
1717

1818
import {expect} from 'chai';
19-
import {Matrix4, Mesh, SphereGeometry, Vector3} from 'three';
19+
import {LoopRepeat, Matrix4, Mesh, SphereGeometry, Vector3} from 'three';
2020

2121
import {$scene} from '../../model-viewer-base.js';
2222
import {ModelViewerElement} from '../../model-viewer.js';
@@ -81,6 +81,51 @@ suite('ModelScene', () => {
8181
scene.detachAnimation(animationName, false);
8282
expect(scene.appendedAnimations).to.not.include(animationName);
8383
});
84+
85+
suite('appendAnimation repetitionCount validation', () => {
86+
// Returns the repetitionCount-related warnings emitted while appending the
87+
// first animation with the given repetitionCount.
88+
const repetitionWarnings = (repetitionCount: any): string[] => {
89+
const warnings: string[] = [];
90+
const originalWarn = console.warn;
91+
console.warn = (...args: any[]) => void warnings.push(args.join(' '));
92+
try {
93+
scene.appendAnimation(
94+
scene.animationNames[0], LoopRepeat, repetitionCount);
95+
} finally {
96+
console.warn = originalWarn;
97+
}
98+
return warnings.filter((w) => w.includes('repetitionCount'));
99+
};
100+
101+
test('a valid numeric value does not warn', () => {
102+
expect(repetitionWarnings(1)).to.be.empty;
103+
});
104+
105+
test('Infinity (the default) does not warn', () => {
106+
expect(repetitionWarnings(Infinity)).to.be.empty;
107+
});
108+
109+
test('a numeric value < 1 warns', () => {
110+
expect(repetitionWarnings(0)).to.have.lengthOf(1);
111+
});
112+
113+
test('NaN warns', () => {
114+
expect(repetitionWarnings(NaN)).to.have.lengthOf(1);
115+
});
116+
117+
test('a numeric string >= 1 does not warn', () => {
118+
expect(repetitionWarnings('3')).to.be.empty;
119+
});
120+
121+
test('a numeric string < 1 warns', () => {
122+
expect(repetitionWarnings('0')).to.have.lengthOf(1);
123+
});
124+
125+
test('a non-numeric string warns', () => {
126+
expect(repetitionWarnings('abc')).to.have.lengthOf(1);
127+
});
128+
});
84129
});
85130

86131
suite('setSize', () => {

packages/model-viewer/src/three-components/ModelScene.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,17 @@ export class ModelScene extends Scene {
990990
}
991991
repetitionCount = Math.max(parseInt(repetitionCount), 1);
992992
}
993-
} else if (typeof repetitionCount === 'number' && repetitionCount < 1) {
994-
repetitionCount = 1;
995-
console.warn(`Invalid repetitionCount value: ${
996-
repetitionCount}. Using 1 value as minimum.`);
993+
} else if (typeof repetitionCount === 'number' && !isNaN(repetitionCount)) {
994+
// A valid number >= 1 (including Infinity) is left untouched.
995+
if (repetitionCount < 1) {
996+
console.warn(`Invalid repetitionCount value: ${
997+
repetitionCount}. Using 1 as minimum.`);
998+
repetitionCount = 1;
999+
}
9971000
} else {
9981001
console.warn(`Invalid repetitionCount value: ${
9991002
repetitionCount}. Using default: Infinity`);
1003+
repetitionCount = Infinity;
10001004
}
10011005

10021006
if (repetitionCount === 1 && loopMode !== LoopOnce) {

0 commit comments

Comments
 (0)