From 342187076a80fccbb1facc6a1dd9faa6c4d43b72 Mon Sep 17 00:00:00 2001 From: Alex Hoffer Date: Thu, 23 Jul 2026 18:34:06 -0700 Subject: [PATCH] Test React 16 through 19 compatibility --- .github/workflows/build.yml | 19 +++++++++++--- src/react-script-hook/index.test.tsx | 37 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d0688c52..c024f628 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,7 +21,19 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - react: [18, 19] + include: + - react: 16.8.6 + react-types: 16 + testing-library: 12.1.5 + - react: 17.0.2 + react-types: 17 + testing-library: 12.1.5 + - react: 18.3.1 + react-types: 18 + testing-library: 16.3.2 + - react: 19.2.8 + react-types: 19 + testing-library: 16.3.2 steps: - uses: actions/checkout@v7 @@ -37,6 +49,7 @@ jobs: yarn add --dev --ignore-scripts --no-lockfile react@${{ matrix.react }} react-dom@${{ matrix.react }} - @types/react@${{ matrix.react }} - @types/react-dom@${{ matrix.react }} + @types/react@${{ matrix.react-types }} + @types/react-dom@${{ matrix.react-types }} + @testing-library/react@${{ matrix.testing-library }} - run: make typecheck test diff --git a/src/react-script-hook/index.test.tsx b/src/react-script-hook/index.test.tsx index 4ee99958..7c3d9696 100644 --- a/src/react-script-hook/index.test.tsx +++ b/src/react-script-hook/index.test.tsx @@ -1,7 +1,36 @@ -import { act, renderHook, waitFor } from '@testing-library/react'; +import React from 'react'; +import { act, render } from '@testing-library/react'; import useScript, { scripts } from './'; +const renderHook = ( + callback: (props: Props) => Result, + options?: { initialProps: Props }, +) => { + let current!: Result; + + const Hook: React.FC<{ hookProps: Props }> = ({ hookProps }) => { + current = callback(hookProps); + return null; + }; + + const initialProps = options + ? options.initialProps + : (undefined as unknown as Props); + const handle = render(); + + return { + result: { + get current() { + return current; + }, + }, + rerender: (props: Props) => + handle.rerender(), + unmount: handle.unmount, + }; +}; + describe('useScript', () => { beforeEach(() => { const html = document.querySelector('html'); @@ -283,7 +312,7 @@ describe('useScript', () => { expect(document.querySelectorAll('script').length).toBe(0); }); - it('should append script after src change from null', async () => { + it('should append script after src change from null', () => { expect(document.querySelectorAll('script').length).toBe(0); const props = { src: null }; @@ -300,9 +329,7 @@ describe('useScript', () => { props.src = 'http://scriptsrc/' as any; rerender(props); - await waitFor(() => { - expect(document.querySelectorAll('script').length).toBe(1); - }); + expect(document.querySelectorAll('script').length).toBe(1); }); it('should remove script from DOM and scripts cache when unmounted during loading', () => {