Skip to content

Commit

Permalink
feat(hooks): add useScene
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Apr 1, 2024
1 parent 23bcd9d commit 499e64d
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './scene';
15 changes: 15 additions & 0 deletions src/helpers/scene.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
11 changes: 11 additions & 0 deletions src/helpers/scene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Scene } from 'phaser';

let _scene: Scene;

export function getScene() {
return _scene;
}

export function setScene(scene: Scene) {
_scene = scene;
}
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useScene';
17 changes: 17 additions & 0 deletions src/hooks/useScene.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
5 changes: 5 additions & 0 deletions src/hooks/useScene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getScene } from '../helpers';

export function useScene() {
return getScene();
}
7 changes: 4 additions & 3 deletions src/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
16 changes: 12 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
},
);

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
12 changes: 9 additions & 3 deletions src/render/render.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Phaser from 'phaser';

import { Container, createElement } from '..';
import { setScene } from '../helpers';
import { render } from './render';

jest.mock('phaser', () => {
Expand All @@ -19,19 +20,24 @@ 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();
});

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);
});
3 changes: 3 additions & 0 deletions src/render/render.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Phaser from 'phaser';
import type { JSX } from 'react';

import { setScene } from '../helpers';
import { createGameObject } from './gameobject';

/**
Expand All @@ -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) {
Expand Down

0 comments on commit 499e64d

Please sign in to comment.