diff --git a/public/app/core/services/search_srv.ts b/public/app/core/services/search_srv.ts index ae8c7d61cc7..d7c0e75de5b 100644 --- a/public/app/core/services/search_srv.ts +++ b/public/app/core/services/search_srv.ts @@ -38,8 +38,11 @@ export class SearchSrv { return this.backendSrv.search({ dashboardIds: dashIds }).then(result => { return dashIds.map(orderId => { - return this.transformToViewModel(_.find(result, { id: orderId })); - }).filter(item => !item.isStarred); + return _.find(result, { id: orderId }); + }).filter(hit => hit && !hit.isStarred) + .map(hit => { + return this.transformToViewModel(hit); + }); }); } diff --git a/public/app/core/specs/search_srv.jest.ts b/public/app/core/specs/search_srv.jest.ts index ef8e6392bd1..0624618a224 100644 --- a/public/app/core/specs/search_srv.jest.ts +++ b/public/app/core/specs/search_srv.jest.ts @@ -53,6 +53,31 @@ describe('SearchSrv', () => { expect(results[0].items[0].title).toBe('first but second'); expect(results[0].items[1].title).toBe('second but first'); }); + + describe('and 3 recent dashboards removed in backend', () => { + let results; + + beforeEach(() => { + backendSrvMock.search = jest + .fn() + .mockReturnValueOnce( + Promise.resolve([{ id: 2, title: 'two' }, { id: 1, title: 'one' }]), + ) + .mockReturnValue(Promise.resolve([])); + + impressionSrv.getDashboardOpened = jest.fn().mockReturnValue([4, 5, 1, 2, 3]); + + return searchSrv.search({ query: '' }).then(res => { + results = res; + }); + }); + + it('should return 2 dashboards', () => { + expect(results[0].items.length).toBe(2); + expect(results[0].items[0].id).toBe(1); + expect(results[0].items[1].id).toBe(2); + }); + }); }); describe('With starred dashboards', () => {