From 037128cc55d9f3293f6f73adcbedd74f41125f8a Mon Sep 17 00:00:00 2001 From: Douglas Fabris Date: Mon, 2 Sep 2024 13:00:47 -0300 Subject: [PATCH] test: Add some unit tests for `useRetentionPolicy` (#33196) --- .../room/hooks/useRetentionPolicy.spec.ts | 87 +++++++++++++++++++ apps/meteor/tests/mocks/data.ts | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 apps/meteor/client/views/room/hooks/useRetentionPolicy.spec.ts diff --git a/apps/meteor/client/views/room/hooks/useRetentionPolicy.spec.ts b/apps/meteor/client/views/room/hooks/useRetentionPolicy.spec.ts new file mode 100644 index 000000000000..9b99d6e4d781 --- /dev/null +++ b/apps/meteor/client/views/room/hooks/useRetentionPolicy.spec.ts @@ -0,0 +1,87 @@ +import { mockAppRoot } from '@rocket.chat/mock-providers'; +import { renderHook } from '@testing-library/react'; + +import { createFakeRoom } from '../../../../tests/mocks/data'; +import { useRetentionPolicy } from './useRetentionPolicy'; + +const getGlobalSettings = ({ + enabled = false, + filesOnly = false, + doNotPrunePinned = false, + ignoreThreads = false, + appliesToChannels = false, + appliesToGroups = false, + appliesToDMs = false, +}) => { + return mockAppRoot() + .withSetting('RetentionPolicy_Enabled', enabled) + .withSetting('RetentionPolicy_FilesOnly', filesOnly) + .withSetting('RetentionPolicy_DoNotPrunePinned', doNotPrunePinned) + .withSetting('RetentionPolicy_DoNotPruneThreads', ignoreThreads) + .withSetting('RetentionPolicy_AppliesToChannels', appliesToChannels) + .withSetting('RetentionPolicy_AppliesToGroups', appliesToGroups) + .withSetting('RetentionPolicy_AppliesToDMs', appliesToDMs); +}; + +const defaultValue = { + enabled: false, + isActive: false, + filesOnly: false, + excludePinned: false, + ignoreThreads: false, +}; + +const roomTypeConfig = { + c: { appliesToChannels: true }, + p: { appliesToGroups: true }, + d: { appliesToDMs: true }, +}; + +const CHANNELS_TYPE = 'c'; + +it('should return the default value if global retention is not enabled', async () => { + const fakeRoom = createFakeRoom({ t: CHANNELS_TYPE }); + + const { result } = renderHook(() => useRetentionPolicy(fakeRoom), { + legacyRoot: true, + wrapper: getGlobalSettings({}).build(), + }); + + expect(result.current).toEqual(expect.objectContaining(defaultValue)); +}); + +it('should return enabled true if global retention is enabled', async () => { + const fakeRoom = createFakeRoom({ t: CHANNELS_TYPE }); + + const { result } = renderHook(() => useRetentionPolicy(fakeRoom), { + legacyRoot: true, + wrapper: getGlobalSettings({ enabled: true }).build(), + }); + + expect(result.current).toEqual(expect.objectContaining({ ...defaultValue, enabled: true })); +}); + +it('should return enabled and active true global retention is active for rooms of the type', async () => { + const fakeRoom = createFakeRoom({ t: CHANNELS_TYPE }); + + const { result } = renderHook(() => useRetentionPolicy(fakeRoom), { + legacyRoot: true, + wrapper: getGlobalSettings({ enabled: true, ...roomTypeConfig[CHANNELS_TYPE] }).build(), + }); + + expect(result.current).toEqual(expect.objectContaining({ ...defaultValue, enabled: true, isActive: true })); +}); + +it.failing( + 'should isActive be false if global retention is active for rooms of the type and room has retention.enabled false', + async () => { + const fakeRoom = createFakeRoom({ t: CHANNELS_TYPE, retention: { enabled: false } }); + + const { result } = renderHook(() => useRetentionPolicy(fakeRoom), { + legacyRoot: true, + wrapper: getGlobalSettings({ enabled: true, ...roomTypeConfig[CHANNELS_TYPE] }).build(), + }); + + expect(result.current?.isActive).toBe(false); + }, +); diff --git a/apps/meteor/tests/mocks/data.ts b/apps/meteor/tests/mocks/data.ts index 14f03a6bd9ab..fd6a0ce91123 100644 --- a/apps/meteor/tests/mocks/data.ts +++ b/apps/meteor/tests/mocks/data.ts @@ -21,7 +21,7 @@ export function createFakeUser(overrides?: Partial): IUser { }; } -export const createFakeRoom = (overrides?: Partial): IRoom => ({ +export const createFakeRoom = (overrides?: Partial): IRoom => ({ _id: faker.database.mongodbObjectId(), _updatedAt: faker.date.recent(), t: faker.helpers.arrayElement(['c', 'p', 'd']),