Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
gonpombo8 committed Nov 1, 2023
1 parent be68993 commit a74c826
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
27 changes: 13 additions & 14 deletions packages/@dcl/react-ecs/src/reconciler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ export function createReconciler(

const onChangeExists = 'onChange' in props
const onSubmitExists = 'onSubmit' in props
const onChange = onChangeExists ? (props['onChange'] as OnChangeState['onChangeCallback']) : undefined
const onSubmit = onSubmitExists ? (props['onSubmit'] as OnChangeState['onSubmitCallback']) : undefined
const entityState = changeEvents.get(instance.entity)?.get(componentId)
const onChange = onChangeExists
? (props['onChange'] as OnChangeState['onChangeCallback'])
: entityState?.onChangeCallback
const onSubmit = onSubmitExists
? (props['onSubmit'] as OnChangeState['onSubmitCallback'])
: entityState?.onSubmitCallback

if (onChangeExists || onSubmitExists) {
updateOnChange(instance.entity, componentId, {
Expand Down Expand Up @@ -329,26 +334,20 @@ export function createReconciler(
for (const [entity, Result] of engine.getEntitiesWith(resultComponent)) {
const entityState = changeEvents.get(entity)?.get(componentId)
const isSubmit = !!(Result as any).isSubmit
let update: boolean = false

if (entityState?.onChangeCallback && Result.value !== entityState.value) {
update = true
entityState.onChangeCallback(Result.value)
}

if (entityState?.onSubmitCallback && isSubmit && !entityState.isSubmit) {
update = true
entityState.onSubmitCallback(Result.value)
}

if (update) {
updateOnChange(entity, componentId, {
onChangeCallback: entityState?.onChangeCallback,
onSubmitCallback: entityState?.onSubmitCallback,
value: Result.value,
isSubmit
})
}
updateOnChange(entity, componentId, {
onChangeCallback: entityState?.onChangeCallback,
onSubmitCallback: entityState?.onSubmitCallback,
value: Result.value,
isSubmit
})
}
}

Expand Down
15 changes: 5 additions & 10 deletions test/react-ecs/input.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Ui Listeners React Ecs', () => {
const UiInputResult = components.UiInputResult(engine)
const uiEntity = ((engine.addEntity() as number) + 1) as Entity
const onChange: jest.Mock | undefined = jest.fn()
const onSubmit: jest.Mock | undefined = jest.fn()
const onSubmit: jest.Mock | undefined = jest.fn()
const undefinedChange: jest.Mock | undefined = undefined
let conditional = true
let removeComponent = false
Expand Down Expand Up @@ -64,19 +64,14 @@ describe('Ui Listeners React Ecs', () => {
await engine.update(1)
expect(onChange).toBeCalledWith('Casla - ')
onChange.mockClear()
expect(onSubmit).toBeCalledTimes(0)
UiInputResult.getMutable(uiEntity).isSubmit = true
await engine.update(1)
expect(onSubmit).toBeCalledTimes(1)
removeComponent = true
await engine.update(1)
UiInputResult.create(uiEntity, { value: 'BOEDO' })
await engine.update(1)
expect(onChange).toBeCalledTimes(0)

onChange.mockClear()
removeComponent = false
await engine.update(1)
expect(onSubmit).toBeCalledTimes(0)
UiInputResult.getMutable(uiEntity).value = '666'
UiInputResult.getMutable(uiEntity).isSubmit = true
await engine.update(1)
expect(onSubmit).toBeCalledTimes(1)
})
})

0 comments on commit a74c826

Please sign in to comment.