Skip to content

Commit

Permalink
fix(config): ConfigContext 默认值应该为空对象,以便子元素获取对象中的配置
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Apr 27, 2024
1 parent 87815df commit 10b9eaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ConfigProvider, useConfig} from '../index';
import {render} from '@testing-library/react';
import { ConfigProvider, useConfig } from '../index';
import { render } from '@testing-library/react';

describe('config', () => {
test('basic', () => {
Expand All @@ -8,11 +8,21 @@ describe('config', () => {
return config.key;
};

const {container} = render(<ConfigProvider config={{
const { container } = render(<ConfigProvider config={{
key: 'value',
}}>
<Child/>
</ConfigProvider>);
expect(container).toMatchSnapshot();
});

test('default value is {}', () => {
const Main = () => {
const config = useConfig();
return Object.keys(config).length;
};

const { container } = render(<Main/>);
expect(container.innerHTML).toBe('0');
});
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useContext, useEffect, useState } from 'react';
import propTypes from 'prop-types';

const ConfigContext = createContext(null);
const ConfigContext = createContext({});

const ConfigProvider = ({config: userConfig = {}, children}) => {
const [config, setConfig] = useState(userConfig);
Expand Down

0 comments on commit 10b9eaf

Please sign in to comment.