Skip to content

Commit

Permalink
Added SampleRequestLogic with mock api request
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed Apr 1, 2021
1 parent 7611a7d commit 3a83725
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,55 @@
* 2.0.
*/

import { setMockActions, setMockValues } from '../../../../__mocks__';

import React from 'react';

import { shallow } from 'enzyme';

import { EuiCodeBlock, EuiFieldSearch } from '@elastic/eui';

import { SampleResponse } from './sample_response';

describe('SampleResponse', () => {
it('renders', () => {
const actions = {
queryChanged: jest.fn(),
};

const values = {
query: 'foo',
response: {
bar: 'baz',
},
};

beforeEach(() => {
jest.clearAllMocks();
setMockActions(actions);
setMockValues(values);
});

it('renders a text box with the current user "query" value from state', () => {
const wrapper = shallow(<SampleResponse />);
expect(wrapper.find(EuiFieldSearch).prop('value')).toEqual('foo');
});

it('updates the "query" value in state when a user updates the text in the text box', () => {
const wrapper = shallow(<SampleResponse />);
wrapper.find(EuiFieldSearch).simulate('change', { target: { value: 'bar' } });
expect(actions.queryChanged).toHaveBeenCalledWith('bar');
});

it('renders the response from the given user "query" in a code block', () => {
const wrapper = shallow(<SampleResponse />);
expect(wrapper.find(EuiCodeBlock).prop('children')).toEqual('{\n "bar": "baz"\n}');
});

it('renders an empty string the code block if no response exists yet', () => {
setMockValues({
response: null,
});
const wrapper = shallow(<SampleResponse />);
expect(wrapper.isEmptyRender()).toBe(false);
expect(wrapper.find(EuiCodeBlock).prop('children')).toEqual('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import React from 'react';

import { useActions, useValues } from 'kea';

import {
EuiCodeBlock,
EuiFieldSearch,
Expand All @@ -18,7 +20,12 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { SampleResponseLogic } from './sample_response_logic';

export const SampleResponse: React.FC = () => {
const { query, response } = useValues(SampleResponseLogic);
const { queryChanged } = useActions(SampleResponseLogic);

return (
<EuiPanel hasShadow>
<EuiFlexGroup alignItems="center">
Expand All @@ -38,7 +45,8 @@ export const SampleResponse: React.FC = () => {
</EuiFlexGroup>
<EuiSpacer />
<EuiFieldSearch
// TODO onChange={(e) => setQuery(e.target.value || '')}
value={query}
onChange={(e) => queryChanged(e.target.value)}
placeholder={i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.resultSettings.sampleResponse.inputPlaceholder',
{ defaultMessage: 'Type a search query to test a response...' }
Expand All @@ -47,55 +55,9 @@ export const SampleResponse: React.FC = () => {
/>
<EuiSpacer />
<EuiCodeBlock language="json" whiteSpace="pre-wrap">
{/* TODO Replace this static content with dynamic content */}
{JSON.stringify(
{
visitors: {
raw: 776218,
},
nps_image_url: {
raw:
'https://www.nps.gov/common/uploads/banner_image/imr/homepage/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
square_km: {
raw: 1366.2,
},
world_heritage_site: {
raw: 'false',
},
date_established: {
raw: '1964-09-12T05:00:00+00:00',
},
image_url: {
raw:
'https://storage.googleapis.com/public-demo-assets.swiftype.info/swiftype-dot-com-search-ui-national-parks-demo/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
description: {
raw:
'This landscape was eroded into a maze of canyons, buttes, and mesas by the combined efforts of the Colorado River, Green River, and their tributaries, which divide the park into three districts. The park also contains rock pinnacles and arches, as well as artifacts from Ancient Pueblo peoples.',
},
location: {
raw: '38.2,-109.93',
},
acres: {
raw: '337597.83',
},
title: {
raw: 'Canyonlands',
},
nps_link: {
raw: 'https://www.nps.gov/cany/index.htm',
},
states: {
raw: ['Utah'],
},
id: {
raw: 'park_canyonlands',
},
},
null,
2
)}
{/* TODO No results messsage */}
{/* TODO Query on load */}
{response ? JSON.stringify(response, null, 2) : ''}
</EuiCodeBlock>
</EuiPanel>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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 { nextTick } from '@kbn/test/jest';

import { SampleResponseLogic } from './sample_response_logic';

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

const DEFAULT_VALUES = {
query: '',
response: null,
isLoading: false,
};

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

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

describe('actions', () => {
describe('queryChanged', () => {
it('updates query and sets isLoading to true', () => {
mount({
query: {},
isLoading: false,
});

SampleResponseLogic.actions.queryChanged('foo');

expect(SampleResponseLogic.values).toEqual({
...DEFAULT_VALUES,
query: 'foo',
isLoading: true,
});
});
});

describe('getSearchResultsSuccess', () => {
it('sets the response from a search API request and sets isLoading to false', () => {
mount({
response: null,
isLoading: true,
});

SampleResponseLogic.actions.getSearchResultsSuccess({});

expect(SampleResponseLogic.values).toEqual({
...DEFAULT_VALUES,
response: {},
isLoading: false,
});
});
});

describe('getSearchResultsFailure', () => {
it('sets isLoading to false', () => {
mount({
isLoading: true,
});

SampleResponseLogic.actions.getSearchResultsFailure();

expect(SampleResponseLogic.values).toEqual({
...DEFAULT_VALUES,
isLoading: false,
});
});
});
});

describe('listeners', () => {
describe('queryChanged', () => {
// TODO it('makes an API request')

it('calls getSearchResultsSuccess with the result of the response from the search API request', async () => {
mount();
jest.spyOn(SampleResponseLogic.actions, 'getSearchResultsSuccess');

SampleResponseLogic.actions.queryChanged('foo');
await nextTick();

expect(SampleResponseLogic.actions.getSearchResultsSuccess).toHaveBeenCalledWith({
visitors: {
raw: 776218,
},
nps_image_url: {
raw:
'https://www.nps.gov/common/uploads/banner_image/imr/homepage/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
square_km: {
raw: 1366.2,
},
world_heritage_site: {
raw: 'false',
},
date_established: {
raw: '1964-09-12T05:00:00+00:00',
},
image_url: {
raw:
'https://storage.googleapis.com/public-demo-assets.swiftype.info/swiftype-dot-com-search-ui-national-parks-demo/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
description: {
raw:
'This landscape was eroded into a maze of canyons, buttes, and mesas by the combined efforts of the Colorado River, Green River, and their tributaries, which divide the park into three districts. The park also contains rock pinnacles and arches, as well as artifacts from Ancient Pueblo peoples.',
},
location: {
raw: '38.2,-109.93',
},
acres: {
raw: '337597.83',
},
title: {
raw: 'Canyonlands',
},
nps_link: {
raw: 'https://www.nps.gov/cany/index.htm',
},
states: {
raw: ['Utah'],
},
id: {
raw: 'park_canyonlands',
},
});
});

// TODO it('handles errors')
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 { kea, MakeLogicType } from 'kea';

import { SampleSearchResponse } from '../types';

interface SampleResponseValues {
query: string;
response?: SampleSearchResponse;
isLoading: boolean;
}

interface SampleResponseActions {
queryChanged: (query: string) => { query: string };
getSearchResultsSuccess: (response: SampleSearchResponse) => { response: SampleSearchResponse };
getSearchResultsFailure: () => void;
}

export const SampleResponseLogic = kea<MakeLogicType<SampleResponseValues, SampleResponseActions>>({
path: ['enterprise_search', 'app_search', 'sample_response_logic'],
actions: {
queryChanged: (query) => ({ query }),
getSearchResultsSuccess: (response) => ({ response }),
getSearchResultsFailure: true,
},
reducers: {
query: ['', { queryChanged: (_, { query }) => query }],
response: [null, { getSearchResultsSuccess: (_, { response }) => response }],
isLoading: [
false,
{
queryChanged: () => true,
getSearchResultsSuccess: () => false,
getSearchResultsFailure: () => false,
},
],
},
listeners: ({ actions }) => ({
queryChanged: async () => {
try {
const response = await Promise.resolve({
visitors: {
raw: 776218,
},
nps_image_url: {
raw:
'https://www.nps.gov/common/uploads/banner_image/imr/homepage/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
square_km: {
raw: 1366.2,
},
world_heritage_site: {
raw: 'false',
},
date_established: {
raw: '1964-09-12T05:00:00+00:00',
},
image_url: {
raw:
'https://storage.googleapis.com/public-demo-assets.swiftype.info/swiftype-dot-com-search-ui-national-parks-demo/9E7FC0DB-1DD8-B71B-0BC3880DC2250415.jpg',
},
description: {
raw:
'This landscape was eroded into a maze of canyons, buttes, and mesas by the combined efforts of the Colorado River, Green River, and their tributaries, which divide the park into three districts. The park also contains rock pinnacles and arches, as well as artifacts from Ancient Pueblo peoples.',
},
location: {
raw: '38.2,-109.93',
},
acres: {
raw: '337597.83',
},
title: {
raw: 'Canyonlands',
},
nps_link: {
raw: 'https://www.nps.gov/cany/index.htm',
},
states: {
raw: ['Utah'],
},
id: {
raw: 'park_canyonlands',
},
});
actions.getSearchResultsSuccess(response as SampleSearchResponse);
} catch (error) {
actions.getSearchResultsFailure();
}
},
}),
});
Loading

0 comments on commit 3a83725

Please sign in to comment.