-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SampleRequestLogic with mock api request
- Loading branch information
1 parent
7611a7d
commit 3a83725
Showing
5 changed files
with
299 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...tions/app_search/components/result_settings/sample_response/sample_response_logic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}); | ||
}); | ||
}); |
96 changes: 96 additions & 0 deletions
96
...plications/app_search/components/result_settings/sample_response/sample_response_logic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}, | ||
}), | ||
}); |
Oops, something went wrong.