UNPKG

@spearwolf/twopoint5d

Version:

Create 2.5D realtime graphics and pixelart with WebGL and three.js

78 lines 3.59 kB
import { getEffectsCount, getSignalsCount } from '@spearwolf/signalize'; import { createSandbox } from 'sinon'; import { NodeMaterial, Texture } from 'three/webgpu'; import { afterEach, describe, expect, test } from 'vitest'; import { AnimatedSpritesMaterial } from './AnimatedSpritesMaterial.js'; const makeAnimsMap = () => { const tex = new Texture(); tex.image = { width: 4, height: 4 }; return tex; }; describe('AnimatedSpritesMaterial', () => { const sandbox = createSandbox(); afterEach(() => { sandbox.restore(); }); test('constructs without options', () => { const material = new AnimatedSpritesMaterial(); expect(material).toBeInstanceOf(AnimatedSpritesMaterial); expect(material.animsMap).toBeUndefined(); material.dispose(); }); test('constructs with an animsMap option', () => { const animsMap = makeAnimsMap(); const material = new AnimatedSpritesMaterial({ animsMap }); expect(material.animsMap).toBe(animsMap); material.dispose(); }); describe('dispose()', () => { test('disposes the animsMap texture', () => { const animsMap = makeAnimsMap(); const animsMapDispose = sandbox.spy(animsMap, 'dispose'); const material = new AnimatedSpritesMaterial({ animsMap }); material.dispose(); expect(animsMapDispose.calledOnce).toBe(true); }); test('does not throw when no animsMap was set', () => { const material = new AnimatedSpritesMaterial(); expect(() => material.dispose()).not.toThrow(); }); test('clears the animsMap reference', () => { const animsMap = makeAnimsMap(); const material = new AnimatedSpritesMaterial({ animsMap }); material.dispose(); expect(material.animsMap).toBeUndefined(); }); test('disposes the animsMap texture before the underlying NodeMaterial dispose runs', () => { const animsMap = makeAnimsMap(); const animsMapDispose = sandbox.spy(animsMap, 'dispose'); const nodeMaterialDispose = sandbox.spy(NodeMaterial.prototype, 'dispose'); const material = new AnimatedSpritesMaterial({ animsMap }); material.dispose(); expect(animsMapDispose.calledOnce).toBe(true); expect(nodeMaterialDispose.called).toBe(true); expect(animsMapDispose.calledBefore(nodeMaterialDispose)).toBe(true); }); test('does not leak signals or effects', () => { const baselineSignals = getSignalsCount(); const baselineEffects = getEffectsCount(); const material = new AnimatedSpritesMaterial({ animsMap: makeAnimsMap() }); expect(getSignalsCount()).toBeGreaterThan(baselineSignals); expect(getEffectsCount()).toBeGreaterThan(baselineEffects); material.dispose(); expect(getSignalsCount()).toBe(baselineSignals); expect(getEffectsCount()).toBe(baselineEffects); }); test('is safe to call twice', () => { const animsMap = makeAnimsMap(); const animsMapDispose = sandbox.spy(animsMap, 'dispose'); const material = new AnimatedSpritesMaterial({ animsMap }); expect(() => { material.dispose(); material.dispose(); }).not.toThrow(); expect(animsMapDispose.calledOnce).toBe(true); }); }); }); //# sourceMappingURL=AnimatedSpritesMaterial.spec.js.map