Skip to content

Commit

Permalink
feat: supports reloading beforeSend for next/react/remix (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy authored Nov 7, 2024
1 parent 1d1845e commit 857f881
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 119 deletions.
9 changes: 7 additions & 2 deletions packages/web/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { beforeEach } from '@jest/globals';
import '@testing-library/jest-dom';
// Adds helpers like `.toHaveAttribute`
import '@testing-library/jest-dom/jest-globals';

beforeEach(() => {
if ('document' in global) {
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- there is an HTML element */
document.querySelector('html')!.innerHTML = '';
const html = document.querySelector('html');
if (html) {
html.innerHTML = '';
}
}
});
5 changes: 2 additions & 3 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@
"@sveltejs/kit": "^2.7",
"@swc/core": "^1.3.103",
"@swc/jest": "^0.2.29",
"@testing-library/jest-dom": "^6.2.0",
"@testing-library/react": "^14.1.2",
"@types/jest": "^29.5.11",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@types/node": "^20.11.4",
"@types/react": "^18.2.48",
"copyfiles": "^2.4.1",
Expand Down
11 changes: 10 additions & 1 deletion packages/web/src/generic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from '@jest/globals';
import { injectSpeedInsights } from './generic';
import { injectSpeedInsights, type SpeedInsightsProps } from './generic';

describe('injectSpeedInsights()', () => {
it('allows no parameters', () => {
Expand All @@ -16,6 +16,15 @@ describe('injectSpeedInsights()', () => {
});
expectInjectedScript({ framework });
});

it('can set beforeSend', () => {
const beforeSend: Required<SpeedInsightsProps>['beforeSend'] = (event) =>
event;
injectSpeedInsights({ beforeSend });

expect(window.siq?.[0]).toEqual(['beforeSend', beforeSend]);
expect(window.siq).toHaveLength(1);
});
});

function expectInjectedScript({
Expand Down
64 changes: 64 additions & 0 deletions packages/web/src/react/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import { afterEach, beforeEach, describe, it, expect } from '@jest/globals';
import { cleanup, render } from '@testing-library/react';
import type { SpeedInsightsProps } from '../types';
import { SpeedInsights } from '.';

describe('<SpeedInsights />', () => {
afterEach(() => {
cleanup();
});

describe.each([
{
mode: 'development',
file: 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js',
},
{
mode: 'production',
file: 'http://localhost/_vercel/speed-insights/script.js',
},
])('in $mode mode', ({ mode, file }) => {
const env = process.env.NODE_ENV;
beforeEach(() => {
process.env.NODE_ENV = mode;
window.si = undefined;
window.siq = undefined;
});

afterEach(() => {
process.env.NODE_ENV = env;
});

it('adds the script tag correctly', () => {
render(<SpeedInsights />);

const scripts = document.getElementsByTagName('script');
expect(scripts).toHaveLength(1);

const script = document.head.querySelector('script');
expect(script).toBeDefined();
expect(script?.src).toEqual(file);
expect(script).toHaveAttribute('defer');
});

it('sets and changes beforeSend', () => {
const beforeSend: Required<SpeedInsightsProps>['beforeSend'] = (event) =>
event;
const beforeSend2: Required<SpeedInsightsProps>['beforeSend'] = (event) =>
event;
const { rerender } = render(<SpeedInsights beforeSend={beforeSend} />);

expect(window.siq?.[0]).toEqual(['beforeSend', beforeSend]);
expect(window.siq).toHaveLength(1);
window.siq?.splice(0, 1);

rerender(<SpeedInsights beforeSend={beforeSend} />);
expect(window.siq).toHaveLength(0);

rerender(<SpeedInsights beforeSend={beforeSend2} />);
expect(window.siq?.[0]).toEqual(['beforeSend', beforeSend2]);
expect(window.siq).toHaveLength(1);
});
});
});
4 changes: 4 additions & 0 deletions packages/web/src/react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function SpeedInsights(
framework?: string;
},
): JSX.Element | null {
useEffect(() => {
window.si?.('beforeSend', props.beforeSend);
}, [props.beforeSend]);

const setScriptRoute = useRef<((path: string) => void) | null>(null);
useEffect(() => {
if (!setScriptRoute.current) {
Expand Down
2 changes: 1 addition & 1 deletion packages/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"module": "esnext"
},
"include": ["src"]
"include": ["src", "./jest.setup.ts"]
}
145 changes: 33 additions & 112 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 857f881

Please sign in to comment.