-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supports reloading beforeSend for next/react/remix (#83)
- Loading branch information
Showing
7 changed files
with
121 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ''; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
"compilerOptions": { | ||
"module": "esnext" | ||
}, | ||
"include": ["src"] | ||
"include": ["src", "./jest.setup.ts"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.