Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Update tests, remove entitiesByUUIDState
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEstes committed Aug 13, 2024
1 parent d79e5f4 commit 8ca240e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
31 changes: 19 additions & 12 deletions packages/ecs/src/UUIDComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { act, render } from '@testing-library/react'
import assert from 'assert'
import React, { useEffect } from 'react'

import { ReactorRoot, startReactor } from '@etherealengine/hyperflux'
import {
ComponentMap,
getComponent,
Expand All @@ -38,7 +39,7 @@ import {
} from './ComponentFunctions'
import { createEngine, destroyEngine } from './Engine'
import { Entity, EntityUUID, UndefinedEntity } from './Entity'
import { createEntity, removeEntity } from './EntityFunctions'
import { EntityContext, createEntity, removeEntity } from './EntityFunctions'
import { UUIDComponent } from './UUIDComponent'

describe('UUIDComponent', () => {
Expand Down Expand Up @@ -84,10 +85,16 @@ describe('UUIDComponent', () => {
})

it('Should throw an error when the UUID is already in use for another entity', () => {
let root = undefined as ReactorRoot | undefined
setComponent(entity1, UUIDComponent, TestUUID)
assert.throws(() => {
setComponent(entity2, UUIDComponent, TestUUID)
}, Error)
setComponent(entity2, UUIDComponent, TestUUID)
const reactor = UUIDComponent.reactorMap.get(entity2)?.Reactor
root = startReactor(() => {
return React.createElement(EntityContext.Provider, { value: entity2 }, React.createElement(reactor!, {}))
}) as ReactorRoot

assert(root?.errors.length)
root?.stop()
})

it('should remove the old uuid from the entity', () => {
Expand All @@ -114,14 +121,14 @@ describe('UUIDComponent', () => {
it('should remove the component from the entity', () => {
setComponent(entity1, UUIDComponent, TestUUID)
removeComponent(entity1, UUIDComponent)
assert.equal(UndefinedEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(UndefinedEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(false, hasComponent(entity1, UUIDComponent))
assert.equal(getOptionalComponent(entity1, UUIDComponent), undefined)
})

it('should do nothing if the entity does not have the component', () => {
removeComponent(entity1, UUIDComponent)
assert.equal(UndefinedEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(UndefinedEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(getOptionalComponent(entity1, UUIDComponent), undefined)
})
})
Expand All @@ -130,7 +137,7 @@ describe('UUIDComponent', () => {
it('should return the correct entity', () => {
setComponent(entity1, UUIDComponent, TestUUID)
const testEntity = UUIDComponent.getEntityByUUID(TestUUID)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(testEntity, entity1)
})

Expand All @@ -139,12 +146,12 @@ describe('UUIDComponent', () => {
removeComponent(entity1, UUIDComponent)
setComponent(entity1, UUIDComponent, TestUUID2)
const testEntity = UUIDComponent.getEntityByUUID(TestUUID2)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID2].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID2))
})

it('should return UndefinedEntity when the UUID has not been added to any entity', () => {
const testEntity = UUIDComponent.getEntityByUUID(TestUUID)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(testEntity, UndefinedEntity)
})
})
Expand All @@ -153,14 +160,14 @@ describe('UUIDComponent', () => {
it('should return the correct entity when it exists', () => {
setComponent(entity1, UUIDComponent, TestUUID)
const testEntity = UUIDComponent.getOrCreateEntityByUUID(TestUUID)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(testEntity, entity1)
})

it("should create a new entity when the UUID hasn't been added to any entity", () => {
setComponent(entity1, UUIDComponent, TestUUID)
const testEntity = UUIDComponent.getOrCreateEntityByUUID(TestUUID2)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID2].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID2))
assert.notEqual(testEntity, entity1)
})
})
Expand Down Expand Up @@ -246,7 +253,7 @@ describe('UUIDComponent Hooks', async () => {
assert.equal(counter, 1, `The reactor has run an incorrect number of times: ${counter}`)
assert.notEqual(result, undefined, "The result data didn't get assigned.")
assert.equal(result, ExpectedValue, `Did not return the correct data. result = ${result}`)
assert.equal(testEntity, UUIDComponent.entitiesByUUIDState[TestUUID].value)
assert.equal(testEntity, UUIDComponent.getEntityByUUID(TestUUID))
assert.equal(testEntity, ExpectedValue)
unmount()
})
Expand Down
5 changes: 1 addition & 4 deletions packages/ecs/src/UUIDComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Ethereal Engine. All Rights Reserved.

import { MathUtils } from 'three'

import { defineState, getState, State, useImmediateEffect, useMutableState } from '@etherealengine/hyperflux'
import { defineState, getState, useImmediateEffect, useMutableState } from '@etherealengine/hyperflux'

import { defineComponent, setComponent, useComponent } from './ComponentFunctions'
import { Entity, EntityUUID, UndefinedEntity } from './Entity'
Expand All @@ -45,7 +45,6 @@ export const UUIDComponent = defineComponent({
onSet: (entity, component, uuid: EntityUUID) => {
if (!uuid) throw new Error('UUID cannot be empty')
if (component.value === uuid) return

component.set(uuid)
},

Expand Down Expand Up @@ -79,8 +78,6 @@ export const UUIDComponent = defineComponent({
return null
},

entitiesByUUIDState: {} as Record<EntityUUID, State<Entity>>,

useEntityByUUID(uuid: EntityUUID) {
return useMutableState(UUIDState)[uuid].value || UndefinedEntity
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class EEECSImporterExtension extends ImporterExtension implements
if (component === UUIDComponent) {
const uuid = ecsExtensions[jsonID]
//check if uuid already exists
if (UUIDComponent.entitiesByUUIDState[uuid]?.value) {
if (UUIDComponent.getEntityByUUID(uuid)) {
//regenerate uuid if it already exists
ecsExtensions[jsonID] = generateEntityUUID()
}
Expand Down

0 comments on commit 8ca240e

Please sign in to comment.