Skip to content

Commit a6b85ad

Browse files
committed
test: Prop Name test
1 parent bfc84c1 commit a6b85ad

File tree

10 files changed

+161
-417
lines changed

10 files changed

+161
-417
lines changed

docs/examples/immutable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
createContext,
33
makeImmutable,
44
responseImmutable,
5-
useContextSelector,
5+
useContext,
66
} from '@rc-component/context';
77
import React from 'react';
88
import useRenderTimes from './useRenderTimes';
@@ -34,7 +34,7 @@ const ImmutableMyApp = makeImmutable(MyApp);
3434

3535
const MyComponent = ({ name }: { name: any }) => {
3636
const renderTimes = useRenderTimes();
37-
const value = useContextSelector(AppContext, name);
37+
const value = useContext(AppContext, name);
3838

3939
return (
4040
<div>

docs/examples/simple.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContextSelector } from '@rc-component/context';
1+
import { createContext, useContext } from '@rc-component/context';
22
import React from 'react';
33
import useRenderTimes from './useRenderTimes';
44

@@ -8,7 +8,7 @@ const CountContext = createContext<{
88
}>();
99

1010
const MyConsumer = React.memo(({ name }: { name: any }) => {
11-
const value = useContextSelector(CountContext, name);
11+
const value = useContext(CountContext, name);
1212
const renderTimes = useRenderTimes();
1313

1414
return (

src/context.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,41 @@ export function createContext<ContextProps>(
5555
return { Context, Provider };
5656
}
5757

58+
/** e.g. useSelect(userContext) => user */
59+
export function useContext<ContextProps>(
60+
holder: SelectorContext<ContextProps>,
61+
): ContextProps;
62+
5863
/** e.g. useSelect(userContext, user => user.name) => user.name */
59-
export function useContextSelector<ContextProps, SelectorValue>(
64+
export function useContext<ContextProps, SelectorValue>(
6065
holder: SelectorContext<ContextProps>,
6166
selector: Selector<ContextProps, SelectorValue>,
6267
): SelectorValue;
6368

6469
/** e.g. useSelect(userContext, ['name', 'age']) => user { name, age } */
65-
export function useContextSelector<ContextProps, SelectorValue extends Partial<ContextProps>>(
70+
export function useContext<ContextProps, SelectorValue extends Partial<ContextProps>>(
6671
holder: SelectorContext<ContextProps>,
6772
selector: (keyof ContextProps)[],
6873
): SelectorValue;
6974

7075
/** e.g. useSelect(userContext, 'name') => user.name */
71-
export function useContextSelector<ContextProps, PropName extends keyof ContextProps>(
76+
export function useContext<ContextProps, PropName extends keyof ContextProps>(
7277
holder: SelectorContext<ContextProps>,
7378
selector: PropName,
7479
): ContextProps[PropName];
7580

76-
export function useContextSelector<ContextProps, SelectorValue>(
81+
export function useContext<ContextProps, SelectorValue>(
7782
holder: SelectorContext<ContextProps>,
78-
selector: Selector<ContextProps, any> | (keyof ContextProps)[] | keyof ContextProps,
83+
selector?: Selector<ContextProps, any> | (keyof ContextProps)[] | keyof ContextProps,
7984
) {
8085
const eventSelector = useEvent<Selector<ContextProps, SelectorValue>>(
8186
typeof selector === 'function'
8287
? selector
8388
: ctx => {
89+
if (selector === undefined) {
90+
return ctx;
91+
}
92+
8493
if (!Array.isArray(selector)) {
8594
return ctx[selector];
8695
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SelectorContext } from './context';
2-
import { createContext, useContextSelector } from './context';
2+
import { createContext, useContext } from './context';
33
import { makeImmutable, responseImmutable } from './Immutable';
44

5-
export { createContext, useContextSelector, makeImmutable, responseImmutable };
5+
export { createContext, useContext, makeImmutable, responseImmutable };
66
export type { SelectorContext };

tests/common.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
3+
export const useRenderTimes = () => {
4+
const renderRef = React.useRef(0);
5+
renderRef.current += 1;
6+
7+
return renderRef.current;
8+
};
9+
10+
export function RenderTimer({ id }: { id?: string }) {
11+
const renderTimes = useRenderTimes();
12+
13+
return (
14+
<div id={id} className="render-times">
15+
{renderTimes}
16+
</div>
17+
);
18+
}
19+
20+
export function Value({ id, value }: { id?: string; value: any }) {
21+
const str = JSON.stringify(value);
22+
23+
return (
24+
<div id={id} className="value">
25+
{str.replace(/^"/, '').replace(/"$/, '')}
26+
</div>
27+
);
28+
}

tests/context.test.tsx

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { fireEvent, render } from '@testing-library/react';
2+
import React from 'react';
3+
import { createContext, useContext } from '../src';
4+
import { RenderTimer, Value } from './common';
5+
6+
describe('Basic', () => {
7+
interface User {
8+
name: string;
9+
age: number;
10+
}
11+
12+
const UserContext = createContext<User>();
13+
14+
const Root = ({ children }: { children?: React.ReactNode }) => {
15+
const [name, setName] = React.useState('bamboo');
16+
const [age, setAge] = React.useState(30);
17+
18+
return (
19+
<UserContext.Provider value={{ name, age }}>
20+
<input id="name" value={name} onChange={e => setName(e.target.value)} />
21+
<input id="age" value={age} onChange={e => setAge(Number(e.target.value))} />
22+
23+
{children}
24+
</UserContext.Provider>
25+
);
26+
};
27+
28+
function changeValue(container: HTMLElement, id: string, value: string) {
29+
fireEvent.change(container.querySelector(`#${id}`), {
30+
target: {
31+
value,
32+
},
33+
});
34+
}
35+
36+
it('raw', () => {
37+
const Raw = () => {
38+
const user = useContext(UserContext);
39+
40+
return (
41+
<>
42+
<Value id="value" value={user} />
43+
<RenderTimer id="raw" />
44+
</>
45+
);
46+
};
47+
48+
const { container } = render(
49+
<Root>
50+
<Raw />
51+
</Root>,
52+
);
53+
54+
// Mount
55+
expect(container.querySelector('#raw')!.textContent).toEqual('1');
56+
57+
// Update `name`: Full Update
58+
changeValue(container, 'name', 'light');
59+
expect(container.querySelector('#raw')!.textContent).toEqual('2');
60+
expect(container.querySelector('#value')!.textContent).toEqual(
61+
JSON.stringify({
62+
name: 'light',
63+
age: 30,
64+
}),
65+
);
66+
67+
// Update `age`: Full Update
68+
changeValue(container, 'age', '20');
69+
expect(container.querySelector('#raw')!.textContent).toEqual('3');
70+
expect(container.querySelector('#value')!.textContent).toEqual(
71+
JSON.stringify({
72+
name: 'light',
73+
age: 20,
74+
}),
75+
);
76+
});
77+
78+
it('PropName', () => {
79+
const PropName = ({ name }: { name: keyof User }) => {
80+
const value = useContext(UserContext, name);
81+
82+
return (
83+
<>
84+
<Value id={`${name}-value`} value={value} />
85+
<RenderTimer id={`${name}-times`} />
86+
</>
87+
);
88+
};
89+
90+
const { container } = render(
91+
<Root>
92+
<PropName name="name" />
93+
<PropName name="age" />
94+
</Root>,
95+
);
96+
97+
// Mount
98+
expect(container.querySelector('#name-times')!.textContent).toEqual('1');
99+
expect(container.querySelector('#age-times')!.textContent).toEqual('1');
100+
101+
// Update `name`: Partial Update
102+
changeValue(container, 'name', 'light');
103+
expect(container.querySelector('#name-times')!.textContent).toEqual('2');
104+
expect(container.querySelector('#name-value')!.textContent).toEqual('light');
105+
expect(container.querySelector('#age-times')!.textContent).toEqual('1');
106+
107+
// Update `age`: Partial Update
108+
changeValue(container, 'age', '20');
109+
expect(container.querySelector('#name-times')!.textContent).toEqual('2');
110+
expect(container.querySelector('#age-times')!.textContent).toEqual('2');
111+
expect(container.querySelector('#age-value')!.textContent).toEqual('20');
112+
});
113+
});

tests/element.test.tsx

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)