Skip to content

Commit

Permalink
test: 补全单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Oct 3, 2023
1 parent 675a6c0 commit 9ba138c
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/components/AMapHawkEye/__tests__/AMapHawkEye.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable no-underscore-dangle */
import * as React from 'react';
import { render, cleanup } from '@testing-library/react';

import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance';

import AMapHawkEye from '../AMapHawkEye';

jest.mock('../../../hooks/useAMapPluginInstance', () => ({
esModule: true,
default: jest.fn((__, cb) => {
cb({
HawkEye: jest.fn(),
}, {});
}),
}));

describe('AMapHawkEye Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(cleanup);

test('renders without crashing', () => {
const autoMove = true;
const showRectangle = true;
const showButton = true;
const mapStyle = 'normal';
const width = '200px';
const height = '150px';
const offset: [number, number] = [10, 10];
const borderStyle = 'solid';
const borderColor = '#000000';
const borderRadius = '5px';
const borderWidth = '2px';
const buttonSize = '20px';

expect(() => {
render(<AMapHawkEye
autoMove={autoMove}
showRectangle={showRectangle}
showButton={showButton}
mapStyle={mapStyle}
width={width}
height={height}
offset={offset}
borderStyle={borderStyle}
borderColor={borderColor}
borderRadius={borderRadius}
borderWidth={borderWidth}
buttonSize={buttonSize}
/>);
}).not.toThrowError();
expect(useAMapPluginInstance).toHaveBeenCalledWith('HawkEye', expect.any(Function));
});

test('renders without crashing when instance is null', () => {
(useAMapPluginInstance as jest.Mock).mockReturnValue(null);
expect(() => {
render(<AMapHawkEye isOpen={false} />);
}).not.toThrowError();
});

test('set to invisible', () => {
const mockInstance = {
show: jest.fn(),
hide: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);
const { rerender } = render(<AMapHawkEye />);

expect(mockInstance.show).toBeCalled();

rerender(<AMapHawkEye visible={false} />);

expect(mockInstance.hide).toBeCalled();
});

test('open and close', () => {
const mockInstance = {
open: jest.fn(),
close: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);
const { rerender } = render(<AMapHawkEye />);

expect(mockInstance.open).toBeCalled();

rerender(<AMapHawkEye isOpen={false} />);

expect(mockInstance.close).toBeCalled();
});

test('bind event correctly', () => {
const mockInstance = {
on: jest.fn(),
off: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);

const onShow = jest.fn();
const onHide = jest.fn();

const { unmount } = render(<AMapHawkEye
onShow={onShow}
onHide={onHide}

/>);

expect(mockInstance.on).toBeCalledTimes(2);
expect(mockInstance.on).toHaveBeenCalledWith('show', onShow);
expect(mockInstance.on).toHaveBeenCalledWith('hide', onHide);

unmount();

expect(mockInstance.off).toBeCalledTimes(2);
expect(mockInstance.off).toHaveBeenCalledWith('show', onShow);
expect(mockInstance.off).toHaveBeenCalledWith('hide', onHide);
});
});
96 changes: 96 additions & 0 deletions src/components/AMapToolBar/__tests__/AMapToolBar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable no-underscore-dangle */
import * as React from 'react';
import { render, cleanup } from '@testing-library/react';

import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance';

import AMapToolBar from '../AMapToolBar';

jest.mock('../../../hooks/useAMapPluginInstance', () => ({
esModule: true,
default: jest.fn((__, cb) => {
cb({
ToolBar: jest.fn(),
}, {});
}),
}));

describe('AMapToolBar Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(cleanup);

test('renders without crashing', () => {
expect(() => {
render(<AMapToolBar />);
}).not.toThrowError();
expect(useAMapPluginInstance).toHaveBeenCalledWith('ToolBar', expect.any(Function));
});

test('change position and offset', () => {
const mockInstance = {
_config: {} as any,
_container: document.createElement('div'),
show: jest.fn(),
hide: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);

const { rerender } = render(<AMapToolBar />);

expect(mockInstance._container.style.cssText).toBe('left: 10px; top: 10px;');

rerender(<AMapToolBar position="RB" offset={[20, 20]} />);

expect(mockInstance._container.style.cssText).toBe('right: 20px; bottom: 20px;');
expect(mockInstance._config.position).toEqual('RB');
expect(mockInstance._config.offset).toEqual([20, 20]);
});

test('set to invisible', () => {
const mockInstance = {
_config: {},
_container: document.createElement('div'),
show: jest.fn(),
hide: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);
const { rerender } = render(<AMapToolBar />);

expect(mockInstance.show).toBeCalled();

rerender(<AMapToolBar visible={false} />);

expect(mockInstance.hide).toBeCalled();
});

test('bind event correctly', () => {
const mockInstance = {
_config: {},
_container: document.createElement('div'),
on: jest.fn(),
off: jest.fn(),
};
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance);

const onShow = jest.fn();
const onHide = jest.fn();

const { unmount } = render(<AMapToolBar
onShow={onShow}
onHide={onHide}

/>);

expect(mockInstance.on).toBeCalledTimes(2);
expect(mockInstance.on).toHaveBeenCalledWith('show', onShow);
expect(mockInstance.on).toHaveBeenCalledWith('hide', onHide);

unmount();

expect(mockInstance.off).toBeCalledTimes(2);
expect(mockInstance.off).toHaveBeenCalledWith('show', onShow);
expect(mockInstance.off).toHaveBeenCalledWith('hide', onHide);
});
});

0 comments on commit 9ba138c

Please sign in to comment.