From d183fa5fb08130a1fd966bb2ccaaf8d49dc45b7b Mon Sep 17 00:00:00 2001 From: Cody Bennett Date: Thu, 26 Oct 2023 02:20:46 -0500 Subject: [PATCH 1/3] chore(tests): add array attach case --- packages/fiber/tests/core/renderer.test.tsx | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/fiber/tests/core/renderer.test.tsx b/packages/fiber/tests/core/renderer.test.tsx index e24b19abfa..412a97b7b4 100644 --- a/packages/fiber/tests/core/renderer.test.tsx +++ b/packages/fiber/tests/core/renderer.test.tsx @@ -546,6 +546,57 @@ describe('renderer', () => { expect(state.scene.children[3]).toBe(c) }) + it('can swap 4 array primitives via attach', async () => { + let state: RootState = null! + const a = new THREE.Group() + const b = new THREE.Group() + const c = new THREE.Group() + const d = new THREE.Group() + const array = [a, b, c, d] + + const Test = ({ array }: { array: THREE.Group[] }) => ( + <> + {array.map((group, i) => ( + + ))} + + ) + + await act(async () => { + state = root.render().getState() + }) + + expect(state.scene.children.length).toBe(0) + expect(state.scene.userData.objects[0]).toBe(a) + expect(state.scene.userData.objects[1]).toBe(b) + expect(state.scene.userData.objects[2]).toBe(c) + expect(state.scene.userData.objects[3]).toBe(d) + + const reversedArray = [...array.reverse()] + + await act(async () => { + state = root.render().getState() + }) + + expect(state.scene.children.length).toBe(0) + expect(state.scene.userData.objects[0]).toBe(d) + expect(state.scene.userData.objects[1]).toBe(c) + expect(state.scene.userData.objects[2]).toBe(b) + expect(state.scene.userData.objects[3]).toBe(a) + + const mixedArray = [b, a, d, c] + + await act(async () => { + state = root.render().getState() + }) + + expect(state.scene.children.length).toBe(0) + expect(state.scene.userData.objects[0]).toBe(b) + expect(state.scene.userData.objects[1]).toBe(a) + expect(state.scene.userData.objects[2]).toBe(d) + expect(state.scene.userData.objects[3]).toBe(c) + }) + it('will make an Orthographic Camera & set the position', async () => { let camera: THREE.Camera = null! From 41cccce0f034e88bd5ded55cb87f3d1deb662bd5 Mon Sep 17 00:00:00 2001 From: Cody Bennett Date: Thu, 26 Oct 2023 02:28:08 -0500 Subject: [PATCH 2/3] experiment: label objects Working around local issues with Jest --- packages/fiber/tests/core/renderer.test.tsx | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/fiber/tests/core/renderer.test.tsx b/packages/fiber/tests/core/renderer.test.tsx index 412a97b7b4..635d8db149 100644 --- a/packages/fiber/tests/core/renderer.test.tsx +++ b/packages/fiber/tests/core/renderer.test.tsx @@ -500,10 +500,10 @@ describe('renderer', () => { it('can swap 4 array primitives', async () => { let state: RootState = null! - const a = new THREE.Group() - const b = new THREE.Group() - const c = new THREE.Group() - const d = new THREE.Group() + const a = Object.assign(new THREE.Group(), { name: 'a' }) + const b = Object.assign(new THREE.Group(), { name: 'b' }) + const c = Object.assign(new THREE.Group(), { name: 'c' }) + const d = Object.assign(new THREE.Group(), { name: 'd' }) const array = [a, b, c, d] const Test = ({ array }: { array: THREE.Group[] }) => ( @@ -548,10 +548,10 @@ describe('renderer', () => { it('can swap 4 array primitives via attach', async () => { let state: RootState = null! - const a = new THREE.Group() - const b = new THREE.Group() - const c = new THREE.Group() - const d = new THREE.Group() + const a = Object.assign(new THREE.Group(), { name: 'a' }) + const b = Object.assign(new THREE.Group(), { name: 'b' }) + const c = Object.assign(new THREE.Group(), { name: 'c' }) + const d = Object.assign(new THREE.Group(), { name: 'd' }) const array = [a, b, c, d] const Test = ({ array }: { array: THREE.Group[] }) => ( @@ -567,6 +567,7 @@ describe('renderer', () => { }) expect(state.scene.children.length).toBe(0) + console.log(state.scene.userData.objects.map((o: any) => o.name)) expect(state.scene.userData.objects[0]).toBe(a) expect(state.scene.userData.objects[1]).toBe(b) expect(state.scene.userData.objects[2]).toBe(c) From 3427b3e002f423d5199180decfac8c76a6bb67cb Mon Sep 17 00:00:00 2001 From: Cody Bennett Date: Thu, 26 Oct 2023 04:07:21 -0500 Subject: [PATCH 3/3] experiment: revert changes --- packages/fiber/src/core/renderer.ts | 9 +++------ .../__snapshots__/RTTR.core.test.tsx.snap | 14 +++++++------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/fiber/src/core/renderer.ts b/packages/fiber/src/core/renderer.ts index 617b998dc5..371fe6cb61 100644 --- a/packages/fiber/src/core/renderer.ts +++ b/packages/fiber/src/core/renderer.ts @@ -272,16 +272,13 @@ function createRenderer(_roots: Map, _getEventPriority?: instance.__r3f.objects.forEach((child) => appendChild(newInstance, child)) instance.__r3f.objects = [] - const autoRemovedBeforeAppend = !!newInstance.parent + newInstance.__r3f.autoRemovedBeforeAppend = !!newInstance.__r3f.parent if (!instance.__r3f.autoRemovedBeforeAppend) { - insertBefore(parent, newInstance, instance) + // insertBefore(parent, newInstance, instance) removeChild(parent, instance) - } else { - appendChild(parent, newInstance) } - - newInstance.__r3f.autoRemovedBeforeAppend = autoRemovedBeforeAppend + appendChild(parent, newInstance) // Re-bind event handlers if (newInstance.raycast && newInstance.__r3f.eventCount) { diff --git a/packages/test-renderer/src/__tests__/__snapshots__/RTTR.core.test.tsx.snap b/packages/test-renderer/src/__tests__/__snapshots__/RTTR.core.test.tsx.snap index bc4b647a4b..3f068db859 100644 --- a/packages/test-renderer/src/__tests__/__snapshots__/RTTR.core.test.tsx.snap +++ b/packages/test-renderer/src/__tests__/__snapshots__/RTTR.core.test.tsx.snap @@ -296,19 +296,19 @@ Array [ Object { "children": Array [], "props": Object { - "args": Array [ - 2, - 2, - ], + "args": Array [], }, - "type": "boxGeometry", + "type": "meshBasicMaterial", }, Object { "children": Array [], "props": Object { - "args": Array [], + "args": Array [ + 2, + 2, + ], }, - "type": "meshBasicMaterial", + "type": "boxGeometry", }, ], "props": Object {