Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { shallow } from 'enzyme';

import { EngineIcon } from './engine_icon';
import { MetaEngineIcon } from './meta_engine_icon';

describe('Engines icons', () => {
it('renders an engine icon', () => {
const wrapper = shallow(<EngineIcon />);
expect(wrapper.hasClass('engineIcon')).toBe(true);
});

it('renders a meta engine icon', () => {
const wrapper = shallow(<MetaEngineIcon />);
expect(wrapper.hasClass('engineIcon')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

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

jest.mock('../../../shared/http', () => ({
HttpLogic: { values: { http: { get: jest.fn() } } },
}));
import { HttpLogic } from '../../../shared/http';

import { EngineDetails } from '../engine/types';
import { EnginesLogic } from './';

describe('EnginesLogic', () => {
const DEFAULT_VALUES = {
dataLoading: true,
engines: [],
enginesTotal: 0,
enginesPage: 1,
metaEngines: [],
metaEnginesTotal: 0,
metaEnginesPage: 1,
};

const MOCK_ENGINE = {
name: 'hello-world',
created_at: 'Fri, 1 Jan 1970 12:00:00 +0000',
document_count: 50,
field_count: 10,
} as EngineDetails;
const MOCK_ENGINES_API_RESPONSE = {
results: [MOCK_ENGINE],
meta: {
page: {
current: 1,
total_pages: 10,
total_results: 100,
size: 10,
},
},
};

const { mount } = new LogicMounter(EnginesLogic);

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

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

describe('actions', () => {
describe('onEnginesLoad', () => {
describe('dataLoading', () => {
it('should be set to false', () => {
mount();
EnginesLogic.actions.onEnginesLoad({ engines: [], total: 0 });

expect(EnginesLogic.values).toEqual({
...DEFAULT_VALUES,
dataLoading: false,
});
Comment on lines 64 to 67
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Jason also implemented this method of expecting/asserting against ALL SomeLogic.values (as opposed to just SomeLogic.values.specificValues), as a way of regression testing against reducer changes that we make and forget to write tests/assertions for. It's been working really well just IMO and has helped us catch bugs.

});
});

describe('engines & enginesTotal', () => {
it('should be set to the provided value', () => {
mount();
EnginesLogic.actions.onEnginesLoad({ engines: [MOCK_ENGINE], total: 100 });

expect(EnginesLogic.values).toEqual({
...DEFAULT_VALUES,
dataLoading: false,
engines: [MOCK_ENGINE],
enginesTotal: 100,
});
});
});
});

describe('onMetaEnginesLoad', () => {
describe('engines & enginesTotal', () => {
it('should be set to the provided value', () => {
mount();
EnginesLogic.actions.onMetaEnginesLoad({ engines: [MOCK_ENGINE], total: 1 });

expect(EnginesLogic.values).toEqual({
...DEFAULT_VALUES,
metaEngines: [MOCK_ENGINE],
metaEnginesTotal: 1,
});
});
});
});

describe('onEnginesPagination', () => {
describe('enginesPage', () => {
it('should be set to the provided value', () => {
mount();
EnginesLogic.actions.onEnginesPagination(2);

expect(EnginesLogic.values).toEqual({
...DEFAULT_VALUES,
enginesPage: 2,
});
});
});
});

describe('onMetaEnginesPagination', () => {
describe('metaEnginesPage', () => {
it('should be set to the provided value', () => {
mount();
EnginesLogic.actions.onMetaEnginesPagination(99);

expect(EnginesLogic.values).toEqual({
...DEFAULT_VALUES,
metaEnginesPage: 99,
});
});
});
});

describe('loadEngines', () => {
it('should call the engines API endpoint and set state based on the results', async () => {
const promise = Promise.resolve(MOCK_ENGINES_API_RESPONSE);
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
mount({ enginesPage: 10 });
jest.spyOn(EnginesLogic.actions, 'onEnginesLoad');

EnginesLogic.actions.loadEngines();
await promise;

expect(HttpLogic.values.http.get).toHaveBeenCalledWith('/api/app_search/engines', {
query: { type: 'indexed', pageIndex: 10 },
});
expect(EnginesLogic.actions.onEnginesLoad).toHaveBeenCalledWith({
engines: [MOCK_ENGINE],
total: 100,
});
});
});

describe('loadMetaEngines', () => {
it('should call the engines API endpoint and set state based on the results', async () => {
const promise = Promise.resolve(MOCK_ENGINES_API_RESPONSE);
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
mount({ metaEnginesPage: 99 });
jest.spyOn(EnginesLogic.actions, 'onMetaEnginesLoad');

EnginesLogic.actions.loadMetaEngines();
await promise;

expect(HttpLogic.values.http.get).toHaveBeenCalledWith('/api/app_search/engines', {
query: { type: 'meta', pageIndex: 99 },
});
expect(EnginesLogic.actions.onMetaEnginesLoad).toHaveBeenCalledWith({
engines: [MOCK_ENGINE],
total: 100,
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { kea, MakeLogicType } from 'kea';

import { HttpLogic } from '../../../shared/http';

import { EngineDetails } from '../engine/types';

interface EnginesValues {
dataLoading: boolean;
engines: EngineDetails[];
enginesTotal: number;
enginesPage: number;
metaEngines: EngineDetails[];
metaEnginesTotal: number;
metaEnginesPage: number;
}

interface OnEnginesLoad {
engines: EngineDetails[];
total: number;
}
interface EnginesActions {
onEnginesLoad({ engines, total }: OnEnginesLoad): OnEnginesLoad;
onMetaEnginesLoad({ engines, total }: OnEnginesLoad): OnEnginesLoad;
onEnginesPagination(page: number): { page: number };
onMetaEnginesPagination(page: number): { page: number };
loadEngines(): void;
loadMetaEngines(): void;
}

export const EnginesLogic = kea<MakeLogicType<EnginesValues, EnginesActions>>({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not a 1:1 translation from ent-search's EnginesLogic b/c the view/table component is actually fairly different from the standalone UI. Quick sparknotes of changes:

  • Meta engines is on this view as well, not just source engines
  • Flash messages is now a global state, not per-view
  • We currently don't have engines filtering in, I'm not sure if this is something Davey eventually wants post-MVP

We're also missing a delete engine/create engine listener which I assume we'll add at some point (I guess create engine could use a different logic file actually on second thought?)

path: ['enterprise_search', 'app_search', 'engines_logic'],
actions: {
onEnginesLoad: ({ engines, total }) => ({ engines, total }),
onMetaEnginesLoad: ({ engines, total }) => ({ engines, total }),
onEnginesPagination: (page) => ({ page }),
onMetaEnginesPagination: (page) => ({ page }),
loadEngines: true,
loadMetaEngines: true,
},
reducers: {
dataLoading: [
true,
{
onEnginesLoad: () => false,
},
],
engines: [
[],
{
onEnginesLoad: (_, { engines }) => engines,
},
],
enginesTotal: [
0,
{
onEnginesLoad: (_, { total }) => total,
},
],
enginesPage: [
1,
{
onEnginesPagination: (_, { page }) => page,
},
],
metaEngines: [
[],
{
onMetaEnginesLoad: (_, { engines }) => engines,
},
],
metaEnginesTotal: [
0,
{
onMetaEnginesLoad: (_, { total }) => total,
},
],
metaEnginesPage: [
1,
{
onMetaEnginesPagination: (_, { page }) => page,
},
],
},
listeners: ({ actions, values }) => ({
loadEngines: async () => {
const { http } = HttpLogic.values;
const { enginesPage } = values;

const response = await http.get('/api/app_search/engines', {
query: { type: 'indexed', pageIndex: enginesPage },
});
actions.onEnginesLoad({
engines: response.results,
total: response.meta.page.total_results,
});
},
loadMetaEngines: async () => {
const { http } = HttpLogic.values;
const { metaEnginesPage } = values;

const response = await http.get('/api/app_search/engines', {
query: { type: 'meta', pageIndex: metaEnginesPage },
});
actions.onMetaEnginesLoad({
engines: response.results,
total: response.meta.page.total_results,
});
},
}),
});
Loading