-
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.
- Loading branch information
1 parent
5e3af54
commit 1c414d0
Showing
10 changed files
with
316 additions
and
66 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
36 changes: 36 additions & 0 deletions
36
src/ui/public/courier/saved_object/get_title_already_exists.js
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,36 @@ | ||
import _ from 'lodash'; | ||
/** | ||
* Returns true if the given saved object has a title that already exists, false otherwise. Search is case | ||
* insensitive. | ||
* @param savedObject {SavedObject} The object with the title to check. | ||
* @param esAdmin {Object} Used to query es | ||
* @returns {Promise<string|undefined>} Returns the title that matches. Because this search is not case | ||
* sensitive, it may not exactly match the title of the object. | ||
*/ | ||
export function getTitleAlreadyExists(savedObject, esAdmin) { | ||
const { index, title, id } = savedObject; | ||
const esType = savedObject.getEsType(); | ||
if (!title) { | ||
throw new Error('Title must be supplied'); | ||
} | ||
|
||
const body = { | ||
query: { | ||
bool: { | ||
must: { match_phrase: { title } }, | ||
must_not: { match: { id } } | ||
} | ||
} | ||
}; | ||
|
||
// Elastic search will return the most relevant results first, which means exact matches should come | ||
// first, and so we shouldn't need to request everything. Using 10 just to be on the safe side. | ||
const size = 10; | ||
return esAdmin.search({ index, type: esType, body, size }) | ||
.then((response) => { | ||
const match = _.find(response.hits.hits, function currentVersion(hit) { | ||
return hit._source.title.toLowerCase() === title.toLowerCase(); | ||
}); | ||
return match ? match._source.title : undefined; | ||
}); | ||
} |
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
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,86 @@ | ||
import expect from 'expect.js'; | ||
import { bdd } from '../../../support'; | ||
|
||
import PageObjects from '../../../support/page_objects'; | ||
|
||
bdd.describe('dashboard save', function describeIndexTests() { | ||
const dashboardName = 'Dashboard Save Test'; | ||
|
||
bdd.before(async function () { | ||
return PageObjects.dashboard.initTests(); | ||
}); | ||
|
||
bdd.it('warns on duplicate name for new dashboard', async function() { | ||
await PageObjects.dashboard.clickNewDashboard(); | ||
await PageObjects.dashboard.saveDashboard(dashboardName); | ||
|
||
let isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(false); | ||
|
||
await PageObjects.dashboard.gotoDashboardLandingPage(); | ||
await PageObjects.dashboard.clickNewDashboard(); | ||
await PageObjects.dashboard.enterDashboardTitleAndClickSave(dashboardName); | ||
|
||
isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(true); | ||
}); | ||
|
||
bdd.it('does not save on reject confirmation', async function() { | ||
await PageObjects.common.clickCancelOnModal(); | ||
|
||
const countOfDashboards = await PageObjects.dashboard.getDashboardCountWithName(dashboardName); | ||
expect(countOfDashboards).to.equal(1); | ||
}); | ||
|
||
bdd.it('Saves on confirm duplicate title warning', async function() { | ||
await PageObjects.dashboard.gotoDashboardLandingPage(); | ||
await PageObjects.dashboard.clickNewDashboard(); | ||
await PageObjects.dashboard.enterDashboardTitleAndClickSave(dashboardName); | ||
|
||
await PageObjects.common.clickConfirmOnModal(); | ||
|
||
// This is important since saving a new dashboard will cause a refresh of the page. We have to | ||
// wait till it finishes reloading or it might reload the url after simulating the | ||
// dashboard landing page click. | ||
await PageObjects.header.waitUntilLoadingHasFinished(); | ||
|
||
const countOfDashboards = await PageObjects.dashboard.getDashboardCountWithName(dashboardName); | ||
expect(countOfDashboards).to.equal(2); | ||
}); | ||
|
||
bdd.it('Does not warn when you save an existing dashboard with the title it already has, and that title is a duplicate', | ||
async function() { | ||
await PageObjects.dashboard.clickDashboardByLinkText(dashboardName); | ||
await PageObjects.header.isGlobalLoadingIndicatorHidden(); | ||
await PageObjects.dashboard.saveDashboard(dashboardName); | ||
|
||
const isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(false); | ||
} | ||
); | ||
|
||
bdd.it('Warns you when you Save as New Dashboard, and the title is a duplicate', async function() { | ||
await PageObjects.dashboard.enterDashboardTitleAndClickSave(dashboardName, { saveAsNew: true }); | ||
|
||
const isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(true); | ||
|
||
await PageObjects.common.clickCancelOnModal(); | ||
}); | ||
|
||
bdd.it('Does not warn when only the prefix matches', async function() { | ||
await PageObjects.dashboard.saveDashboard(dashboardName.split(' ')[0]); | ||
|
||
const isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(false); | ||
}); | ||
|
||
bdd.it('Warns when case is different', async function() { | ||
await PageObjects.dashboard.enterDashboardTitleAndClickSave(dashboardName.toUpperCase()); | ||
|
||
const isConfirmOpen = await PageObjects.common.isConfirmModalOpen(); | ||
expect(isConfirmOpen).to.equal(true); | ||
|
||
await PageObjects.common.clickCancelOnModal(); | ||
}); | ||
}); |
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
Oops, something went wrong.