Skip to content

Instantly share code, notes, and snippets.

@andiwinata
andiwinata / enzyme-vs-react-testing-library-use-effect.tsx
Created December 16, 2019 03:15
Enzyme vs react testing library on calling useEffect
export const usePolling = ({ pollFn, interval }: Params, deps: any[]) => {
useEffect(() => {
let timeout: number | undefined;
let isMounted = true;
const poller = async () => {
await pollFn();
// only do next polling if the function is still mounted
// i.e. it hasn't been replaced by new function because `deps` change
import { Action } from 'redux';
import * as plansActions from './plans/actions';
import * as planDetailsActions from './planDetails/actions';
import { reducers } from './reducer';
export type StoreState = Readonly<{ [R in keyof typeof reducers]: ReturnType<typeof reducers[R]> }>;
// from object containing { [actionName]: [action] }
// then for every action, if the action itself extending AnyAction -> append it to the object
// otherwise, if the action is a function and returning AnyAction -> append it to the object
import { render, fireEvent, waitForElementToBeRemoved, prettyDOM } from '@testing-library/react';
const { getByText, queryByText } = render(
<Component />
);
const button = getByText(/Remove [0-9]+ projects? from this migration/);
fireEvent.click(button);
// click the remove project button
@andiwinata
andiwinata / mockSpecificFunctionJest.ts
Last active November 12, 2019 00:46
Mock specific function in jest
import { useCallback } from 'react';
jest.mock('react', () => ({
...require.requireActual('react'),
useCallback: jest.fn(),
}));
const useCallbackSpy = useCallback as jest.Mock<typeof useCallback>;
@andiwinata
andiwinata / functional-implementation.js
Created April 1, 2019 10:09
Implementation of curry is es6
// Curry is only taking 1 parameter different from partial application
curry = fn => {
const self = params => arg => {
const nextParams = [...params, arg]
if (nextParams.length === fn.length) return fn(...nextParams);
return self(nextParams);
}
return self([])
}
@andiwinata
andiwinata / snippet.js
Created March 10, 2019 05:20
Helpful snippets for chrome
(() => {
const convertCallback = (cb, value, status, defaultLetter) => {
if (typeof cb === 'function') {
cb(value);
} else if (typeof cb === 'string' || typeof cb === 'undefined') {
const letter = typeof cb === 'string' ? cb : defaultLetter;
window[letter] = value;
console.log(`request ${status}, stored in variable: ${letter}, below is the value:`);
console.log(value);