Skip to content

Commit

Permalink
Made code changes based on comments within the PR. Also added a unit …
Browse files Browse the repository at this point in the history
…test to make sure that the newQueryEditor function in the TabbedSqlEditors component works as intended
  • Loading branch information
cccs-Dustin committed Mar 2, 2022
1 parent 3fcd052 commit c49fb7e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('async actions', () => {
latestQueryId: null,
selectedText: null,
sql: 'SELECT *\nFROM\nWHERE',
title: 'Untitled Query',
title: 'Untitled Query 1',
schemaOptions: [{ value: 'main', label: 'main', title: 'main' }],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ describe('TabbedSqlEditors', () => {
wrapper.instance().props.actions.addQueryEditor.getCall(0).args[0].title,
).toContain('Untitled Query');
});
it('should properly increment query tab name', () => {
wrapper = getWrapper();
sinon.stub(wrapper.instance().props.actions, 'addQueryEditor');

wrapper.instance().newQueryEditor();
expect(
wrapper.instance().props.actions.addQueryEditor.getCall(0).args[0].title,
).toContain('Untitled Query 2');
});
it('should duplicate query editor', () => {
wrapper = getWrapper();
sinon.stub(wrapper.instance().props.actions, 'cloneQueryToNewTab');
Expand Down
14 changes: 3 additions & 11 deletions superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,18 @@ class TabbedSqlEditors extends React.PureComponent {
'-- Note: Unless you save your query, these tabs will NOT persist if you clear your cookies or change browsers.\n\n',
);

let newTitle;
let newTitle = 'Untitled Query 1';

if (this.props.queryEditors.length > 0) {
const untitledQueryNumbers = this.props.queryEditors
.filter(x => x.title.includes('Untitled Query '))
.map(x => x.title.replace('Untitled Query ', ''))
.filter(x => !Number.isNaN(Number(x)));
.filter(x => x.title.match(/^Untitled Query (\d+)$/))
.map(x => x.title.replace('Untitled Query ', ''));
if (untitledQueryNumbers.length > 0) {
// When there are query tabs open, and at least one is called "Untitled Query #"
// Where # is a valid number
const largestNumber = Math.max.apply(null, untitledQueryNumbers);
newTitle = t('Untitled Query %s', largestNumber + 1);
} else {
// When there are query tabs open but none of them are called "Untitled Query #"
// Where # is a valid number
newTitle = 'Untitled Query 1';
}
} else {
// When there are no query tabs currently open
newTitle = 'Untitled Query 1';
}

const qe = {
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const defaultQueryEditor = {
latestQueryId: null,
selectedText: null,
sql: 'SELECT *\nFROM\nWHERE',
title: 'Untitled Query',
title: 'Untitled Query 1',
schemaOptions: [
{
value: 'main',
Expand Down

0 comments on commit c49fb7e

Please sign in to comment.