From 499e64dbcdd953b075a00b98d983983ac0c9421d Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 1 Apr 2024 19:05:53 -0400 Subject: [PATCH] feat(hooks): add useScene --- src/helpers/index.ts | 1 + src/helpers/scene.test.ts | 15 +++++++++++++++ src/helpers/scene.ts | 11 +++++++++++ src/hooks/index.ts | 1 + src/hooks/useScene.test.ts | 17 +++++++++++++++++ src/hooks/useScene.ts | 5 +++++ src/index.test.mjs | 7 ++++--- src/index.test.ts | 16 ++++++++++++---- src/index.ts | 1 + src/render/render.test.ts | 12 +++++++++--- src/render/render.ts | 3 +++ 11 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 src/helpers/index.ts create mode 100644 src/helpers/scene.test.ts create mode 100644 src/helpers/scene.ts create mode 100644 src/hooks/index.ts create mode 100644 src/hooks/useScene.test.ts create mode 100644 src/hooks/useScene.ts diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 00000000..15df9a6f --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1 @@ +export * from './scene'; diff --git a/src/helpers/scene.test.ts b/src/helpers/scene.test.ts new file mode 100644 index 00000000..75b03319 --- /dev/null +++ b/src/helpers/scene.test.ts @@ -0,0 +1,15 @@ +import { Scene } from 'phaser'; + +import { getScene, setScene } from './scene'; + +const scene = new Scene(); + +afterEach(() => { + setScene(undefined as unknown as Scene); +}); + +it('sets and gets scene', () => { + expect(getScene()).toEqual(undefined); + expect(setScene(scene)).toEqual(undefined); + expect(getScene()).toEqual(scene); +}); diff --git a/src/helpers/scene.ts b/src/helpers/scene.ts new file mode 100644 index 00000000..57f1c136 --- /dev/null +++ b/src/helpers/scene.ts @@ -0,0 +1,11 @@ +import type { Scene } from 'phaser'; + +let _scene: Scene; + +export function getScene() { + return _scene; +} + +export function setScene(scene: Scene) { + _scene = scene; +} diff --git a/src/hooks/index.ts b/src/hooks/index.ts new file mode 100644 index 00000000..043b1344 --- /dev/null +++ b/src/hooks/index.ts @@ -0,0 +1 @@ +export * from './useScene'; diff --git a/src/hooks/useScene.test.ts b/src/hooks/useScene.test.ts new file mode 100644 index 00000000..7dc5c285 --- /dev/null +++ b/src/hooks/useScene.test.ts @@ -0,0 +1,17 @@ +import { Scene } from 'phaser'; + +import { getScene } from '../helpers'; +import { useScene } from './useScene'; + +jest.mock('../helpers/scene', () => ({ + getScene: jest.fn(), +})); + +const mockedGetScene = jest.mocked(getScene); + +it('returns scene', () => { + const scene = new Scene(); + mockedGetScene.mockReturnValueOnce(scene); + expect(useScene()).toEqual(scene); + expect(mockedGetScene).toHaveBeenCalledTimes(1); +}); diff --git a/src/hooks/useScene.ts b/src/hooks/useScene.ts new file mode 100644 index 00000000..8aed24b6 --- /dev/null +++ b/src/hooks/useScene.ts @@ -0,0 +1,5 @@ +import { getScene } from '../helpers'; + +export function useScene() { + return getScene(); +} diff --git a/src/index.test.mjs b/src/index.test.mjs index c0d325a1..d70d35a8 100644 --- a/src/index.test.mjs +++ b/src/index.test.mjs @@ -10,12 +10,13 @@ import { Image, jsx, render, + useScene, Video, } from '../cjs/index.js'; -[createElement, Image, render, Video].forEach((func) => { - it(`exports ${func.name}`, () => { - assert.strictEqual(typeof func, 'function'); +[createElement, Image, render, Video, useScene].forEach((fun) => { + it(`exports ${fun.name}`, () => { + assert.strictEqual(typeof fun, 'function'); }); }); diff --git a/src/index.test.ts b/src/index.test.ts index 6b93918b..c9e61eab 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,9 +1,17 @@ -import { Container, createElement, jsx, Rectangle, render, Text } from '.'; +import { + Container, + createElement, + jsx, + Rectangle, + render, + Text, + useScene, +} from '.'; -it.each([Container, Rectangle, Text, createElement, render])( +it.each([Container, Rectangle, Text, createElement, render, useScene])( 'exports %p', - (func) => { - expect(func).toBeInstanceOf(Function); + (fun) => { + expect(fun).toBeInstanceOf(Function); }, ); diff --git a/src/index.ts b/src/index.ts index 4a5f43a4..82bbeed7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from './components'; export { createElement, jsx } from './element'; +export { useScene } from './hooks'; export { createRef } from './ref'; export { render } from './render'; export type { GameObjectProps, Ref } from './types'; diff --git a/src/render/render.test.ts b/src/render/render.test.ts index a84e59eb..6186e465 100644 --- a/src/render/render.test.ts +++ b/src/render/render.test.ts @@ -1,6 +1,7 @@ import Phaser from 'phaser'; import { Container, createElement } from '..'; +import { setScene } from '../helpers'; import { render } from './render'; jest.mock('phaser', () => { @@ -19,13 +20,17 @@ jest.mock('phaser', () => { }; }); +jest.mock('../helpers/scene', () => ({ + setScene: jest.fn(), +})); + it('does not render element into the scene', () => { const spy = jest.spyOn(console, 'warn').mockImplementation(); const element = {} as JSX.Element; const scene = new Phaser.Scene(); expect(render(element, scene)).toBe(undefined); - expect(scene.add.existing).not.toBeCalled(); - expect(spy).toBeCalledTimes(1); + expect(scene.add.existing).not.toHaveBeenCalled(); + expect(spy).toHaveBeenCalledTimes(1); spy.mockRestore(); }); @@ -33,5 +38,6 @@ it('renders element into the scene', () => { const element = createElement(Container); const scene = new Phaser.Scene(); expect(render(element, scene)).toBe(undefined); - expect(scene.add.existing).toBeCalledTimes(1); + expect(scene.add.existing).toHaveBeenCalledTimes(1); + expect(setScene).toHaveBeenCalledWith(scene); }); diff --git a/src/render/render.ts b/src/render/render.ts index f07c126f..f2deb76f 100644 --- a/src/render/render.ts +++ b/src/render/render.ts @@ -1,6 +1,7 @@ import Phaser from 'phaser'; import type { JSX } from 'react'; +import { setScene } from '../helpers'; import { createGameObject } from './gameobject'; /** @@ -10,6 +11,8 @@ import { createGameObject } from './gameobject'; * @param scene - Phaser scene. */ export function render(element: JSX.Element, scene: Phaser.Scene) { + setScene(scene); + const gameObject = createGameObject(element, scene); if (gameObject) {