Skip to content

Commit

Permalink
fix: 错误的类型声明
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Oct 3, 2023
1 parent 30f75f8 commit 646b654
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
71 changes: 71 additions & 0 deletions src/components/AMapMapType/__tests__/AMapMapType.test.tsx
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);
});
});
6 changes: 3 additions & 3 deletions src/components/AMapMapType/interface.ts
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;
};

0 comments on commit 646b654

Please sign in to comment.