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

test: Remove mockDate and fix unit tests #32947

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
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.

Loading