wrolpi/app/src/components/STLLoader.test.js
lrnselfreliance e5987fdd7e
ASCII STL files preview again (#948)
react-stl-viewer pins three-stdlib 2.17.2, whose STLLoader decodes ASCII STL
files with three's LoaderUtils.decodeText.  three removed that function, so
every ASCII STL preview failed while binary STLs (which never decode text)
still worked.  Override three-stdlib to 2.36.1, which ships its own decodeText.

Jest can now load three: a small transform converts three/three-stdlib ES
modules to CommonJS without babel-preset-react-app, whose class-properties
plugin rejects three's static class blocks at our pinned Babel version.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:55:55 -07:00

41 lines
1.4 KiB
JavaScript

// react-stl-viewer parses STL files with three-stdlib's STLLoader against our top-level three.
// Both STL encodings must parse with the installed three/three-stdlib pair.
import {STLLoader} from 'three-stdlib/loaders/STLLoader.js';
const ASCII_STL = `solid ASCII
facet normal 0 0 1
outer loop
vertex 0 0 0
vertex 1 0 0
vertex 0 1 0
endloop
endfacet
endsolid
`;
function binaryStl() {
// 80-byte header, uint32 triangle count, then one 50-byte triangle.
const buffer = new ArrayBuffer(84 + 50);
const view = new DataView(buffer);
view.setUint32(80, 1, true);
const floats = [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0];
floats.forEach((value, i) => view.setFloat32(84 + i * 4, value, true));
return buffer;
}
describe('STLLoader', () => {
it('parses an ASCII STL file', () => {
const buffer = new TextEncoder().encode(ASCII_STL).buffer;
const geometry = new STLLoader().parse(buffer);
expect(geometry.getAttribute('position').count).toBe(3);
expect(Array.from(geometry.getAttribute('position').array))
.toEqual([0, 0, 0, 1, 0, 0, 0, 1, 0]);
});
it('parses a binary STL file', () => {
const geometry = new STLLoader().parse(binaryStl());
expect(geometry.getAttribute('position').count).toBe(3);
expect(Array.from(geometry.getAttribute('position').array))
.toEqual([0, 0, 0, 1, 0, 0, 0, 1, 0]);
});
});