Skip to content

Instantly share code, notes, and snippets.

View vinnymac's full-sized avatar
🤓

Vincent Taverna vinnymac

🤓
View GitHub Profile
@vinnymac
vinnymac / installReact18.sh
Created February 1, 2023 03:59
Install React 18
# Install React 18 in the workspace
yarn add react@^18.2.0 react-dom@^18.2.0 -W
# Install react 18 type definitions in the workspace
yarn add -D @types/react@^18.0.24 @types/react-dom@^18.0.8 -W
@vinnymac
vinnymac / flushSync.ts
Created February 1, 2023 03:27
React 18 Flush Sync
import { flushSync } from 'react-dom';
flushSync(() => {
currentEl.focus();
setFocused(false);
});
@vinnymac
vinnymac / errorBoundarySpec.ts
Created February 1, 2023 03:26
React error boundary spec
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import { Account } from './Account';
import { ErrorBoundary } from './ErrorBoundary';
describe('Account', () => {
it('renders a fallback when an account error is thrown', async () => {
const fallback = jest.fn(() => (
@vinnymac
vinnymac / ignoreReact18DOMrenderWarning.ts
Created February 1, 2023 03:24
Ignore React 18 DOM render warning
export function ignoreReact18DOMrenderWarning() {
const originalError = console.error;
beforeAll(() => {
console.error = (...args) => {
if (/Warning: ReactDOM.render is no longer supported in React 18./.test(args[0])) {
return;
}
originalError.call(console, ...args);
};
@vinnymac
vinnymac / mockWindowLocationSpecExample.ts
Created February 1, 2023 03:24
Example spec using mockWindowLocation
import React from 'react';
import { render } from '@testing-library/react';
import AutoRedirect from './AutoRedirect';
import { mockWindowLocation } from './testUtils';
describe('Navigation', () => {
mockWindowLocation();
it('navigates to seatgeek', () => {
render(<AutoRedirect />);
@vinnymac
vinnymac / mockWindowLocation.ts
Created February 1, 2023 03:22
Jest 26 Mock Window Location
// Jest 26 with latest JSDOM prevents location from being proxied
// Instead we can redefine location on window, and then restore.
export function mockWindowLocation(
mockLocation: Partial<Location> = {
href: '',
assign: jest.fn(),
reload: jest.fn(),
}
) {
const savedLocation = window.location;
@vinnymac
vinnymac / actEnvironment.ts
Created February 1, 2023 03:21
React 18 Act Environment
function getGlobalThis() {
return global || globalThis || self || window;
}
export function disableReactActEnv() {
let prev: boolean;
beforeEach(() => {
prev = getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = false;
});
@vinnymac
vinnymac / react18TestingImports.ts
Created February 1, 2023 03:20
React 18 testing imports
// Before
import { waitForNextUpdate, renderHook } from '@testing-library/react-hooks';
// After
import { waitFor, renderHook } from '@testing-library/react';
@vinnymac
vinnymac / react18ChildrenPropType.ts
Created February 1, 2023 03:19
React 18 Children Prop Type
// React 17 or lower
type DropdownProps = {
open: boolean;
}
// React 18 or higher
type DropdownProps = {
open: boolean;
children?: React.ReactNode;
}
@vinnymac
vinnymac / AppWithCallbackAfterRender.tsx
Created February 1, 2023 03:18
React 18 callback after render
import { ReactElement, useLayoutEffect, useRef } from 'react';
export function AppWithCallbackAfterRender({
children,
callback,
}: {
children: ReactElement;
callback: () => void;
}): ReactElement {
const onlyOnce = useRef(false);