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 32d6672
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { AppDependencies } from '../../../..';
import { registerTestBed, TestBed } from '@kbn/test-jest-helpers';
import { licensingMock } from '@kbn/licensing-plugin/public/mocks';
import { ConfigurationForm } from '../../components/configuration_form';
import { WithAppDependencies } from './helpers/setup_environment';
import { TestSubjects } from './helpers/mappings_editor.helpers';
Expand All @@ -29,21 +28,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 +62,87 @@ 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 } = 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 } = testBed;

// Clicking on the field to open the options dropdown
find('sourceValueField').simulate('click');
expect(exists('syntheticSourceFieldOption')).toBe(false);
});
});

describe('has correct default value', () => {
it("defaults to 'stored' if index mode prop is 'standard'", async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [], indexMode: 'standard' }, getContext());
});
testBed.component.update();
const { find } = testBed;

// Check that the stored option is selected
expect(find('sourceValueField').text()).toBe('Stored _source');
});

it("defaults to 'synthetic' if index mode prop is 'logsdb'", async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [], indexMode: 'logsdb' }, getContext());
});
testBed.component.update();
const { find } = testBed;

// Check that the synthetic option is selected
expect(find('sourceValueField').text()).toBe('Synthetic _source');
});

it("defaults to 'synthetic' if index mode prop is 'time_series'", async () => {
await act(async () => {
testBed = setup({ esNodesPlugins: [], indexMode: 'time_series' }, getContext());
});
testBed.component.update();
const { find } = testBed;

// Check that the synthetic option is selected
expect(find('sourceValueField').text()).toBe('Synthetic _source');
});
});
});
});
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 32d6672

Please sign in to comment.