Skip to content

Commit

Permalink
moved recently accessed adding into app.tsx so that the embeddable do…
Browse files Browse the repository at this point in the history
…esn't have to use it. Added tests
  • Loading branch information
ThomThomson committed Sep 10, 2020
1 parent 9595722 commit 18139a3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
36 changes: 36 additions & 0 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,29 @@ describe('Lens App', () => {
]);
});

it('adds to the recently viewed list on load', async () => {
const defaultArgs = makeDefaultArgs();
instance = mount(<App {...defaultArgs} />);

(defaultArgs.docStorage.load as jest.Mock).mockResolvedValue({
id: '1234',
title: 'Daaaaaaadaumching!',
state: {
query: 'fake query',
filters: [],
},
references: [],
});
await act(async () => {
instance.setProps({ docId: '1234' });
});
expect(defaultArgs.core.chrome.recentlyAccessed.add).toHaveBeenCalledWith(
'/app/lens#/edit/1234',
'Daaaaaaadaumching!',
'1234'
);
});

it('sets originatingApp breadcrumb when the document title changes', async () => {
const defaultArgs = makeDefaultArgs();
defaultArgs.originatingApp = 'ultraCoolDashboard';
Expand Down Expand Up @@ -591,6 +614,19 @@ describe('Lens App', () => {
expect(args.docStorage.load).not.toHaveBeenCalled();
});

it('adds to the recently viewed list on save', async () => {
const { args } = await save({
initialDocId: undefined,
newCopyOnSave: false,
newTitle: 'hello there',
});
expect(args.core.chrome.recentlyAccessed.add).toHaveBeenCalledWith(
'/app/lens#/edit/aaa',
'hello there',
'aaa'
);
});

it('saves the latest doc as a copy', async () => {
const { args, instance: inst } = await save({
initialDocId: '1234',
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
IndexPatternsContract,
SavedQuery,
} from '../../../../../src/plugins/data/public';
import { getFullPath } from '../../common';

interface State {
indicateNoData: boolean;
Expand Down Expand Up @@ -271,6 +272,7 @@ export function App({
docStorage
.load(docId)
.then((doc) => {
core.chrome.recentlyAccessed.add(getFullPath(docId), doc.title, docId);
getAllIndexPatterns(
_.uniq(
doc.references.filter(({ type }) => type === 'index-pattern').map(({ id }) => id)
Expand Down Expand Up @@ -365,6 +367,7 @@ export function App({
docStorage
.save(doc)
.then(({ id }) => {
core.chrome.recentlyAccessed.add(getFullPath(id), doc.title, id);
// Prevents unnecessary network request and disables save button
const newDoc = { ...doc, id };
const currentOriginatingApp = state.originatingApp;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/mounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function mountApp(
editorFrame={instance}
storage={new Storage(localStorage)}
docId={routeProps.match.params.id}
docStorage={new SavedObjectIndexStore(savedObjectsClient, coreStart.chrome)}
docStorage={new SavedObjectIndexStore(savedObjectsClient)}
redirectTo={(id, returnToOrigin, newlyCreated) =>
redirectTo(routeProps, id, returnToOrigin, newlyCreated)
}
Expand Down
10 changes: 2 additions & 8 deletions x-pack/plugins/lens/public/persistence/saved_object_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
SavedObjectAttributes,
SavedObjectsClientContract,
SavedObjectReference,
ChromeStart,
} from 'kibana/public';
import { Query } from '../../../../../src/plugins/data/public';
import { getFullPath, PersistableFilter } from '../../common';
import { PersistableFilter } from '../../common';

export interface Document {
id?: string;
Expand Down Expand Up @@ -41,7 +40,7 @@ export interface DocumentLoader {
export type SavedObjectStore = DocumentLoader & DocumentSaver;

export class SavedObjectIndexStore implements SavedObjectStore {
constructor(private client: SavedObjectsClientContract, private chrome: ChromeStart) {}
constructor(private client: SavedObjectsClientContract) {}

async save(vis: Document) {
const { id, type, references, ...rest } = vis;
Expand All @@ -54,9 +53,6 @@ export class SavedObjectIndexStore implements SavedObjectStore {
: this.client.create(DOC_TYPE, attributes, {
references,
}));

this.chrome.recentlyAccessed.add(getFullPath(result.id), vis.title, result.id);

return { ...vis, id: result.id };
}

Expand Down Expand Up @@ -95,8 +91,6 @@ export class SavedObjectIndexStore implements SavedObjectStore {
id,
type,
} as Document;
this.chrome.recentlyAccessed.add(getFullPath(id), document.title, id);

return document;
}
}

0 comments on commit 18139a3

Please sign in to comment.