-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add destroy method
- Loading branch information
1 parent
e69f263
commit 71a205a
Showing
5 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tedengine/ted": minor | ||
--- | ||
|
||
Add destroy method to components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import TActorComponent from '../actor-components/actor-component'; | ||
import TActor from '../core/actor'; | ||
|
||
describe('TActorComponent', () => { | ||
describe('destroy', () => { | ||
test('should call the "onDestroy" method if available', () => { | ||
const onDestroyMock = jest.fn(); | ||
const actor = new TActor(); | ||
const actorComponent = new TActorComponent(actor) as any; | ||
actorComponent.onDestroy = onDestroyMock; | ||
actorComponent.destroy(); | ||
|
||
expect(onDestroyMock).toHaveBeenCalled(); | ||
expect(actorComponent.dead).toBe(true); | ||
}); | ||
|
||
test('should only call the "onDestroy" method once if destroy is called multiple times', () => { | ||
const onDestroyMock = jest.fn(); | ||
const actor = new TActor(); | ||
const actorComponent = new TActorComponent(actor) as any; | ||
actorComponent.onDestroy = onDestroyMock; | ||
actorComponent.destroy(); | ||
actorComponent.destroy(); | ||
|
||
expect(onDestroyMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import TActorComponent from '../actor-components/actor-component'; | ||
import TActor from './actor'; | ||
|
||
describe('TActor', () => { | ||
describe('destroy', () => { | ||
test('should set the "dead" flag to true', () => { | ||
const actor = new TActor(); | ||
actor.destroy(); | ||
expect(actor.dead).toBe(true); | ||
}); | ||
|
||
test('should call the "destroy" method on each component', () => { | ||
const actor = new TActor(); | ||
const component1 = new TActorComponent(actor); | ||
component1.destroy = jest.fn(); | ||
|
||
const component2 = new TActorComponent(actor); | ||
component2.destroy = jest.fn(); | ||
|
||
actor.destroy(); | ||
expect(component1.destroy).toHaveBeenCalled(); | ||
expect(component2.destroy).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should call the "onDestroy" method if available', () => { | ||
const actor = new TActor() as any; | ||
actor.onDestroy = jest.fn(); | ||
actor.destroy(); | ||
expect(actor.onDestroy).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should only call the "onDestroy" method once if destory is called multiple times', () => { | ||
const actor = new TActor() as any; | ||
actor.onDestroy = jest.fn(); | ||
actor.destroy(); | ||
actor.destroy(); | ||
expect(actor.onDestroy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should remove the actor from the world if it exists', () => { | ||
const actor = new TActor(); | ||
const world = { removeActor: jest.fn() }; | ||
actor.world = world as any; | ||
actor.destroy(); | ||
expect(world.removeActor).toHaveBeenCalledWith(actor); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters