Skip to content

Instantly share code, notes, and snippets.

View vinnymac's full-sized avatar
🤓

Vincent Taverna vinnymac

🤓
View GitHub Profile
@vinnymac
vinnymac / README.md
Created September 13, 2022 03:10
Chiaki helper script to determine Account ID from any modern browser

Instructions

  1. Open the browser of your choice
  2. Visit https://playstation.com (and sign out if you are signed in)
  3. Open the developer tools for your browser, and click on the network activity tab
  4. Choose to preserve the network log on the network tab

image

  1. Now sign back into the playstation site
  2. Search the network activity for requests named id, and click on one of them
@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);
@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 / 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 / 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 / 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 / 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 / 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 / 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 / flushSync.ts
Created February 1, 2023 03:27
React 18 Flush Sync
import { flushSync } from 'react-dom';
flushSync(() => {
currentEl.focus();
setFocused(false);
});