Skip to content

Commit

Permalink
[Lens] move from slice to reducers/actions and simplify loading (elas…
Browse files Browse the repository at this point in the history
…tic#113324) (elastic#113740)

* structure changes

* tests & fix for sessionId

* share mocks in time_range_middleware

* make switchVisualization and selectSuggestion one reducer as it's very similar

* CR

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Marta Bondyra <[email protected]>
  • Loading branch information
kibanamachine and mbondyra authored Oct 4, 2021
1 parent f659702 commit d5cb31a
Show file tree
Hide file tree
Showing 21 changed files with 845 additions and 1,008 deletions.
48 changes: 28 additions & 20 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { App } from './app';
import { LensAppProps, LensAppServices } from './types';
import { EditorFrameInstance, EditorFrameProps } from '../types';
import { Document } from '../persistence';
import { visualizationMap, datasourceMap, makeDefaultServices, mountWithProvider } from '../mocks';
import {
visualizationMap,
datasourceMap,
makeDefaultServices,
mountWithProvider,
mockStoreDeps,
} from '../mocks';
import { I18nProvider } from '@kbn/i18n/react';
import {
SavedObjectSaveModal,
Expand Down Expand Up @@ -92,9 +98,11 @@ describe('Lens App', () => {
};
}

const makeDefaultServicesForApp = () => makeDefaultServices(sessionIdSubject, 'sessionId-1');

async function mountWith({
props = makeDefaultProps(),
services = makeDefaultServices(sessionIdSubject),
services = makeDefaultServicesForApp(),
preloadedState,
}: {
props?: jest.Mocked<LensAppProps>;
Expand All @@ -110,11 +118,11 @@ describe('Lens App', () => {
</I18nProvider>
);
};

const storeDeps = mockStoreDeps({ lensServices: services });
const { instance, lensStore } = await mountWithProvider(
<App {...props} />,
{
data: services.data,
storeDeps,
preloadedState,
},
{ wrappingComponent }
Expand Down Expand Up @@ -144,7 +152,7 @@ describe('Lens App', () => {
});

it('updates global filters with store state', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
const indexPattern = { id: 'index1' } as unknown as IndexPattern;
const pinnedField = { name: 'pinnedField' } as unknown as FieldSpec;
const pinnedFilter = esFilters.buildExistsFilter(pinnedField, indexPattern);
Expand Down Expand Up @@ -216,7 +224,7 @@ describe('Lens App', () => {

it('sets originatingApp breadcrumb when the document title changes', async () => {
const props = makeDefaultProps();
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
props.incomingState = { originatingApp: 'coolContainer' };
services.getOriginatingAppName = jest.fn(() => 'The Coolest Container Ever Made');

Expand Down Expand Up @@ -262,7 +270,7 @@ describe('Lens App', () => {

describe('TopNavMenu#showDatePicker', () => {
it('shows date picker if any used index pattern isTimeBased', async () => {
const customServices = makeDefaultServices(sessionIdSubject);
const customServices = makeDefaultServicesForApp();
customServices.data.indexPatterns.get = jest
.fn()
.mockImplementation((id) =>
Expand All @@ -275,7 +283,7 @@ describe('Lens App', () => {
);
});
it('shows date picker if active datasource isTimeBased', async () => {
const customServices = makeDefaultServices(sessionIdSubject);
const customServices = makeDefaultServicesForApp();
customServices.data.indexPatterns.get = jest
.fn()
.mockImplementation((id) =>
Expand All @@ -290,7 +298,7 @@ describe('Lens App', () => {
);
});
it('does not show date picker if index pattern nor active datasource is not time based', async () => {
const customServices = makeDefaultServices(sessionIdSubject);
const customServices = makeDefaultServicesForApp();
customServices.data.indexPatterns.get = jest
.fn()
.mockImplementation((id) =>
Expand Down Expand Up @@ -337,7 +345,7 @@ describe('Lens App', () => {
);
});
it('handles rejected index pattern', async () => {
const customServices = makeDefaultServices(sessionIdSubject);
const customServices = makeDefaultServicesForApp();
customServices.data.indexPatterns.get = jest
.fn()
.mockImplementation((id) => Promise.reject({ reason: 'Could not locate that data view' }));
Expand Down Expand Up @@ -385,7 +393,7 @@ describe('Lens App', () => {
: undefined,
};

const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.attributeService.wrapAttributes = jest
.fn()
.mockImplementation(async ({ savedObjectId }) => ({
Expand Down Expand Up @@ -419,7 +427,7 @@ describe('Lens App', () => {
}

it('shows a disabled save button when the user does not have permissions', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.application = {
...services.application,
capabilities: {
Expand Down Expand Up @@ -469,7 +477,7 @@ describe('Lens App', () => {

it('Shows Save and Return and Save As buttons in create by value mode with originating app', async () => {
const props = makeDefaultProps();
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.dashboardFeatureFlag = { allowByValueEmbeddables: true };
props.incomingState = {
originatingApp: 'ultraDashboard',
Expand Down Expand Up @@ -618,7 +626,7 @@ describe('Lens App', () => {
const mockedConsoleDir = jest.spyOn(console, 'dir'); // mocked console.dir to avoid messages in the console when running tests
mockedConsoleDir.mockImplementation(() => {});

const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.attributeService.wrapAttributes = jest
.fn()
.mockRejectedValue({ message: 'failed' });
Expand Down Expand Up @@ -692,7 +700,7 @@ describe('Lens App', () => {
});

it('checks for duplicate title before saving', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.attributeService.wrapAttributes = jest
.fn()
.mockReturnValue(Promise.resolve({ savedObjectId: '123' }));
Expand Down Expand Up @@ -759,7 +767,7 @@ describe('Lens App', () => {
});

it('should still be enabled even if the user is missing save permissions', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.application = {
...services.application,
capabilities: {
Expand Down Expand Up @@ -799,7 +807,7 @@ describe('Lens App', () => {
});

it('should open inspect panel', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
const { instance } = await mountWith({ services, preloadedState: { isSaveable: true } });

await runInspect(instance);
Expand Down Expand Up @@ -943,7 +951,7 @@ describe('Lens App', () => {

describe('saved query handling', () => {
it('does not allow saving when the user is missing the saveQuery permission', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.application = {
...services.application,
capabilities: {
Expand Down Expand Up @@ -1136,7 +1144,7 @@ describe('Lens App', () => {

it('updates the state if session id changes from the outside', async () => {
const sessionIdS = new Subject<string>();
const services = makeDefaultServices(sessionIdS);
const services = makeDefaultServices(sessionIdS, 'sessionId-1');
const { lensStore } = await mountWith({ props: undefined, services });

act(() => {
Expand Down Expand Up @@ -1180,7 +1188,7 @@ describe('Lens App', () => {
});

it('does not confirm if the user is missing save permissions', async () => {
const services = makeDefaultServices(sessionIdSubject);
const services = makeDefaultServicesForApp();
services.application = {
...services.application,
capabilities: {
Expand Down
Loading

0 comments on commit d5cb31a

Please sign in to comment.