Skip to content

Commit

Permalink
refactor: move setStorageItem to helpers and change to getElementByTe…
Browse files Browse the repository at this point in the history
…stId

Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Jan 30, 2024
1 parent 4f6e751 commit 3ac435b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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'
);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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'
Expand All @@ -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();

Expand Down
16 changes: 16 additions & 0 deletions cypress/utils/plugins/dashboards-assistant/helpers.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
};

0 comments on commit 3ac435b

Please sign in to comment.