Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App Search] Relevance Tuning logic - actions and selectors only, no listeners #89313

Merged
merged 10 commits into from
Feb 4, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export { RELEVANCE_TUNING_TITLE } from './constants';
export { RelevanceTuning } from './relevance_tuning';
export { RelevanceTuningLogic } from './relevance_tuning_logic';
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LogicMounter } from '../../../__mocks__';

import { BoostType } from './types';

import { RelevanceTuningLogic } from './relevance_tuning_logic';

describe('RelevanceTuningLogic', () => {
const { mount } = new LogicMounter(RelevanceTuningLogic);

const searchSettings = {
Copy link
Member Author

@JasonStoltz JasonStoltz Jan 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, I changed the name and overall theme of this file to match its place it the web application, which is "Relevance Tuning", not "Search Settings".

The underlying model that gets updated and persisted is still named SearchSettings to match what we call it on the server. So you will still see the "Search Settings" name used in this file.

boosts: {
foo: [
{
type: 'value' as BoostType,
factor: 5,
},
],
},
search_fields: {},
};
const schema = {};
const schemaConflicts = {};
const relevanceTuningProps = {
searchSettings,
schema,
schemaConflicts,
};
const searchResults = [{}, {}];

const DEFAULT_VALUES = {
dataLoading: true,
schema: {},
schemaConflicts: {},
searchSettings: {},
unsavedChanges: false,
filterInputValue: '',
query: '',
resultsLoading: false,
searchResults: null,
showSchemaConflictCallout: true,
engineHasSchemaFields: false,
schemaFields: [],
schemaFieldsWithConflicts: [],
filteredSchemaFields: [],
filteredSchemaFieldsWithConflicts: [],
};

beforeEach(() => {
jest.clearAllMocks();
});

it('has expected default values', () => {
mount();
expect(RelevanceTuningLogic.values).toEqual(DEFAULT_VALUES);
});

describe('actions', () => {
describe('onInitializeRelevanceTuning', () => {
it('should set searchSettings, schema, & schemaConflicts from the API response, and set dataLoading to false', () => {
mount({
dataLoading: true,
});
RelevanceTuningLogic.actions.onInitializeRelevanceTuning(relevanceTuningProps);

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
searchSettings,
schema,
dataLoading: false,
schemaConflicts,
});
});
});

describe('setSearchSettings', () => {
it('should set setSearchSettings and set unsavedChanges to true', () => {
mount({
unsavedChanges: false,
});
RelevanceTuningLogic.actions.setSearchSettings(searchSettings);

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
searchSettings,
unsavedChanges: true,
});
});
});

describe('setFilterValue', () => {
it('should set filterInputValue', () => {
mount();
RelevanceTuningLogic.actions.setFilterValue('foo');

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
filterInputValue: 'foo',
});
});
});

describe('setSearchQuery', () => {
it('should set query', () => {
mount();
RelevanceTuningLogic.actions.setSearchQuery('foo');

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
query: 'foo',
});
});
});

describe('setSearchResults', () => {
it('should set searchResults and set resultLoading to false', () => {
mount({
resultsLoading: true,
});
RelevanceTuningLogic.actions.setSearchResults(searchResults);

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
searchResults,
resultsLoading: false,
});
});
});

describe('setResultsLoading', () => {
it('should set resultsLoading', () => {
mount({
resultsLoading: false,
});
RelevanceTuningLogic.actions.setResultsLoading(true);

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
resultsLoading: true,
});
});
});

describe('clearSearchResults', () => {
it('should set searchResults', () => {
mount({
searchResults: [{}],
});
RelevanceTuningLogic.actions.clearSearchResults();

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
searchResults: null,
});
});
});

describe('resetSearchSettingsState', () => {
it('should set dataLoading', () => {
mount({
dataLoading: false,
});
RelevanceTuningLogic.actions.resetSearchSettingsState();

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
dataLoading: true,
});
});
});

describe('dismissSchemaConflictCallout', () => {
it('should set showSchemaConflictCallout to false', () => {
mount({
showSchemaConflictCallout: true,
});
RelevanceTuningLogic.actions.dismissSchemaConflictCallout();

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
showSchemaConflictCallout: false,
});
});
});
});

describe('selectors', () => {
describe('engineHasSchemaFields', () => {
it('should return false if there is only a single field in a schema', () => {
// This is because if a schema only has a single field, it is "id", which we do not
// consider a tunable field.
Comment on lines +196 to +197
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 thanks for the explanation!

mount({
schema: {
id: 'foo',
},
});
expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(false);
});

it('should return true otherwise', () => {
mount({
schema: {
id: 'foo',
bar: 'bar',
},
});
expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(true);
});
});

describe('schemaFields', () => {
it('should return the list of field names from the schema', () => {
mount({
schema: {
id: 'foo',
bar: 'bar',
},
});
expect(RelevanceTuningLogic.values.schemaFields).toEqual(['id', 'bar']);
});
});

describe('schemaFieldsWithConflicts', () => {
it('should return the list of field names that have schema conflicts', () => {
mount({
schemaConflicts: {
foo: {
text: ['source_engine_1'],
number: ['source_engine_2'],
},
},
});
expect(RelevanceTuningLogic.values.schemaFieldsWithConflicts).toEqual(['foo']);
});
});

describe('filteredSchemaFields', () => {
it('should return a list of schema field names that contain the text from filterInputValue ', () => {
mount({
filterInputValue: 'ba',
schema: {
id: 'string',
foo: 'string',
bar: 'string',
baz: 'string',
},
});
expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual(['bar', 'baz']);
});

it('should return all schema fields if there is no filter applied', () => {
mount({
filterTerm: '',
schema: {
id: 'string',
foo: 'string',
bar: 'string',
baz: 'string',
},
});
expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual([
'id',
'foo',
'bar',
'baz',
]);
});
});

describe('filteredSchemaFieldsWithConflicts', () => {
it('should return a list of schema field names that contain the text from filterInputValue, and if that field has a schema conflict', () => {
mount({
filterInputValue: 'ba',
schema: {
id: 'string',
foo: 'string',
bar: 'string',
baz: 'string',
},
schemaConflicts: {
bar: {
text: ['source_engine_1'],
number: ['source_engine_2'],
},
},
});
expect(RelevanceTuningLogic.values.filteredSchemaFieldsWithConflicts).toEqual(['bar']);
});
});
});
});
Loading