Skip to content

Instantly share code, notes, and snippets.

View SimonMueller's full-sized avatar
💭
🎉

Simon Müller SimonMueller

💭
🎉
  • codeto.ch
  • Switzerland
View GitHub Profile
@SimonMueller
SimonMueller / combineWrappers.tsx
Created January 30, 2023 13:20
React Testing Library combine wrappers
type Wrapper = (props: { children: JSX.Element }) => JSX.Element;
export function combineWrappers(...wrappers: Wrapper[]): Wrapper {
return function wrapper({ children }: { children: JSX.Element }) {
return wrappers.reduce((acc, curr) => {
const CurrentWrapper = curr;
return <CurrentWrapper>{acc}</CurrentWrapper>;
}, children);
};
@SimonMueller
SimonMueller / typed-filter-empty-values.ts
Last active July 18, 2022 14:20
Typed filtering of empty values
export default function filterEmptyValues<T>(list: (T | null | undefined)[]): T[] {
return list.reduce<T[]>(
(acc: T[], curr: T | null | undefined) =>
curr !== undefined && curr !== null ? [...acc, curr] : acc,
[]
);
}
@SimonMueller
SimonMueller / typed-react-hoc.tsx
Last active January 8, 2023 14:30
Typed HOC for React
import React, { ComponentType } from "react";
export type ConfigProps = {
config: string;
}
export function withConfig<T>(WrappedComponent: ComponentType<T & ConfigProps>) {
return function Wrapper(props: T) {
const config = 'some config';
@SimonMueller
SimonMueller / TextSecondTimer.test.tsx
Last active February 11, 2022 09:20
Simple text timer in seconds written in React with Typescript
import React from 'react';
import { render, act } from '@testing-library/react';
import TextSecondTimer from './TextSecondTimer';
describe('Text second timer component', () => {
beforeEach(() => jest.useFakeTimers());
it('shows zero if timeout is zero', () => {
const { getByText } = render(<TextSecondTimer timeoutInSeconds={0} />);