Skip to content
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
@@ -0,0 +1,58 @@
import type { ISetting } from '@rocket.chat/core-typings';
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import SettingField from './SettingField';
import EditableSettingsProvider from '../../settings/EditableSettingsProvider';

const settingStructure = {
packageValue: false,
blocked: false,
public: true,
type: 'boolean',
i18nLabel: 'Test_Setting',
i18nDescription: 'Test_Setting_Description',
enableQuery: undefined,
displayQuery: undefined,
} as Partial<ISetting>;

const dispatchMock = jest.fn();

jest.mock('@rocket.chat/ui-contexts', () => ({
...jest.requireActual('@rocket.chat/ui-contexts'),
useSettingsDispatch: () => dispatchMock,
}));
jest.mock('@rocket.chat/core-typings', () => ({
...jest.requireActual('@rocket.chat/core-typings'),
isSetting: jest.fn().mockReturnValue(true),
}));

describe('SettingField', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});

it('should call dispatch when setting value is changed', async () => {
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });

render(<SettingField settingId='Test_Setting' />, {
wrapper: mockAppRoot()
.wrap((children) => <EditableSettingsProvider>{children}</EditableSettingsProvider>)
.withSetting('Test_Setting', false, settingStructure)
.build(),
});

const checkbox = screen.getByRole('checkbox');
await user.click(checkbox);

await waitFor(() => {
expect(dispatchMock).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ISettingColor, SettingEditor, SettingValue } from '@rocket.chat/core-typings';
import { isSettingColor, isSetting } from '@rocket.chat/core-typings';
import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
import { useSettingStructure } from '@rocket.chat/ui-contexts';
import { useSettingsDispatch, useSettingStructure } from '@rocket.chat/ui-contexts';
import DOMPurify from 'dompurify';
import type { ReactElement } from 'react';
import { useEffect, useMemo, useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import MarkdownText from '../../../../components/MarkdownText';
import { useEditableSetting, useEditableSettingsDispatch, useEditableSettingVisibilityQuery } from '../../EditableSettingsContext';
import { useEditableSetting, useEditableSettingVisibilityQuery } from '../../EditableSettingsContext';
import MemoizedSetting from '../../settings/Setting/MemoizedSetting';
import { useHasSettingModule } from '../../settings/hooks/useHasSettingModule';

Expand All @@ -32,7 +32,7 @@ function SettingField({ className = undefined, settingId, sectionChanged }: Sett
throw new Error(`Setting ${settingId} is not valid`);
}

const dispatch = useEditableSettingsDispatch();
const dispatch = useSettingsDispatch();

const update = useDebouncedCallback(
({ value, editor }: { value?: SettingValue; editor?: SettingEditor }) => {
Expand All @@ -45,9 +45,6 @@ function SettingField({ className = undefined, settingId, sectionChanged }: Sett
_id: persistedSetting._id,
...(value !== undefined && { value }),
...(editor !== undefined && { editor }),
changed:
JSON.stringify(persistedSetting.value) !== JSON.stringify(value) ||
(isSettingColor(persistedSetting) && JSON.stringify(persistedSetting.editor) !== JSON.stringify(editor)),
},
]);
},
Expand Down
Loading