Skip to content

Commit

Permalink
Merge pull request #14248 from nextcloud/fix/noid/jest-date-now
Browse files Browse the repository at this point in the history
  • Loading branch information
Antreesy authored Jan 29, 2025
2 parents 00e504a + cd15420 commit 93b87b4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -694,6 +692,8 @@ describe('Message.vue', () => {

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

jest.useRealTimers()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand All @@ -289,6 +288,8 @@ describe('MessageButtonsBar.vue', () => {
await actionButton.find('button').trigger('click')

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

jest.useRealTimers()
})

/**
Expand Down
4 changes: 1 addition & 3 deletions src/store/conversationsStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/store/participantsStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/utils/__tests__/formattedTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/utils/__tests__/prepareTemporaryMessage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 93b87b4

Please sign in to comment.