Skip to content

Commit

Permalink
refactor: 移除不必要的逻辑判断
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Oct 8, 2023
1 parent 9ba138c commit 03dc214
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/components/AMapMap/AMapMap.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PropsWithChildren } from 'react';
import React, {
import * as React from 'react';
import {
useEffect,
useRef,
useState,
Expand Down Expand Up @@ -98,7 +99,7 @@ const AMapMap = forwardRef<any, AMapMapProps>(
// set city
useEffect(() => {
if (cityName) {
curMap?.setCity?.(cityName, () => {});
curMap?.setCity(cityName, () => {});
}
}, [cityName, curMap]);

Expand All @@ -112,7 +113,7 @@ const AMapMap = forwardRef<any, AMapMapProps>(
// set center
useEffect(() => {
if (center) {
curMap?.setCenter?.(center);
curMap?.setCenter(center);
}
}, [center, curMap]);

Expand Down
109 changes: 109 additions & 0 deletions src/components/AMapMap/__tests__/AMapMap.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as React from 'react';
import { createRef } from 'react';
import { render, cleanup } from '@testing-library/react';

import useAMapAPI from '../../../hooks/useAMapAPI';
import AMapMap from '../AMapMap';

jest.mock('../../../hooks/useAMapAPI', () => ({
esModule: true,
default: jest.fn().mockReturnValue({
__AMAP__: {
Map: jest.fn().mockReturnValue({
add: jest.fn(),
emit: jest.fn(),
remove: jest.fn(),
destroy: jest.fn(),
setCity: jest.fn(),
setZoom: jest.fn(),
setCenter: jest.fn(),
on: jest.fn(),
}),
},
__AMAP_UI__: null,
}),
}));

describe('AMapMap', () => {
afterEach(cleanup);

test('render without error', () => {
const $ref = createRef<any>();
expect(() => {
const { unmount, rerender } = render(<AMapMap ref={$ref} />);
rerender(<AMapMap ref={$ref} />);
unmount();
}).not.toThrowError();
});

test('support ref to instance', () => {
const $ref = createRef<any>();
render(<AMapMap ref={$ref} />);

expect($ref.current.add).toEqual(expect.any(Function));
expect($ref.current.emit).toEqual(expect.any(Function));
expect($ref.current.remove).toEqual(expect.any(Function));
expect($ref.current.destroy).toEqual(expect.any(Function));
expect($ref.current.setCity).toEqual(expect.any(Function));
expect($ref.current.setZoom).toEqual(expect.any(Function));
expect($ref.current.setCenter).toEqual(expect.any(Function));
expect($ref.current.on).toEqual(expect.any(Function));

$ref.current.on('add', jest.fn());
});

test('ref is null when loading API sdk', () => {
(useAMapAPI as jest.Mock).mockReturnValueOnce({
__AMAP__: null,
__AMAP_UI__: null,
});

const $ref = createRef<any>();
render(<AMapMap ref={$ref} cityName="深圳" center={[118, 23]} zoom={2} />);
expect($ref.current).toEqual(null);
});

test('set city name', () => {
const $ref = createRef<any>();
render(<AMapMap ref={$ref} cityName="深圳" />);
expect($ref.current.setCity).toBeCalledWith('深圳', expect.any(Function));
});

test('set zoom', () => {
const $ref = createRef<any>();
render(<AMapMap ref={$ref} zoom={12} />);
expect($ref.current.setZoom).toBeCalledWith(12);
});

test('set center', () => {
const $ref = createRef<any>();
const mockCenter: [number, number] = [110, 23];
render(<AMapMap ref={$ref} center={mockCenter} />);
expect($ref.current.setCenter).toBeCalledWith(mockCenter);
});

test('emit "overlaysAdded" event', () => {
const $ref = createRef<any>();
render(<AMapMap ref={$ref} />);

const overlay = {};
$ref.current.add(overlay);

expect($ref.current.emit).toBeCalledWith('overlaysAdded', overlay);
});

test('emit "overlaysRemoved" event', () => {
const $ref = createRef<any>();
render(<AMapMap ref={$ref} />);

const overlay = {};
$ref.current.remove(overlay);

expect($ref.current.emit).toBeCalledWith('overlaysRemoved', overlay);
});

test('has children', () => {
const { getByText } = render(<AMapMap><div>123</div></AMapMap>);
getByText('123');
});
});

0 comments on commit 03dc214

Please sign in to comment.