Skip to content

Commit 7adbec7

Browse files
committed
chore: optimize test cases and documentation
1 parent 1082b46 commit 7adbec7

File tree

6 files changed

+14
-18
lines changed

6 files changed

+14
-18
lines changed

.umirc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { defineConfig } from 'dumi';
33

44
export default defineConfig({
5-
title: 'rc-util',
5+
title: 'rc-mutate-observer',
66
favicon: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4',
77
logo: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4',
88
outputPath: '.doc',

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ We use typescript to create the Type definition. You can view directly in IDE. B
5555
| Prop | Description | Type | Default |
5656
| -------- | ---------------------------------------------------------------------------------------------------------------- | -------------------- | ------- |
5757
| onMutate | A function which will be called on each DOM change that qualifies given the observed node or subtree and options | MutationCallback | - |
58-
| options | An object providing options that describe which DOM mutations should be reported to mutationObserver's callback | MutationObserverInit | - |
58+
| options | An object providing options that describe which DOM mutations should be reported to mutationObserver's callback | MutationObserverInit | { subtree: `true`, childList: `true`, attributeFilter: `['style', 'class']` } |

docs/examples/basic.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import MutateObserver from '../../src';
2-
import React, { useCallback } from 'react';
2+
import React from 'react';
33

44
const App: React.FC = () => {
55
const [flag, setFlag] = React.useState<boolean>(true);
66

7-
const onMutate = useCallback((mutations: MutationRecord[]) => {
7+
const onMutate = (mutations: MutationRecord[]) => {
88
console.log(mutations);
9-
}, []);
9+
};
1010

1111
return (
1212
<MutateObserver onMutate={onMutate}>

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
3737
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
3838
"start": "dumi dev",
39-
"test": "umi-test",
40-
"test:coverage": "npm run test --coverage",
39+
"test": "rc-test",
40+
"test:coverage": "rc-test --coverage",
4141
"watch": "father dev"
4242
},
4343
"dependencies": {
@@ -59,10 +59,10 @@
5959
"gh-pages": "^3.1.0",
6060
"np": "^5.0.3",
6161
"prettier": "^2.1.2",
62+
"rc-test": "^7.0.14",
6263
"react": "^18.0.0",
6364
"react-dom": "^18.0.0",
64-
"typescript": "^4.6.3",
65-
"umi-test": "^1.9.7"
65+
"typescript": "^4.6.3"
6666
},
6767
"peerDependencies": {
6868
"react": ">=16.9.0",

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import useEvent from 'rc-util/lib/hooks/useEvent';
66
import DomWrapper from './wrapper';
77
import type { MutationObserverProps } from './interface';
88

9-
const defOptions: MutationObserverInit = {
9+
const defaultOptions: MutationObserverInit = {
1010
subtree: true,
1111
childList: true,
1212
attributeFilter: ['style', 'class'],
1313
};
1414

1515
const MutateObserver: React.FC<MutationObserverProps> = props => {
16-
const { children, options = defOptions, onMutate = () => {} } = props;
16+
const { children, options = defaultOptions, onMutate = () => {} } = props;
1717

1818
const callback = useEvent(onMutate);
1919

tests/index.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
2-
import { fireEvent, render } from '@testing-library/react';
2+
import { fireEvent, render, waitFor } from '@testing-library/react';
33
import MutateObserver from '../src';
44

55
describe('MutateObserver', () => {
6-
it('MutateObserver should support onMutate', () => {
6+
it('MutateObserver should support onMutate', async () => {
77
const fn = jest.fn();
88
const Demo: React.FC = () => {
99
const [flag, setFlag] = React.useState<boolean>(true);
@@ -20,11 +20,7 @@ describe('MutateObserver', () => {
2020
};
2121
const { container, unmount } = render(<Demo />);
2222
fireEvent.click(container.querySelector('button')!);
23-
if ('MutationObserver' in window) {
24-
expect(fn).toHaveBeenCalled();
25-
} else {
26-
expect(fn).not.toHaveBeenCalled();
27-
}
23+
await waitFor(() => expect(fn).toHaveBeenCalled());
2824
unmount();
2925
});
3026

0 commit comments

Comments
 (0)