From 3ac435b4c14373dc5361a37f37495af9f0c02b0f Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 30 Jan 2024 17:22:35 +0800 Subject: [PATCH] refactor: move setStorageItem to helpers and change to getElementByTestId Signed-off-by: Lin Wang --- .../conversation_history_spec.js | 62 +++++++++---------- .../plugins/dashboards-assistant/helpers.js | 16 +++++ 2 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 cypress/utils/plugins/dashboards-assistant/helpers.js diff --git a/cypress/integration/plugins/dashboards-assistant/conversation_history_spec.js b/cypress/integration/plugins/dashboards-assistant/conversation_history_spec.js index fc9198fcc..0349481b9 100644 --- a/cypress/integration/plugins/dashboards-assistant/conversation_history_spec.js +++ b/cypress/integration/plugins/dashboards-assistant/conversation_history_spec.js @@ -3,18 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ import { BASE_PATH } from '../../../utils/constants'; - -const setLocalStorageItem = (key, value) => { - const oldValue = localStorage.getItem(key); - localStorage.setItem(key, value); - return () => { - if (oldValue === null) { - localStorage.removeItem(key); - } else { - localStorage.setItem(key, oldValue); - } - }; -}; +import { setStorageItem } from '../../../utils/plugins/dashboards-assistant/helpers'; if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { describe('Assistant conversation history spec', () => { @@ -23,9 +12,14 @@ if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { before(() => { // Set welcome screen tracking to false - restoreShowHome = setLocalStorageItem('home:welcome:show', 'false'); + restoreShowHome = setStorageItem( + localStorage, + 'home:welcome:show', + 'false' + ); // Hide new theme modal - restoreNewThemeModal = setLocalStorageItem( + restoreNewThemeModal = setStorageItem( + localStorage, 'home:newThemeModal:show', 'false' ); @@ -133,9 +127,9 @@ if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { cy.get('.llm-chat-flyout').contains('Conversations'); conversations.forEach(({ conversationId }) => { - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationId}"]` - ).should('exist'); + cy.getElementByTestId(`chatHistoryItem-${conversationId}`).should( + 'exist' + ); }); cy.contains('What are the indices in my cluster?'); cy.get('.llm-chat-flyout button[aria-label="history"]').click(); @@ -146,8 +140,8 @@ if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { const conversationToLoad = conversations[0]; - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationToLoad.conversationId}"]` + cy.getElementByTestId( + `chatHistoryItem-${conversationToLoad.conversationId}` ) .contains(conversationToLoad.title) .click(); @@ -163,19 +157,21 @@ if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { const conversationToUpdate = conversations[0]; const newTitle = 'New title'; - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationToUpdate.conversationId}"] button[aria-label="Edit conversation name"]` - ).click(); + cy.getElementByTestId( + `chatHistoryItem-${conversationToUpdate.conversationId}` + ) + .find('button[aria-label="Edit conversation name"]') + .click(); cy.contains('Edit conversation name'); cy.get('input[aria-label="Conversation name input"').type(newTitle); - cy.get('button[data-test-subj="confirmModalConfirmButton"]') + cy.getElementByTestId('confirmModalConfirmButton') .contains('Confirm name') .click(); conversationToUpdate.title = newTitle; - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationToUpdate.conversationId}"]` + cy.getElementByTestId( + `chatHistoryItem-${conversationToUpdate.conversationId}` ).contains(conversationToUpdate.title); cy.contains('Edit conversation name', { timeout: 3000 }).should( 'not.exist' @@ -190,19 +186,21 @@ if (Cypress.env('DASHBOARDS_ASSISTANT_ENABLED')) { const conversationToDelete = conversations[0]; - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationToDelete.conversationId}"] button[aria-label="Delete conversation"]` - ).click(); - cy.get('div[data-test-subj="confirmModalTitleText"]').contains( + cy.getElementByTestId( + `chatHistoryItem-${conversationToDelete.conversationId}` + ) + .find('button[aria-label="Delete conversation"]') + .click(); + cy.getElementByTestId('confirmModalTitleText').contains( 'Delete conversation' ); - cy.get('button[data-test-subj="confirmModalConfirmButton"]') + cy.getElementByTestId('confirmModalConfirmButton') .contains('Delete conversation') .click(); - cy.get( - `div[data-test-subj="chatHistoryItem-${conversationToDelete.conversationId}"]` + cy.getElementByTestId( + `chatHistoryItem-${conversationToDelete.conversationId}` ).should('not.exist'); conversations.shift(); diff --git a/cypress/utils/plugins/dashboards-assistant/helpers.js b/cypress/utils/plugins/dashboards-assistant/helpers.js new file mode 100644 index 000000000..53093a530 --- /dev/null +++ b/cypress/utils/plugins/dashboards-assistant/helpers.js @@ -0,0 +1,16 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export const setStorageItem = (storage, key, value) => { + const oldValue = storage.getItem(key); + storage.setItem(key, value); + return () => { + if (oldValue === null) { + storage.removeItem(key); + } else { + storage.setItem(key, oldValue); + } + }; +};