-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
164 additions
and
3 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
159 changes: 159 additions & 0 deletions
159
x-pack/plugins/maps/public/embeddable/map_embeddable.test.tsx
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,159 @@ | ||
/* | ||
* 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 { v4 as uuidv4 } from 'uuid'; | ||
import { map, distinctUntilChanged } from 'rxjs/operators'; | ||
import { MapEmbeddable } from './map_embeddable'; | ||
|
||
jest.mock('../kibana_services', () => { | ||
return { | ||
getHttp() { | ||
return { | ||
basePath: { | ||
prepend: (url) => url, | ||
}, | ||
}; | ||
}, | ||
getMapsCapabilities() { | ||
return { save: true }; | ||
}, | ||
getSearchService() { | ||
return { | ||
session: { | ||
getSearchOptions() { | ||
return undefined; | ||
} | ||
} | ||
}; | ||
}, | ||
getShowMapsInspectorAdapter() { | ||
return false; | ||
}, | ||
getTimeFilter() { | ||
return { | ||
getTime() { | ||
return { from: 'now-7d', to: 'now' }; | ||
} | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
jest.mock('../connected_components/map_container', () => { | ||
return { | ||
MapContainer: () => { | ||
return <div>mockLayerTOC</div>; | ||
}, | ||
}; | ||
}); | ||
|
||
import { MapContainer } from '../connected_components/map_container'; | ||
|
||
jest.mock('../routes/map_page', () => { | ||
class MockSavedMap { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
private _store = require('../reducers/store').createMapStore(); | ||
private _attributes = { | ||
title: 'myMap' | ||
}; | ||
|
||
whenReady = async function() {} | ||
|
||
getStore = function() { | ||
return this._store; | ||
} | ||
getAttributes = function() { | ||
return this._attributes; | ||
} | ||
getAutoFitToBounds = function() { | ||
return true; | ||
} | ||
getSharingSavedObjectProps = function() { | ||
return null; | ||
} | ||
} | ||
return { SavedMap: MockSavedMap }; | ||
}); | ||
|
||
function untilInitialized(mapEmbeddable: MapEmbeddable) { | ||
return new Promise((resolve) => { | ||
mapEmbeddable.setInitializationFinished = () => { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
|
||
function onNextTick() { | ||
// wait one tick to give observables time to fire | ||
return new Promise((resolve) => setTimeout(resolve, 0)); | ||
} | ||
|
||
describe('shouldFetch$', () => { | ||
test('should not fetch when search context does not change', async () => { | ||
const mapEmbeddable = new MapEmbeddable( | ||
{}, | ||
{ | ||
id: 'map1', | ||
} | ||
); | ||
await untilInitialized(mapEmbeddable); | ||
|
||
const fetchSpy = jest.spyOn(mapEmbeddable, '_dispatchSetQuery'); | ||
|
||
mapEmbeddable.updateInput({ | ||
title: 'updated map title' | ||
}); | ||
|
||
await onNextTick(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe('on searchSessionId change', () => { | ||
test('should fetch when filterByMapExtent is false', async () => { | ||
const mapEmbeddable = new MapEmbeddable( | ||
{}, | ||
{ | ||
id: 'map1', | ||
filterByMapExtent: false, | ||
} | ||
); | ||
await untilInitialized(mapEmbeddable); | ||
|
||
const fetchSpy = jest.spyOn(mapEmbeddable, '_dispatchSetQuery'); | ||
|
||
mapEmbeddable.updateInput({ | ||
searchSessionId: uuidv4() | ||
}); | ||
|
||
await onNextTick(); | ||
|
||
expect(fetchSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should not fetch when filterByMapExtent is true', async () => { | ||
const mapEmbeddable = new MapEmbeddable( | ||
{}, | ||
{ | ||
id: 'map1', | ||
filterByMapExtent: true, | ||
} | ||
); | ||
await untilInitialized(mapEmbeddable); | ||
|
||
const fetchSpy = jest.spyOn(mapEmbeddable, '_dispatchSetQuery'); | ||
|
||
mapEmbeddable.updateInput({ | ||
searchSessionId: uuidv4() | ||
}); | ||
|
||
await onNextTick(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |