diff --git a/.github/workflows/quality-checks.yml b/.github/workflows/quality-checks.yml index 5a9ad92..91f36dc 100644 --- a/.github/workflows/quality-checks.yml +++ b/.github/workflows/quality-checks.yml @@ -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 + diff --git a/.yarn/cache/prettier-npm-2.8.7-1962ac9483-fdc8f2616f.zip b/.yarn/cache/prettier-npm-2.8.7-1962ac9483-fdc8f2616f.zip deleted file mode 100644 index ef951de..0000000 Binary files a/.yarn/cache/prettier-npm-2.8.7-1962ac9483-fdc8f2616f.zip and /dev/null differ diff --git a/.yarn/cache/prettier-npm-3.2.5-6859110d6a-2ee4e14175.zip b/.yarn/cache/prettier-npm-3.2.5-6859110d6a-2ee4e14175.zip new file mode 100644 index 0000000..b9085d2 Binary files /dev/null and b/.yarn/cache/prettier-npm-3.2.5-6859110d6a-2ee4e14175.zip differ diff --git a/babel.config.js b/babel.config.js index bb4a4ab..8f12aad 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,6 +1,3 @@ module.exports = { - presets: [ - ['@babel/preset-env', {targets: {node: 'current'}}], - '@babel/preset-typescript' - ], -}; \ No newline at end of file + presets: [['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript'] +}; diff --git a/hooks/useCommonContext/index.ts b/hooks/useCommonContext/index.ts index 0754830..55a3f39 100644 --- a/hooks/useCommonContext/index.ts +++ b/hooks/useCommonContext/index.ts @@ -1 +1 @@ -export * from './useCommonContext'; \ No newline at end of file +export * from './useCommonContext'; diff --git a/hooks/useCommonContext/useCommonContext.ts b/hooks/useCommonContext/useCommonContext.ts index 92e801a..83691f2 100644 --- a/hooks/useCommonContext/useCommonContext.ts +++ b/hooks/useCommonContext/useCommonContext.ts @@ -1,11 +1,14 @@ export function useCommonContext(...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'); diff --git a/hooks/useFocus/index.ts b/hooks/useFocus/index.ts index 5a0ff50..fb62693 100644 --- a/hooks/useFocus/index.ts +++ b/hooks/useFocus/index.ts @@ -1 +1 @@ -export * from './useFocus'; \ No newline at end of file +export * from './useFocus'; diff --git a/hooks/useLoadData/index.ts b/hooks/useLoadData/index.ts index 3661419..3c31c3e 100644 --- a/hooks/useLoadData/index.ts +++ b/hooks/useLoadData/index.ts @@ -1,2 +1,2 @@ -export * from './useLoadData' -export * from './types'; \ No newline at end of file +export * from './useLoadData'; +export * from './types'; diff --git a/hooks/useLoadData/useLoadData.test.ts b/hooks/useLoadData/useLoadData.test.ts index 99d1830..53977f6 100644 --- a/hooks/useLoadData/useLoadData.test.ts +++ b/hooks/useLoadData/useLoadData.test.ts @@ -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('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('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({...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({...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(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(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'); + }); }); diff --git a/hooks/useOptionalDependency/index.ts b/hooks/useOptionalDependency/index.ts index 1e04ef9..884e5b5 100644 --- a/hooks/useOptionalDependency/index.ts +++ b/hooks/useOptionalDependency/index.ts @@ -1 +1 @@ -export * from './useOptionalDependency'; \ No newline at end of file +export * from './useOptionalDependency'; diff --git a/hooks/useRetry/index.ts b/hooks/useRetry/index.ts index bf3a93d..7bf9109 100644 --- a/hooks/useRetry/index.ts +++ b/hooks/useRetry/index.ts @@ -1 +1 @@ -export * from './useRetry'; \ No newline at end of file +export * from './useRetry'; diff --git a/package.json b/package.json index 524c59f..51044a5 100644 --- a/package.json +++ b/package.json @@ -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/**" @@ -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", diff --git a/prettier.config.js b/prettier.config.js new file mode 100644 index 0000000..0273f70 --- /dev/null +++ b/prettier.config.js @@ -0,0 +1,9 @@ +module.exports = { + singleQuote: true, + printWidth: 120, + bracketSpacing: false, + arrowParens: 'always', + tabWidth: 2, + trailingComma: 'none', + endOfLine: 'auto' +}; diff --git a/test-setup.js b/test-setup.js index 65639d4..d92e788 100644 --- a/test-setup.js +++ b/test-setup.js @@ -1,4 +1,4 @@ const noop = () => {}; -if(typeof window !== 'undefined') { - Object.defineProperty(window, 'scrollTo', {value: noop, writable: true}) -} \ No newline at end of file +if (typeof window !== 'undefined') { + Object.defineProperty(window, 'scrollTo', {value: noop, writable: true}); +} diff --git a/tsconfig.json b/tsconfig.json index 7990e6d..f01d869 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "Node16", - "moduleResolution":"Node16", + "moduleResolution": "Node16", "preserveSymlinks": true, "rootDir": ".", "outDir": "build/cjs", diff --git a/yarn.lock b/yarn.lock index 5de2ab3..715c446 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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