tabs.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. describe('SqlLab query tabs', () => {
  20. beforeEach(() => {
  21. cy.visit('/sqllab');
  22. });
  23. const tablistSelector = '[data-test="sql-editor-tabs"] > [role="tablist"]';
  24. const tabSelector = `${tablistSelector} [role="tab"]:not([type="button"])`;
  25. it('allows you to create and close a tab', () => {
  26. cy.get(tabSelector).then(tabs => {
  27. const initialTabCount = tabs.length;
  28. const initialUntitledCount = Math.max(
  29. 0,
  30. ...tabs
  31. .map(
  32. (i, tabItem) =>
  33. Number(tabItem.textContent?.match(/Untitled Query (\d+)/)?.[1]) ||
  34. 0,
  35. )
  36. .toArray(),
  37. );
  38. // add two new tabs
  39. cy.get('[data-test="add-tab-icon"]:visible:last').click({ force: true });
  40. cy.contains('[role="tab"]', `Untitled Query ${initialUntitledCount + 1}`);
  41. cy.get(tabSelector).should('have.length', initialTabCount + 1);
  42. cy.get('[data-test="add-tab-icon"]:visible:last').click({ force: true });
  43. cy.contains('[role="tab"]', `Untitled Query ${initialUntitledCount + 2}`);
  44. cy.get(tabSelector).should('have.length', initialTabCount + 2);
  45. // close the tabs
  46. cy.get(`${tabSelector}:last [data-test="dropdown-trigger"]`).click({
  47. force: true,
  48. });
  49. cy.get('[data-test="close-tab-menu-option"]').click();
  50. cy.get(tabSelector).should('have.length', initialTabCount + 1);
  51. cy.contains('[role="tab"]', `Untitled Query ${initialUntitledCount + 1}`);
  52. cy.get(`${tablistSelector} [aria-label="remove"]:last`).click();
  53. cy.get(tabSelector).should('have.length', initialTabCount);
  54. });
  55. });
  56. it('opens a new tab by a button and a shortcut', () => {
  57. const editorContent = '#ace-editor .ace_content';
  58. const editorInput = '#ace-editor textarea';
  59. const queryLimitSelector = '#js-sql-toolbar .limitDropdown';
  60. cy.get(tabSelector).then(tabs => {
  61. const initialTabCount = tabs.length;
  62. const initialUntitledCount = Math.max(
  63. 0,
  64. ...tabs
  65. .map(
  66. (i, tabItem) =>
  67. Number(tabItem.textContent?.match(/Untitled Query (\d+)/)?.[1]) ||
  68. 0,
  69. )
  70. .toArray(),
  71. );
  72. // configure some editor settings
  73. cy.get(editorInput).type('some random query string', { force: true });
  74. cy.get(queryLimitSelector).parent().click({ force: true });
  75. cy.get('.ant-dropdown-menu')
  76. .last()
  77. .find('.ant-dropdown-menu-item')
  78. .first()
  79. .click({ force: true });
  80. // open a new tab by a button
  81. cy.get('[data-test="add-tab-icon"]:visible:last').click({ force: true });
  82. cy.contains('[role="tab"]', `Untitled Query ${initialUntitledCount + 1}`);
  83. cy.get(tabSelector).should('have.length', initialTabCount + 1);
  84. cy.get(editorContent).contains('SELECT ...');
  85. cy.get(queryLimitSelector).contains('10');
  86. // close the tab
  87. cy.get(`${tabSelector}:last [data-test="dropdown-trigger"]`).click({
  88. force: true,
  89. });
  90. cy.get(`${tablistSelector} [aria-label="remove"]:last`).click({
  91. force: true,
  92. });
  93. cy.get(tabSelector).should('have.length', initialTabCount);
  94. // open a new tab by a shortcut
  95. cy.get('body').type('{ctrl}t');
  96. cy.get(tabSelector).should('have.length', initialTabCount + 1);
  97. cy.contains('[role="tab"]', `Untitled Query ${initialUntitledCount + 1}`);
  98. cy.get(editorContent).contains('SELECT ...');
  99. cy.get(queryLimitSelector).contains('10');
  100. });
  101. });
  102. });