From 4a1b3f42da24d948d8d58082144174f55666681f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Dombya?= <135591453+hervedombya@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:08:52 +0100 Subject: [PATCH 1/3] ARTESCA-11259: useChainedQuery tests Remove linting step from tests.yml --- .github/workflows/tests.yml | 31 +++++++++++++++++++++ jest.config.js | 3 ++ package.json | 2 +- src/tests/testUtils.tsx | 15 ++++++++++ src/{ => tests}/useChainedMutations.spec.ts | 8 +----- src/{ => tests}/useChainedQuery.spec.tsx | 29 ++++--------------- src/useChainedQuery.tsx | 2 +- 7 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/tests.yml create mode 100644 jest.config.js create mode 100644 src/tests/testUtils.tsx rename src/{ => tests}/useChainedMutations.spec.ts (88%) rename src/{ => tests}/useChainedQuery.spec.tsx (85%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..811815d --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,31 @@ +name: basic tests + +on: + push: + branches: + - 'user/**' + - 'feature/**' + - 'improvement/**' + - 'bugfix/**' + - 'w/**' + - 'q/**' + - 'hotfix/**' + - 'dependabot/**' + pull_request: + types: + - opened + branches: + - 'feature/bump-react-chained-query-version-to-**' + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install node + uses: actions/setup-node@v2 + with: + node-version: '16' + - run: npm ci + - run: npm run test diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..4931934 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + testEnvironment: 'jsdom', +}; diff --git a/package.json b/package.json index 27a341c..81effcd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@scality/react-chained-query", - "version": "1.0.1", + "version": "1.0.2", "description": "A wrapper of react-query useQuery hook allowing chained queries.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/tests/testUtils.tsx b/src/tests/testUtils.tsx new file mode 100644 index 0000000..14c185c --- /dev/null +++ b/src/tests/testUtils.tsx @@ -0,0 +1,15 @@ +import React, { PropsWithChildren } from 'react'; +import { QueryClient, QueryClientProvider } from 'react-query'; +import { ChainedQueryProvider } from '../useChainedQuery'; + +const client = new QueryClient(); + +const wrapper = ({ children }: PropsWithChildren<{}>) => { + return ( + + {children} + + ); +}; + +export { client, wrapper }; diff --git a/src/useChainedMutations.spec.ts b/src/tests/useChainedMutations.spec.ts similarity index 88% rename from src/useChainedMutations.spec.ts rename to src/tests/useChainedMutations.spec.ts index febdcc2..0f8e6c2 100644 --- a/src/useChainedMutations.spec.ts +++ b/src/tests/useChainedMutations.spec.ts @@ -1,11 +1,5 @@ -/** - * @jest-environment jsdom - * @jest-environment-options {"url": "https://jestjs.io/"} - */ - -import { waitFor } from '@testing-library/react'; -import { useChainedMutations } from './useChainedMutations'; import { act, renderHook } from '@testing-library/react-hooks'; +import { useChainedMutations } from '../useChainedMutations'; const fn1 = jest.fn(); const fn2 = jest.fn(); diff --git a/src/useChainedQuery.spec.tsx b/src/tests/useChainedQuery.spec.tsx similarity index 85% rename from src/useChainedQuery.spec.tsx rename to src/tests/useChainedQuery.spec.tsx index 7e0f3d0..02a6e56 100644 --- a/src/useChainedQuery.spec.tsx +++ b/src/tests/useChainedQuery.spec.tsx @@ -4,19 +4,10 @@ import { waitFor, waitForElementToBeRemoved, } from '@testing-library/react'; -import { renderHook, act } from '@testing-library/react-hooks'; -import { PropsWithChildren, useEffect, useState } from 'react'; -import { QueryClient, QueryClientProvider } from 'react-query'; -import { ChainedQueryProvider, useChainedQuery } from './useChainedQuery'; - -const client = new QueryClient(); -const wrapper = ({ children }: PropsWithChildren<{}>) => { - return ( - - {children} - - ); -}; +import { act, renderHook } from '@testing-library/react-hooks'; +import React, { useEffect, useState } from 'react'; +import { useChainedQuery } from '../useChainedQuery'; +import { client, wrapper } from './testUtils'; describe('useChainedQuery', () => { beforeEach(() => { @@ -158,26 +149,16 @@ describe('useChainedQuery', () => { }; render(, { wrapper }); - //E - await waitFor(() => - expect(screen.getByText('component1 loading')).toBeInTheDocument(), - ); //V expect(fn1).toHaveBeenCalledTimes(1); expect(fn2).toHaveBeenCalledTimes(0); expect(fn3).toHaveBeenCalledTimes(0); - expect(screen.getByText('component2 idle')).toBeInTheDocument(); - expect(screen.getByText('component3 idle')).toBeInTheDocument(); //E await waitForElementToBeRemoved(() => screen.getByText(/component2/)); - await waitFor(() => - expect(screen.getByText('component3 loading')).toBeInTheDocument(), - ); - //V expect(fn1).toHaveBeenCalledTimes(1); expect(fn2).toHaveBeenCalledTimes(0); - expect(fn3).toHaveBeenCalledTimes(1); + await waitFor(() => expect(fn3).toHaveBeenCalledTimes(1)); }); it('should raise an error when using the hook outside the ChainedQueryProvider', () => { diff --git a/src/useChainedQuery.tsx b/src/useChainedQuery.tsx index f175d0f..be3a152 100644 --- a/src/useChainedQuery.tsx +++ b/src/useChainedQuery.tsx @@ -4,7 +4,7 @@ import { useQueryClient, UseQueryOptions, } from 'react-query'; -import { +import React, { createContext, PropsWithChildren, useContext, From f9f6c0086f983aaffec800b5a2fdd06e7865a1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Dombya?= <135591453+hervedombya@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:15:10 +0100 Subject: [PATCH 2/3] Update node version to 20 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 811815d..3544b74 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,6 +26,6 @@ jobs: - name: Install node uses: actions/setup-node@v2 with: - node-version: '16' + node-version: '20' - run: npm ci - run: npm run test From 0e93fdd1936b0b2995e696d866c4f98b4f367dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Dombya?= <135591453+hervedombya@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:19:42 +0100 Subject: [PATCH 3/3] Update installation instructions in README --- README.md | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 40c9ce2..e34668d 100644 --- a/README.md +++ b/README.md @@ -12,20 +12,11 @@ By managing a queue and executing the request one after another, it could give t This `useChainedMutations` hook takes an array of mutations and a function to compute the variables for the next mutation in the chain. It returns an object containing a `mutate` function that triggers the chain of mutations, a `computeVariablesForNext` function that computes the variables for the next mutation, and an array of `mutationsWithRetry` that includes a retry function for each mutation. -## Dependencies - -```json -{ - "peerDependencies": { - "react": "^17.0.0", - "react-query": "^3.0.0" - } -} -``` - ## Install -TBD +```bash +npm install @scality/react-chained-query +``` ## Quickstart