Skip to content

Commit

Permalink
test: Remove mockDate and fix unit tests (#32947) (#32949)
Browse files Browse the repository at this point in the history
Co-authored-by: gabriellsh <[email protected]>
  • Loading branch information
dionisio-bot[bot] and gabriellsh committed Jul 31, 2024
1 parent 5ddb8c3 commit e1b4c28
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
18 changes: 4 additions & 14 deletions apps/meteor/client/hooks/usePruneWarningMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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(),
});
Expand All @@ -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(),
});
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
12 changes: 0 additions & 12 deletions apps/meteor/tests/mocks/mockDate.ts

This file was deleted.

0 comments on commit e1b4c28

Please sign in to comment.