-
-
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
74 additions
and
3 deletions.
There are no files selected for viewing
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,71 @@ | ||
import * as React from 'react'; | ||
import { render, cleanup } from '@testing-library/react'; | ||
|
||
import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance'; | ||
|
||
import AMapMapType from '../AMapMapType'; | ||
|
||
jest.mock('../../../hooks/useAMapPluginInstance', () => ({ | ||
esModule: true, | ||
default: jest.fn((__, cb) => { | ||
cb({ | ||
MapType: jest.fn(), | ||
}, {}); | ||
}), | ||
})); | ||
|
||
describe('AMapMapType Component', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
afterEach(cleanup); | ||
|
||
test('renders without crashing', () => { | ||
expect(() => { | ||
render(<AMapMapType />); | ||
}).not.toThrowError(); | ||
expect(useAMapPluginInstance).toHaveBeenCalledWith('MapType', expect.any(Function)); | ||
}); | ||
|
||
test('set to invisible', () => { | ||
const mockInstance = { | ||
show: jest.fn(), | ||
hide: jest.fn(), | ||
}; | ||
(useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); | ||
const { rerender } = render(<AMapMapType />); | ||
|
||
expect(mockInstance.show).toBeCalled(); | ||
|
||
rerender(<AMapMapType visible={false} />); | ||
|
||
expect(mockInstance.hide).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(<AMapMapType | ||
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type AMapMapTypeProps = AMap.MapTypeOptions & { | ||
visible: boolean; | ||
onHide: (event?: any) => void; | ||
onShow: (event?: any) => void; | ||
visible?: boolean; | ||
onHide?: (event?: any) => void; | ||
onShow?: (event?: any) => void; | ||
}; |