Skip to content

Instantly share code, notes, and snippets.

View CarmeloRicarte's full-sized avatar
🏠
Working from home

Carmelo Ricarte Rocamora CarmeloRicarte

🏠
Working from home
  • Murcia, Spain
  • 16:47 (UTC +01:00)
View GitHub Profile
@CarmeloRicarte
CarmeloRicarte / configurar-node-ts.md
Created September 26, 2023 07:20 — forked from Klerith/configurar-node-ts.md
Node con TypeScript - TS-Node-dev simplificado

Node con TypeScript - TS-Node-dev (preferido)

  1. Instalar TypeScript y demás dependencias
npm i -D typescript @types/node ts-node-dev rimraf
  1. Inicializar el archivo de configuración de TypeScript ( Se puede configurar al gusto)
npx tsc --init --outDir dist/ --rootDir src
@CarmeloRicarte
CarmeloRicarte / CustomTable.css
Last active February 23, 2023 12:02
Custom table witn React, Typescript, CSS without libraries
.table-container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
.table {
width: 100%;
max-width: 90vw;
@CarmeloRicarte
CarmeloRicarte / time-since.ts
Created January 23, 2023 16:48 — forked from Klerith/time-since.ts
Fecha de creación humana
export const timeSince = ( date: string ) => {
const baseDate = new Date(date)
const seconds = Math.floor(( new Date().getTime() - baseDate.getTime() ) / 1000);
let interval = seconds / 31536000;
@CarmeloRicarte
CarmeloRicarte / test-utils.tsx
Last active December 20, 2022 15:17
Render store hook with providers
import React, { PropsWithChildren } from "react";
import { renderHook, RenderOptions } from "@testing-library/react";
import { Provider } from "react-redux";
import { ToolkitStore } from "@reduxjs/toolkit/dist/configureStore";
import type { AppStore } from "../../src/store";
// This type interface extends the default options for render from RTL, as well
// as allows the user to specify other things such as store.
interface ExtendedRenderOptions extends Omit<RenderOptions, "queries"> {
store?: AppStore;
@CarmeloRicarte
CarmeloRicarte / renderWithProviders.js
Created December 20, 2022 10:44 — forked from hjumeau/renderWithProviders.js
A simple test helper - store provider for react-testing-library
import { render, RenderOptions } from '@testing-library/react';
import React from 'react';
import { Provider } from 'react-redux';
import { store } from '../../store';
const Providers: React.FC = ({ children }) => {
return (
<Provider store={store}>
{children}
</Provider>
@CarmeloRicarte
CarmeloRicarte / useEnvTestingVite.md
Created December 10, 2022 13:20
Use ENV in testing with Vite

Use ENV in testing with Vite

  1. Install dotenv package as dev depencency.
  2. Create .env.test at root of your project, with the same environment variables that you have in your .env file. Then, add it to .gitignore file.
  3. Create a helper function that will return all env:
export const getEnvironments = () => {
  import.meta.env;
  return {
    ...import.meta.env,
 };
@CarmeloRicarte
CarmeloRicarte / useForm.ts
Last active December 9, 2022 13:22
useForm hook with Typescript
import { useState, ChangeEvent, useEffect, useMemo } from "react";
export const useForm = <T extends Object>(
initialForm: T,
formValidations: {
[key: string]: [(value: string) => boolean, string];
} = {}
) => {
const [formState, setFormState] = useState(initialForm);
const [formValidation, setFormValidation] = useState(
@CarmeloRicarte
CarmeloRicarte / todosApi.ts
Created December 8, 2022 09:21
Example of how to create an api with RTK Query
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
export const todosApi = createApi({
reducerPath: "todos",
baseQuery: fetchBaseQuery({
baseUrl: "https://jsonplaceholder.typicode.com",
}),
endpoints: (builder) => ({
getTodos: builder.query({
query: () => "/todos",
@CarmeloRicarte
CarmeloRicarte / templateSlice.ts
Created December 7, 2022 16:45
Template for create a Redux Toolkit slice
import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
export interface CounterState {
counter: number;
}
const initialState: CounterState = {
counter: 0,
};
@CarmeloRicarte
CarmeloRicarte / useNavigate.mock.ts
Last active November 8, 2024 20:54
useNavigate mock with Vitest
// this is how to mock partial library for mock a method, in this case, useNavigate
const mockedUseNavigate = vi.fn();
vi.mock("react-router-dom", async () => {
const mod = await vi.importActual<typeof import("react-router-dom")>(
"react-router-dom"
);
return {
...mod,
useNavigate: () => mockedUseNavigate,
};