Skip to content

Commit

Permalink
Add jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Nov 14, 2024
1 parent 92adc29 commit 1b18c86
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ const setup = (props: any = { onUpdate() {} }, appDependencies?: any) => {
return testBed;
};

const getContext = (sourceFieldEnabled: boolean = true, hasEnterpriseLicense: boolean = true) =>
({
config: {
enableMappingsSourceFieldSection: sourceFieldEnabled,
},
plugins: {
licensing: {
license$: {
subscribe: jest.fn((callback: any) => {
callback({
isActive: true,
hasAtLeast: jest.fn((type: any) => hasEnterpriseLicense),
});
return { unsubscribe: jest.fn() };
}),
},
},
},
} as unknown as AppDependencies);

describe('Mappings editor: configuration form', () => {
let testBed: TestBed<TestSubjects>;

it('renders the form', async () => {
const ctx = {
config: {
enableMappingsSourceFieldSection: true,
},
plugins: {
licensing: licensingMock.createStart(),
},
} as unknown as AppDependencies;

await act(async () => {
testBed = setup({ esNodesPlugins: [] }, ctx);
testBed = setup({ esNodesPlugins: [] }, getContext());
});
testBed.component.update();
const { exists } = testBed;
Expand All @@ -52,42 +63,52 @@ describe('Mappings editor: configuration form', () => {
});

describe('_source field', () => {
it('renders the _source field when it is enabled', async () => {
const ctx = {
config: {
enableMappingsSourceFieldSection: true,
},
plugins: {
licensing: licensingMock.createStart(),
},
} as unknown as AppDependencies;
describe('renders depending on enableMappingsSourceFieldSection config', () => {
it('renders when config set to true', async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(true));
});
testBed.component.update();
const { exists } = testBed;

await act(async () => {
testBed = setup({ esNodesPlugins: [] }, ctx);
expect(exists('sourceField')).toBe(true);
});
testBed.component.update();
const { exists } = testBed;

expect(exists('sourceField')).toBe(true);
it("doesn't render when config set to false", async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(false));
});
testBed.component.update();
const { exists } = testBed;

expect(exists('sourceField')).toBe(false);
});
});

it("doesn't render the _source field when it is disabled", async () => {
const ctx = {
config: {
enableMappingsSourceFieldSection: false,
},
plugins: {
licensing: licensingMock.createStart(),
},
} as unknown as AppDependencies;
describe('has synthetic option depending on license', () => {
it('has synthetic option on enterprise license', async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(true, true));
});
testBed.component.update();
const { exists, find, component } = testBed;

await act(async () => {
testBed = setup({ esNodesPlugins: [] }, ctx);
// Clicking on the field to open the options dropdown
find('sourceValueField').simulate('click');
expect(exists('syntheticSourceFieldOption')).toBe(true);
});
testBed.component.update();
const { exists } = testBed;

expect(exists('sourceField')).toBe(false);
it("doesn't have synthetic option on lower than enterprise license", async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(true, false));
});
testBed.component.update();
const { exists, find, component } = testBed;

// Clicking on the field to open the options dropdown
find('sourceValueField').simulate('click');
expect(exists('syntheticSourceFieldOption')).toBe(false);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const SourceFieldSection = () => {
value: STORED_SOURCE_OPTION,
inputDisplay: sourceOptionLabels[STORED_SOURCE_OPTION],
dropdownDisplay: renderOptionDropdownDisplay(STORED_SOURCE_OPTION),
'data-test-subj': 'storedSourceFieldOption',
},
];

Expand All @@ -55,12 +56,14 @@ export const SourceFieldSection = () => {
value: SYNTHETIC_SOURCE_OPTION,
inputDisplay: sourceOptionLabels[SYNTHETIC_SOURCE_OPTION],
dropdownDisplay: renderOptionDropdownDisplay(SYNTHETIC_SOURCE_OPTION),
'data-test-subj': 'syntheticSourceFieldOption',
});
}
sourceValueOptions.push({
value: DISABLED_SOURCE_OPTION,
inputDisplay: sourceOptionLabels[DISABLED_SOURCE_OPTION],
dropdownDisplay: renderOptionDropdownDisplay(DISABLED_SOURCE_OPTION),
'data-test-subj': 'disabledSourceFieldOption',
});

const renderDisableWarning = () => (
Expand Down

0 comments on commit 1b18c86

Please sign in to comment.