Skip to content
Snippets Groups Projects
Unverified Commit e1b4c28d authored by dionisio-bot[bot]'s avatar dionisio-bot[bot] Committed by GitHub
Browse files

test: Remove mockDate and fix unit tests (#32947) (#32949)

parent 5ddb8c3e
Branches rebase-backport-6.10.2-32807
No related tags found
No related merge requests found
......@@ -4,21 +4,22 @@ import '@testing-library/jest-dom/extend-expect';
import { createRenteionPolicySettingsMock as createMock } from '../../../tests/mocks/client/mockRetentionPolicySettings';
import { createFakeRoom } from '../../../tests/mocks/data';
import { setDate } from '../../../tests/mocks/mockDate';
import RetentionPolicyCallout from './RetentionPolicyCallout';
jest.useFakeTimers();
beforeEach(() => {
jest.setSystemTime(new Date(2024, 5, 1, 0, 0, 0));
});
describe('RetentionPolicyCallout', () => {
it('Should render callout if settings are valid', () => {
setDate();
const fakeRoom = createFakeRoom({ t: 'c' });
render(<RetentionPolicyCallout room={fakeRoom} />, { wrapper: createMock({ appliesToChannels: true, TTLChannels: 60000 }) });
expect(screen.getByRole('alert')).toHaveTextContent('a minute June 1, 2024, 12:30 AM');
});
it('Should not render callout if settings are invalid', () => {
setDate();
const fakeRoom = createFakeRoom({ t: 'c' });
render(<RetentionPolicyCallout room={fakeRoom} />, {
wrapper: createMock({ appliesToChannels: true, TTLChannels: 60000, advancedPrecisionCron: '* * * 12 *', advancedPrecision: true }),
......
......@@ -3,7 +3,6 @@ import { renderHook } from '@testing-library/react-hooks';
import { createRenteionPolicySettingsMock as createMock } from '../../tests/mocks/client/mockRetentionPolicySettings';
import { createFakeRoom } from '../../tests/mocks/data';
import { setDate } from '../../tests/mocks/mockDate';
import { usePruneWarningMessage } from './usePruneWarningMessage';
jest.useFakeTimers();
......@@ -22,10 +21,13 @@ const getRetentionRoomProps = (props: Partial<IRoomWithRetentionPolicy['retentio
};
};
beforeEach(() => {
jest.setSystemTime(new Date(2024, 5, 1, 0, 0, 0));
});
describe('usePruneWarningMessage hook', () => {
describe('Cron timer and precision', () => {
it('Should update the message after the nextRunDate has passaed', async () => {
setDate();
const fakeRoom = createFakeRoom({ t: 'c' });
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
......@@ -40,7 +42,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the default warning with precision set to every_hour', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -53,7 +54,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the default warning with precision set to every_six_hours', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -66,7 +66,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the default warning with precision set to every_day', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -79,7 +78,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the default warning with advanced precision', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -95,7 +93,6 @@ describe('usePruneWarningMessage hook', () => {
describe('No override', () => {
it('Should return the default warning', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -107,7 +104,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the unpinned messages warning', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
appliesToChannels: true,
......@@ -120,7 +116,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the files only warning', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
......@@ -134,7 +129,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the unpinned files only warning', () => {
const fakeRoom = createFakeRoom({ t: 'c' });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock({
......@@ -151,7 +145,6 @@ describe('usePruneWarningMessage hook', () => {
describe('Overriden', () => {
it('Should return the default warning', () => {
const fakeRoom = createFakeRoom({ t: 'p', ...getRetentionRoomProps() });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock(),
});
......@@ -160,7 +153,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the unpinned messages warning', () => {
const fakeRoom = createFakeRoom({ t: 'p', ...getRetentionRoomProps({ excludePinned: true }) });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock(),
});
......@@ -169,7 +161,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the files only warning', () => {
const fakeRoom = createFakeRoom({ t: 'p', ...getRetentionRoomProps({ filesOnly: true }) });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock(),
......@@ -179,7 +170,6 @@ describe('usePruneWarningMessage hook', () => {
it('Should return the unpinned files only warning', () => {
const fakeRoom = createFakeRoom({ t: 'p', ...getRetentionRoomProps({ excludePinned: true, filesOnly: true }) });
setDate();
const { result } = renderHook(() => usePruneWarningMessage(fakeRoom), {
wrapper: createMock(),
......
......@@ -4,21 +4,22 @@ import '@testing-library/jest-dom/extend-expect';
import { createRenteionPolicySettingsMock as createMock } from '../../../../tests/mocks/client/mockRetentionPolicySettings';
import { createFakeRoom } from '../../../../tests/mocks/data';
import { setDate } from '../../../../tests/mocks/mockDate';
import RetentionPolicyWarning from './RetentionPolicyWarning';
jest.useFakeTimers();
beforeEach(() => {
jest.setSystemTime(new Date(2024, 5, 1, 0, 0, 0));
});
describe('RetentionPolicyWarning', () => {
it('Should render callout if settings are valid', () => {
setDate();
const fakeRoom = createFakeRoom({ t: 'c' });
render(<RetentionPolicyWarning room={fakeRoom} />, { wrapper: createMock({ appliesToChannels: true, TTLChannels: 60000 }) });
expect(screen.getByRole('alert')).toHaveTextContent('a minute June 1, 2024, 12:30 AM');
});
it('Should not render callout if settings are invalid', () => {
setDate();
const fakeRoom = createFakeRoom({ t: 'c' });
render(<RetentionPolicyWarning room={fakeRoom} />, {
wrapper: createMock({ appliesToChannels: true, TTLChannels: 60000, advancedPrecisionCron: '* * * 12 *', advancedPrecision: true }),
......
// you must use jest.useFakeTimers for this to work.
export const setDate = (minutes = 1, hours = 0, date = 1) => {
// June 12, 2024, 12:00 AM
const fakeDate = new Date();
fakeDate.setFullYear(2024);
fakeDate.setMonth(5);
fakeDate.setDate(date);
fakeDate.setHours(hours);
fakeDate.setMinutes(minutes);
fakeDate.setSeconds(0);
jest.setSystemTime(fakeDate);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment