Skip to content

Commit

Permalink
chore: Update ui/TruncatedMessage tests to Vitest (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Sep 20, 2024
1 parent 2d5ce81 commit 303a30e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { render, screen } from '@testing-library/react'
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import { useTruncation } from './hooks'
import TruncatedMessage from './TruncatedMessage'

jest.mock('./hooks')
vi.mock('./hooks')

afterEach(() => {
cleanup()
})

describe('TruncatedMessage', () => {
function setup({ canTruncate = false }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { renderHook } from '@testing-library/react'
import React from 'react'
import { cleanup, renderHook } from '@testing-library/react'

import { useTruncation } from './useTruncation'

Expand All @@ -24,9 +23,24 @@ class ResizeObserver {

global.window.ResizeObserver = ResizeObserver

describe('useTruncation', () => {
let useRefSpy: any
const mocks = vi.hoisted(() => ({
useRef: vi.fn(),
}))

vi.mock('react', async () => {
const original = await vi.importActual('react')

return {
...original,
useRef: mocks.useRef,
}
})

afterEach(() => {
cleanup()
})

describe('useTruncation', () => {
function setup({
clientHeight = 0,
scrollHeight = 0,
Expand All @@ -45,7 +59,8 @@ describe('useTruncation', () => {
scrollWidth,
},
}
useRefSpy = jest.spyOn(React, 'useRef').mockReturnValue(refReturn)

mocks.useRef.mockReturnValue(refReturn)

if (enableEntry) {
entry = {
Expand All @@ -62,7 +77,7 @@ describe('useTruncation', () => {
}

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

describe('scrolls are larger then clients', () => {
Expand All @@ -71,7 +86,7 @@ describe('useTruncation', () => {
setup({ scrollHeight: 10, scrollWidth: 10 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeTruthy()
})
})
Expand All @@ -81,7 +96,7 @@ describe('useTruncation', () => {
setup({ scrollHeight: 10, scrollWidth: 0 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeTruthy()
})
})
Expand All @@ -91,7 +106,7 @@ describe('useTruncation', () => {
setup({ scrollHeight: 0, scrollWidth: 10 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeTruthy()
})
})
Expand All @@ -103,7 +118,7 @@ describe('useTruncation', () => {
setup({ clientHeight: 10, clientWidth: 10 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeFalsy()
})
})
Expand All @@ -113,7 +128,7 @@ describe('useTruncation', () => {
setup({ clientHeight: 10, clientWidth: 0 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeFalsy()
})
})
Expand All @@ -123,7 +138,7 @@ describe('useTruncation', () => {
setup({ clientHeight: 0, clientWidth: 10 })
const { result } = renderHook(() => useTruncation())

expect(useRefSpy).toHaveBeenCalled()
expect(mocks.useRef).toHaveBeenCalled()
expect(result.current.canTruncate).toBeFalsy()
})
})
Expand Down

0 comments on commit 303a30e

Please sign in to comment.