Skip to content

Commit

Permalink
Fix jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Nov 19, 2024
1 parent 1907323 commit 4b89cac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ const setup = (props: any = { onUpdate() {} }, appDependencies?: any) => {
return testBed;
};

const getContext = (sourceFieldEnabled: boolean = true) =>
({
config: {
enableMappingsSourceFieldSection: sourceFieldEnabled,
},
} as unknown as AppDependencies);

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

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

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

describe('_source field', () => {
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;

expect(exists('sourceField')).toBe(true);
it('renders the _source field when it is enabled', async () => {
const ctx = {
config: {
enableMappingsSourceFieldSection: true,
},
} as unknown as AppDependencies;

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

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);
});
expect(exists('sourceField')).toBe(true);
});

describe('has synthetic option depending on license', () => {
it('has synthetic option on enterprise license', async () => {
jest.mock('../../mappings_state_context', () => ({
useMappingsState: jest.fn().mockReturnValue({
hasEnterpriseLicense: true,
}),
}));

await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(true));
});
testBed.component.update();
const { exists, find } = testBed;
it("doesn't render the _source field when it is disabled", async () => {
const ctx = {
config: {
enableMappingsSourceFieldSection: false,
},
} as unknown as AppDependencies;

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

it("doesn't have synthetic option on lower than enterprise license", async () => {
jest.mock('../../mappings_state_context', () => ({
useMappingsState: jest.fn().mockReturnValue({
hasEnterpriseLicense: false,
}),
}));

await act(async () => {
testBed = setup({ esNodesPlugins: [] }, getContext(true));
});
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);
});
expect(exists('sourceField')).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ export type TestSubjects =
| 'advancedConfiguration.dynamicMappingsToggle.input'
| 'advancedConfiguration.metaField'
| 'advancedConfiguration.routingRequiredToggle.input'
| 'sourceValueField'
| 'sourceField.includesField'
| 'sourceField.excludesField'
| 'dynamicTemplatesEditor'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('Mappings editor: core', () => {
let onChangeHandler: jest.Mock = jest.fn();
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
let testBed: MappingsEditorTestBed;
let hasEnterpriseLicense = true;
const mockLicenseCheck = jest.fn((type: any) => hasEnterpriseLicense);
const appDependencies = {
plugins: {
ml: { mlApi: {} },
Expand All @@ -36,7 +38,7 @@ describe('Mappings editor: core', () => {
subscribe: jest.fn((callback: any) => {
callback({
isActive: true,
hasAtLeast: jest.fn((type: any) => true),
hasAtLeast: mockLicenseCheck,
});
return { unsubscribe: jest.fn() };
}),
Expand Down Expand Up @@ -485,7 +487,7 @@ describe('Mappings editor: core', () => {
});

describe('props.indexMode sets the correct default value of _source field', () => {
it("defaults to 'stored' if index mode prop is 'standard'", async () => {
it("defaults to 'stored' with 'standard' index mode prop", async () => {
await act(async () => {
testBed = setup(
{
Expand All @@ -509,52 +511,56 @@ describe('Mappings editor: core', () => {
expect(find('sourceValueField').prop('value')).toBe('stored');
});

it("defaults to 'synthetic' if index mode prop is 'logsdb'", async () => {
await act(async () => {
testBed = setup(
{
value: { ...defaultMappings, _source: undefined },
onChange: onChangeHandler,
indexMode: 'logsdb',
},
ctx
);
});
testBed.component.update();

const {
actions: { selectTab },
find,
} = testBed;
['logsdb', 'time_series'].forEach((indexMode) => {
it(`defaults to 'synthetic' with ${indexMode} index mode prop on enterprise license`, async () => {
hasEnterpriseLicense = true;
await act(async () => {
testBed = setup(
{
value: { ...defaultMappings, _source: undefined },
onChange: onChangeHandler,
indexMode,
},
ctx
);
});
testBed.component.update();

await selectTab('advanced');
const {
actions: { selectTab },
find,
} = testBed;

// Check that the synthetic option is selected
expect(find('sourceValueField').prop('value')).toBe('synthetic');
});
await selectTab('advanced');

it("defaults to 'synthetic' if index mode prop is 'time_series'", async () => {
await act(async () => {
testBed = setup(
{
value: { ...defaultMappings, _source: undefined },
onChange: onChangeHandler,
indexMode: 'time_series',
},
ctx
);
// Check that the synthetic option is selected
expect(find('sourceValueField').prop('value')).toBe('synthetic');
});
testBed.component.update();

const {
actions: { selectTab },
find,
} = testBed;
it(`defaults to 'standard' with ${indexMode} index mode prop on basic license`, async () => {
hasEnterpriseLicense = false;
await act(async () => {
testBed = setup(
{
value: { ...defaultMappings, _source: undefined },
onChange: onChangeHandler,
indexMode,
},
ctx
);
});
testBed.component.update();

await selectTab('advanced');
const {
actions: { selectTab },
find,
} = testBed;

// Check that the synthetic option is selected
expect(find('sourceValueField').prop('value')).toBe('synthetic');
await selectTab('advanced');

// Check that the stored option is selected
expect(find('sourceValueField').prop('value')).toBe('stored');
});
});
});
});
Expand Down

0 comments on commit 4b89cac

Please sign in to comment.