| Function | Clears Call History | Resets Implementations | Restores Original Implementation | Scope of Use |
|---|---|---|---|---|
mockClear() |
✅ | ❌ | ❌ | Clear usage data of the mock function but keep its implementation and return values. |
mockReset() |
✅ | ✅ | ❌ | Reset the mock function to its initial state, removing any custom behavior or return values. |
mockRestore() |
✅ | ✅ | ✅ | Restore the original function, removing the mock (only applicable with jest.spyOn). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This Gist demonstrates a simple yet effective implementation of a memoization function in JavaScript. | |
| * Memoization is an optimization technique used to speed up computer programs by storing the results of expensive function | |
| * calls and returning the cached result when the same inputs occur again. | |
| */ | |
| const memoMap = new Map(); | |
| const complexFunction = (a1: number[], a2: number[]): number => { | |
| // Simulate heavy calculations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div class="app"> | |
| <button @click="store.mutations.open()">Open Modal </button> | |
| <Modal v-if="store.state.isModalOpen"/> | |
| </div> | |
| </template> | |
| <script setup> | |
| import store from "./store"; | |
| import Modal from "./Modal.vue"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div class="app"> | |
| <button @click="store.mutations.open()">Open Modal </button> | |
| <Modal v-if="store.state.isModalOpen"/> | |
| </div> | |
| </template> | |
| <script setup> | |
| import store from "./store"; | |
| import Modal from "./Modal.vue"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createStore } from "vuex"; | |
| import type { Post } from "@/types"; | |
| export interface ApisLoadState { | |
| isCalled: boolean; | |
| isLoaded: boolean; | |
| } | |
| export interface ApiLoadState extends ApisLoadState { | |
| api: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| declare global { | |
| interface AmplitudeInstance { | |
| logEvent(eventName: string, eventProperties?: object): void; | |
| identify(identifyObject: object): void; | |
| setUserId(userId: string | null): void; | |
| init(apiKey: string, userId?: string, options?: object, callback?: () => void): void; | |
| } | |
| interface Amplitude { | |
| getInstance(): AmplitudeInstance; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { expect, describe, it, beforeAll, afterEach, afterAll, beforeEach } from "vitest"; | |
| import { QueryClientProvider } from "react-query"; | |
| import { ReactNode } from "react"; | |
| import { http, HttpResponse } from "msw"; | |
| import { useCommonStore } from "@/_store/useCommonStore"; | |
| import { act, renderHook, waitFor } from "@testing-library/react"; | |
| import { createTestServer, queryClient } from "@/__tests__/utilities"; | |
| import useFetchCustomVariables from "@/_hooks/useFetchCustomVariables"; | |
| const successResponse = [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { describe, it, expect } from "vitest"; | |
| import { renderHook } from "@testing-library/react"; | |
| import useDeepCompareMemoize from "./useDeepCompareMemoize"; | |
| describe("useDeepCompareMemoize", () => { | |
| it('should return a memoized version of the input value based on deep comparison', () => { | |
| const initialData = { user: { name: 'John', age: 25, city: 'New York' } }; | |
| const newData = { user: { name: 'Alice', age: 30, city: 'San Francisco' } }; | |
| const identicalData = { user: { name: 'John', age: 25, city: 'New York' } }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { act, renderHook } from '@testing-library/react'; | |
| import { describe, it, expect, beforeEach } from "vitest"; | |
| import { usePostsStore } from "@/_store/usePostsStore"; | |
| describe('usePostsStore', () => { | |
| beforeEach(() => { | |
| usePostsStore.getState().reset() | |
| }); | |
| it('should return the initial state', () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest"; | |
| import { GET } from "./route"; | |
| import { createMockRequest, createTestServer } from "@/__tests__/utilities"; | |
| import { http, HttpResponse } from "msw"; | |
| const url = `${process.env.VITE_API_BASE_URL_V2}/posts`; | |
| const mockPosts = [ { | |
| "userId": 1, | |
| "id": 2, | |
| "title": "qui est esse", |