Skip to content

Commit b28c0cd

Browse files
authored
test: Make ReferenceTab tests able to run independently (#18108)
1 parent 395d5a0 commit b28c0cd

File tree

1 file changed

+34
-15
lines changed
  • src/Designer/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/ReferenceTab

1 file changed

+34
-15
lines changed

src/Designer/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/ReferenceTab/ReferenceTab.test.tsx

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { screen } from '@testing-library/react';
2+
import type { RenderResult } from '@testing-library/react';
23
import { ReferenceTab } from './ReferenceTab';
34
import { renderWithProviders } from '../../../../../../testing/mocks';
5+
import type { ExtendedRenderOptions } from '../../../../../../testing/mocks';
46
import { ComponentType } from 'app-shared/types/ComponentType';
57
import type { FormComponent } from '../../../../../../types/FormComponent';
68
import { textMock } from '@studio/testing/mocks/i18nMock';
79
import userEvent from '@testing-library/user-event';
810
import { componentMocks } from '../../../../../../testing/componentMocks';
11+
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
12+
import { QueryKey } from 'app-shared/types/QueryKey';
913

1014
// Test data:
1115
const mockComponent = componentMocks[ComponentType.Dropdown];
1216
const mockOptionsId1 = 'test1';
1317
const mockOptionsId2 = 'test2';
14-
const getOptionListIds = jest
15-
.fn()
16-
.mockImplementation(() => Promise.resolve<string[]>([mockOptionsId1, mockOptionsId2]));
18+
const optionListIds: string[] = [mockOptionsId1, mockOptionsId2];
1719
const handleComponentChange = jest.fn();
20+
const org = 'org';
21+
const app = 'app';
1822

1923
describe('ReferenceTab', () => {
2024
afterEach(() => jest.clearAllMocks());
@@ -25,15 +29,15 @@ describe('ReferenceTab', () => {
2529
});
2630

2731
it('should render the component', () => {
28-
renderReferenceTab();
32+
renderReferenceTabWithData();
2933

3034
expect(
3135
screen.getByText(textMock('ux_editor.options.code_list_reference_id.description')),
3236
).toBeInTheDocument();
3337
});
3438

3539
it('should render value when optionsId is set', () => {
36-
renderReferenceTab({
40+
renderReferenceTabWithData({
3741
componentProps: {
3842
optionsId: 'some-id',
3943
},
@@ -43,7 +47,7 @@ describe('ReferenceTab', () => {
4347
});
4448

4549
it('should render no value if optionsId is a codeList from the library', () => {
46-
renderReferenceTab({
50+
renderReferenceTabWithData({
4751
componentProps: {
4852
optionsId: mockOptionsId1,
4953
},
@@ -54,7 +58,7 @@ describe('ReferenceTab', () => {
5458

5559
it('should call handleComponentChange when input value changes', async () => {
5660
const user = userEvent.setup();
57-
renderReferenceTab();
61+
renderReferenceTabWithData();
5862
const inputElement = getInputElement();
5963
await user.type(inputElement, 'new-id');
6064
expect(handleComponentChange).toHaveBeenCalledWith({
@@ -65,7 +69,7 @@ describe('ReferenceTab', () => {
6569

6670
it('should call remove options property (if it exists) when input value changes', async () => {
6771
const user = userEvent.setup();
68-
renderReferenceTab({
72+
renderReferenceTabWithData({
6973
componentProps: {
7074
options: [
7175
{
@@ -90,14 +94,27 @@ function getInputElement() {
9094
});
9195
}
9296

93-
const renderReferenceTab = ({
94-
componentProps = {},
95-
}: {
96-
handleComponentChange?: () => void;
97+
type RenderReferenceTabWithDataProps = Pick<RenderReferenceTabProps, 'componentProps'>;
98+
99+
function renderReferenceTabWithData({
100+
componentProps,
101+
}: RenderReferenceTabWithDataProps = {}): RenderResult {
102+
const queryClient = createQueryClientMock();
103+
queryClient.setQueryData([QueryKey.OptionListIds, org, app], optionListIds);
104+
return renderReferenceTab({ componentProps, options: { queryClient } });
105+
}
106+
107+
type RenderReferenceTabProps = {
97108
componentProps?: Partial<
98109
FormComponent<ComponentType.RadioButtons | ComponentType.Checkboxes | ComponentType.Dropdown>
99110
>;
100-
} = {}) => {
111+
options?: ExtendedRenderOptions;
112+
};
113+
114+
const renderReferenceTab = ({
115+
componentProps = {},
116+
options,
117+
}: RenderReferenceTabProps = {}): RenderResult =>
101118
renderWithProviders(
102119
<ReferenceTab
103120
handleComponentChange={handleComponentChange}
@@ -106,6 +123,8 @@ const renderReferenceTab = ({
106123
...componentProps,
107124
}}
108125
/>,
109-
{ queries: { getOptionListIds } },
126+
{
127+
appRouteParams: { org, app },
128+
...options,
129+
},
110130
);
111-
};

0 commit comments

Comments
 (0)