Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed May 24, 2021
1 parent c406c45 commit 7ab5b85
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/lib/viewers/box3d/__tests__/Box3DViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,41 @@ describe('lib/viewers/box3d/Box3DViewer', () => {
box3d.handleSceneLoaded();
expect(box3d.controls.addUi).toBeCalled();
});

describe('With react controls', () => {
beforeEach(() => {
jest.spyOn(box3d, 'getViewerOption').mockImplementation(() => true);
});

test('should not call addUi', () => {
box3d.handleSceneLoaded();

expect(box3d.controls.addUi).not.toBeCalled();
});
});
});

describe('handleShowVrButton()', () => {
test('should call controls.showVrButton()', () => {
beforeEach(() => {
box3d.controls.showVrButton = jest.fn();
});

test('should call controls.showVrButton()', () => {
box3d.handleShowVrButton();

expect(box3d.controls.showVrButton).toBeCalled();
});

describe('With react controls', () => {
beforeEach(() => {
jest.spyOn(box3d, 'getViewerOption').mockImplementation(() => true);
});

test('should not call addUi', () => {
box3d.handleShowVrButton();

expect(box3d.controls.showVrButton).not.toBeCalled();
});
});
});

Expand Down
34 changes: 34 additions & 0 deletions src/lib/viewers/box3d/image360/__tests__/Image360Controls-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import FullscreenToggle from '../../../controls/fullscreen';
import Image360Controls, { Props } from '../Image360Controls';
import VrToggleControl from '../../../controls/box3d/VrToggleControl';

describe('lib/viewers/box3d/image360/Image360Controls', () => {
const getDefaults = (): Props => ({
isVrShown: false,
onFullscreenToggle: jest.fn(),
onVrToggle: jest.fn(),
});

const getWrapper = (props: Partial<Props>): ShallowWrapper =>
shallow(<Image360Controls {...getDefaults()} {...props} />);

describe('render()', () => {
test('should return a valid wrapper', () => {
const onFullscreenToggle = jest.fn();
const onVrToggle = jest.fn();

const wrapper = getWrapper({
onFullscreenToggle,
onVrToggle,
});

expect(wrapper.find(VrToggleControl).props()).toMatchObject({
isVrShown: false,
onVrToggle,
});
expect(wrapper.find(FullscreenToggle).prop('onFullscreenToggle')).toEqual(onFullscreenToggle);
});
});
});
76 changes: 76 additions & 0 deletions src/lib/viewers/box3d/image360/__tests__/Image360Viewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Image360Viewer from '../Image360Viewer';
import BaseViewer from '../../../BaseViewer';
import Box3DControls from '../../Box3DControls';
import ControlsRoot from '../../../controls/controls-root';
import Image360Renderer from '../Image360Renderer';
import { SELECTOR_BOX_PREVIEW_CONTENT } from '../../../../constants';

Expand Down Expand Up @@ -62,5 +63,80 @@ describe('lib/viewers/box3d/image360/Image360Viewer', () => {
test('should create Image360Renderer instance and assign to .renderer', () => {
expect(viewer.renderer).toBeInstanceOf(Image360Renderer);
});

describe('With react controls', () => {
beforeEach(() => {
jest.spyOn(viewer, 'getViewerOption').mockImplementation(() => true);
});

test('should create ControlsRoot instance and assign to .controls', () => {
viewer.createSubModules();
expect(viewer.controls).toBeInstanceOf(ControlsRoot);
});
});
});

describe('handleSceneLoaded()', () => {
test('should render the react UI', () => {
jest.spyOn(viewer, 'renderUI');
viewer.handleSceneLoaded();
expect(viewer.renderUI).toBeCalled();
});
});

describe('handleShowVrButton()', () => {
beforeEach(() => {
jest.spyOn(viewer, 'getViewerOption').mockImplementation(() => true);
jest.spyOn(viewer, 'renderUI');

viewer.setup();
viewer.createSubModules();
});

test('should render the react UI', () => {
viewer.handleShowVrButton();

expect(viewer.showVrButton).toBe(true);
expect(viewer.renderUI).toBeCalled();
});

describe('Without react controls', () => {
beforeEach(() => {
jest.spyOn(viewer, 'getViewerOption').mockImplementation(() => false);

viewer.createSubModules();

jest.spyOn(viewer.controls, 'showVrButton');
});

test('should call showVrButton on the controls', () => {
viewer.handleShowVrButton();

expect(viewer.renderUI).not.toBeCalled();
expect(viewer.controls.showVrButton).toBeCalled();
});
});
});

describe('renderUI()', () => {
const getProps = instance => instance.controls.render.mock.calls[0][0].props;

beforeEach(() => {
jest.spyOn(viewer, 'getViewerOption').mockImplementation(() => true);
viewer.controls = {
destroy: jest.fn(),
render: jest.fn(),
};
});

test('should render react controls with the correct props', () => {
viewer.renderUI();

expect(getProps(viewer)).toMatchObject({
isVrShown: false,
onFullscreenToggle: viewer.toggleFullscreen,
onVrToggle: viewer.handleToggleVr,
});
});
});
});

0 comments on commit 7ab5b85

Please sign in to comment.