Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): replace jest.spyOn with jest.useFakeTimers #14248

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -658,9 +658,7 @@ describe('Message.vue', () => {
store = new Store(testStoreConfig)

// need to mock the date to be within 6h
const mockDate = new Date('2020-05-07 10:00:00')
jest.spyOn(global.Date, 'now')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(new Date('2020-05-07T10:00:00'))

const wrapper = shallowMount(Message, {
localVue,
@@ -694,6 +692,8 @@ describe('Message.vue', () => {

expect(wrapper.vm.isDeleting).toBe(false)
expect(wrapper.find('.icon-loading-small').exists()).toBe(false)

jest.useRealTimers()
})
})

Original file line number Diff line number Diff line change
@@ -267,9 +267,8 @@ describe('MessageButtonsBar.vue', () => {
describe('delete action', () => {
test('emits delete event', async () => {
// need to mock the date to be within 6h
const mockDate = new Date('2020-05-07 10:00:00')
jest.spyOn(global.Date, 'now')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(new Date('2020-05-07T10:00:00'))

useMessageInfoSpy.mockReturnValue({
isDeleteable: computed(() => true),
})
@@ -289,6 +288,8 @@ describe('MessageButtonsBar.vue', () => {
await actionButton.find('button').trigger('click')

expect(wrapper.emitted().delete).toBeTruthy()

jest.useRealTimers()
})

/**
4 changes: 1 addition & 3 deletions src/store/conversationsStore.spec.js
Original file line number Diff line number Diff line change
@@ -975,9 +975,7 @@ describe('conversationsStore', () => {

test('updates last activity', () => {
const mockDate = new Date('2020-01-01')

jest.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(mockDate)

testConversation.lastActivity = 1200300

10 changes: 5 additions & 5 deletions src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
@@ -560,12 +560,12 @@ describe('messagesStore', () => {
})

describe('temporary messages', () => {
let mockDate

beforeEach(() => {
mockDate = new Date('2020-01-01 20:00:00')
jest.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(new Date('2020-01-01T20:00:00'))
})

afterEach(() => {
jest.useRealTimers()
})

test('adds temporary message to the list', () => {
5 changes: 3 additions & 2 deletions src/store/participantsStore.spec.js
Original file line number Diff line number Diff line change
@@ -866,6 +866,8 @@ describe('participantsStore', () => {
restoreConsole = mockConsole(['error', 'debug'])
})
afterEach(() => {
jest.useRealTimers()

expect(testStoreConfig.actions.setCurrentParticipant).not.toHaveBeenCalled()
expect(testStoreConfig.actions.addConversation).not.toHaveBeenCalled()
expect(sessionStorage.setItem).not.toHaveBeenCalled()
@@ -883,8 +885,7 @@ describe('participantsStore', () => {
participantData.lastPing = mockDate.getTime() / 1000 - lastPingAge
participantData.inCall = inCall

jest.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(mockDate)

const error = generateOCSErrorResponse({ payload: participantData, status: 409 })
joinConversation.mockRejectedValue(error)
9 changes: 7 additions & 2 deletions src/utils/__tests__/formattedTime.spec.js
Original file line number Diff line number Diff line change
@@ -23,8 +23,13 @@ describe('formattedTime', () => {
})

describe('futureRelativeTime', () => {
const fixedDate = new Date('2024-01-01T00:00:00Z')
jest.spyOn(Date, 'now').mockImplementation(() => fixedDate.getTime())
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00Z'))
})

afterEach(() => {
jest.useRealTimers()
})

it('should return the correct string for time in hours', () => {
const timeInFuture = Date.now() + (2 * 60 * 60 * 1000) // 2 hours from now
9 changes: 5 additions & 4 deletions src/utils/__tests__/prepareTemporaryMessage.spec.js
Original file line number Diff line number Diff line change
@@ -7,12 +7,13 @@ import { prepareTemporaryMessage } from '../prepareTemporaryMessage.ts'

describe('prepareTemporaryMessage', () => {
const TOKEN = 'XXTOKENXX'
let mockDate

beforeEach(() => {
mockDate = new Date('2020-01-01 20:00:00')
jest.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
jest.useFakeTimers().setSystemTime(new Date('2020-01-01T20:00:00'))
})

afterEach(() => {
jest.useRealTimers()
})

const defaultPayload = {