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
  • 18:32 (UTC +01:00)
View GitHub Profile
@CarmeloRicarte
CarmeloRicarte / parse-jwt.js
Created August 6, 2019 09:40 — forked from Klerith/parse-jwt.js
Parse - JWT - Obtener Payload y fecha de creación y expiración
function parseJwt (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse(window.atob(base64));
};
@CarmeloRicarte
CarmeloRicarte / vite-testing-config.md
Created November 3, 2022 08:14 — forked from Klerith/vite-testing-config.md
Vite + Jest + React Testing Library - Configuraciones a seguir

Instalación y configuracion de Jest + React Testing Library

En proyectos de React + Vite

  1. Instalaciones:
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react 
yarn add --dev @testing-library/react @types/jest jest-environment-jsdom
  1. Opcional: Si usamos Fetch API en el proyecto:
@CarmeloRicarte
CarmeloRicarte / Vitest-TestingLibrary-React.md
Last active November 28, 2022 15:41
Vitest - Testing Library - React config for Javascript
  1. Instalaciones
    yarn add -D vitest jsdom @vitest/ui @testing-library/react @vitest/coverage-c8
  2. Configuraciones
    2.1 En vite.config.js, añadir lo siguiente al defineConfig:
    test: { environment: 'jsdom', globals: true }
    2.2 En package.json, añadir lo siguiente dentro de scripts:
    "test": "vitest",
    "test:ui": "vitest --ui",\
@CarmeloRicarte
CarmeloRicarte / Vitest-TestingLibrary-React-Typescript.md
Last active November 29, 2022 12:52
Vitest-Testing Library React - Typescript
  1. Instalaciones
    yarn add -D vitest jsdom @vitest/ui @testing-library/react @testing-library/jest-dom @vitest/coverage-c8
  2. Configuraciones
    2.1 Crear el archivo setupTests.ts en el root con lo siguiente:
    import "@testing-library/jest-dom";
    import matchers from "@testing-library/jest-dom/matchers";
    import { expect } from "vitest";
    expect.extend(matchers);
    2.2 En el tsconfig.json, insertar lo siguiente dentro de compilerOptions: "types": ["vitest/globals"]\
@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,
};
@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 / 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 / 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 / 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 / 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>