Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into upgrade-version
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsJPeschel committed Apr 8, 2024
2 parents aa9fa1f + 12180f8 commit 67a7f22
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 84 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/quality-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ jobs:
- name: Run Unit Tests
run: yarn jest

format:
runs-on: ubuntu-latest
steps:
- name: Setup
uses: Optum/react-hooks/.github/actions/setup@main

- name: Run format checks
run: yarn format:check


Binary file not shown.
Binary file not shown.
7 changes: 2 additions & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript'
],
};
presets: [['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript']
};
2 changes: 1 addition & 1 deletion hooks/useCommonContext/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useCommonContext';
export * from './useCommonContext';
17 changes: 10 additions & 7 deletions hooks/useCommonContext/useCommonContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export function useCommonContext<T>(...contextHooks: (() => T)[]): T {
const context = contextHooks.reduce((prev, useContext) => {
try {
return useContext();
} catch {
return prev;
}
}, undefined as T | undefined);
const context = contextHooks.reduce(
(prev, useContext) => {
try {
return useContext();
} catch {
return prev;
}
},
undefined as T | undefined
);

if (!context) {
throw new Error('contexts not available');
Expand Down
2 changes: 1 addition & 1 deletion hooks/useFocus/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useFocus';
export * from './useFocus';
4 changes: 2 additions & 2 deletions hooks/useLoadData/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './useLoadData'
export * from './types';
export * from './useLoadData';
export * from './types';
108 changes: 54 additions & 54 deletions hooks/useLoadData/useLoadData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,61 +385,61 @@ describe('useLoadData', () => {
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with normal dep', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>('a');
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep('b'));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);

expect(getSuccess).toHaveBeenCalledWith('b');
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>('a');
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with finished dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>({...successfulResponse, result: 'a'});
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep('b'));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);

expect(getSuccess).toHaveBeenCalledWith('b');
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with finished dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>({...successfulResponse, result: 'a'});
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with pending dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>(pendingResponse);
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);
await act(() => result.current.setDep({...successfulResponse, result: 'a'}));

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
expect(result.current.loadedData.isInProgress).toBe(true);

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
});

it('should re-invoke fetchData when fetchWhenDepsChange with an initial promise with pending dependency', async () => {
const {result} = renderHook(() => {
const [dep, setDep] = useState<any>(pendingResponse);
const loadedData = useLoadData(getSuccess, [dep], {fetchWhenDepsChange: true});

return {loadedData, setDep};
});
expect(result.current.loadedData.isInProgress).toBe(true);
await act(() => result.current.setDep({...successfulResponse, result: 'a'}));

await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledWith('a');
expect(getSuccess).toHaveBeenCalledTimes(1);

await act(() => result.current.setDep({...successfulResponse, result: 'b'}));
await waitFor(() => expect(result.current.loadedData.isInProgress).toBe(false));
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
});
});
2 changes: 1 addition & 1 deletion hooks/useOptionalDependency/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useOptionalDependency';
export * from './useOptionalDependency';
2 changes: 1 addition & 1 deletion hooks/useRetry/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useRetry';
export * from './useRetry';
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"sideEffects": false,
"scripts": {
"build": "tsc --project tsconfig.json",
"prepack": "yarn build"
"prepack": "yarn build",
"format": "yarn format:nowrite --write",
"format:nowrite": "prettier --ignore-path .gitignore \"**/*.+(js|ts|tsx|html|css|scss|json)\"",
"format:check": "yarn format:nowrite --list-different"
},
"files": [
"build/**"
Expand All @@ -25,7 +28,7 @@
"eslint": "^8.28.0",
"jest": "^27.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"prettier": "^3.2.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-jest": "^27.0.5",
Expand Down
9 changes: 9 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
singleQuote: true,
printWidth: 120,
bracketSpacing: false,
arrowParens: 'always',
tabWidth: 2,
trailingComma: 'none',
endOfLine: 'auto'
};
6 changes: 3 additions & 3 deletions test-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const noop = () => {};
if(typeof window !== 'undefined') {
Object.defineProperty(window, 'scrollTo', {value: noop, writable: true})
}
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'scrollTo', {value: noop, writable: true});
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "Node16",
"moduleResolution":"Node16",
"moduleResolution": "Node16",
"preserveSymlinks": true,
"rootDir": ".",
"outDir": "build/cjs",
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ __metadata:
eslint: ^8.28.0
jest: ^27.2.1
npm-run-all: ^4.1.5
prettier: ^2.3.2
prettier: ^3.2.5
react: ^18.2.0
react-dom: ^18.2.0
ts-jest: ^27.0.5
Expand Down Expand Up @@ -5888,12 +5888,12 @@ __metadata:
languageName: node
linkType: hard

"prettier@npm:^2.3.2":
version: 2.8.7
resolution: "prettier@npm:2.8.7"
"prettier@npm:^3.2.5":
version: 3.2.5
resolution: "prettier@npm:3.2.5"
bin:
prettier: bin-prettier.js
checksum: fdc8f2616f099f5f0d685907f4449a70595a0fc1d081a88919604375989e0d5e9168d6121d8cc6861f21990b31665828e00472544d785d5940ea08a17660c3a6
prettier: bin/prettier.cjs
checksum: 2ee4e1417572372afb7a13bb446b34f20f1bf1747db77cf6ccaf57a9be005f2f15c40f903d41a6b79eec3f57fff14d32a20fb6dee1f126da48908926fe43c311
languageName: node
linkType: hard

Expand Down

0 comments on commit 67a7f22

Please sign in to comment.