|
| 1 | +import { mount } from 'enzyme'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { CrystalToolkitAnimationScene } from './CrystalToolkitAnimationScene'; |
| 4 | +import { s2 as scene } from '../scene/simple-scene'; |
| 5 | +import { MOUNT_NODE_CLASS, Renderer } from '../scene/constants'; |
| 6 | +import Scene from '../scene/Scene'; |
| 7 | + |
| 8 | +const spy = jest.spyOn(Scene.prototype, 'renderScene'); |
| 9 | +const RENDERSCENE_CALLS_BY_REACT_RENDERING = 1; // goal is to reach 1 and stay there :) |
| 10 | + |
| 11 | +// When we run test, three.js is bundled differently, and we encounter again the bug |
| 12 | +// where we have 2 different instances of three |
| 13 | +describe('<CrystalToolkitAnimationScene/>', () => { |
| 14 | + it('should be rendered', () => { |
| 15 | + const wrapper = renderElement(); |
| 16 | + expect(wrapper.find(`.${MOUNT_NODE_CLASS}`).length).toBe(1); |
| 17 | + expect(wrapper.find(`.mpc-scene-square`).length).toBe(1); |
| 18 | + |
| 19 | + // Note(chab) we call renderScene when we mount, due to the react effect |
| 20 | + // those are the three call sites (constructor / toggleVis / inlet ) |
| 21 | + expect(spy).toBeCalledTimes(1 * RENDERSCENE_CALLS_BY_REACT_RENDERING); |
| 22 | + |
| 23 | + // fails because SVGRender will import a different instance of Three |
| 24 | + // expect(wrapper.find('path').length).toBe(6); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should re-render if we change the size of the screen', () => { |
| 28 | + const wrapper = renderElement(); |
| 29 | + wrapper.setProps({ size: 400 }); |
| 30 | + expect(spy).toBeCalledTimes(2 * RENDERSCENE_CALLS_BY_REACT_RENDERING); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +function renderElement() { |
| 35 | + // we use mount to test the rendering of the underlying elements |
| 36 | + return mount( |
| 37 | + <CrystalToolkitAnimationScene |
| 38 | + sceneSize={500} |
| 39 | + settings={{ |
| 40 | + renderer: Renderer.SVG |
| 41 | + }} |
| 42 | + data={scene} |
| 43 | + debug={false} |
| 44 | + toggleVisibility={{}} |
| 45 | + /> |
| 46 | + ); |
| 47 | +} |
0 commit comments