-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/components/AMapHawkEye/__tests__/AMapHawkEye.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |