From 63b6d349d65bf955ddd30e5d53117b8252c90151 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 28 Jun 2022 17:02:26 +0500 Subject: [PATCH] Add E2E for Custom Results Feature --- .../features/custom-results.spec.js | 77 +++++++++++++++++++ tests/cypress/support/commands.js | 47 +++++++++++ 2 files changed, 124 insertions(+) create mode 100644 tests/cypress/integration/features/custom-results.spec.js diff --git a/tests/cypress/integration/features/custom-results.spec.js b/tests/cypress/integration/features/custom-results.spec.js new file mode 100644 index 0000000000..eb904ef9ff --- /dev/null +++ b/tests/cypress/integration/features/custom-results.spec.js @@ -0,0 +1,77 @@ +describe('Custom Results', () => { + after(() => { + cy.login(); + cy.visitAdminPage('edit.php?post_type=ep-pointer'); + cy.get('#cb-select-all-1').click(); + cy.get('#bulk-action-selector-top').select('trash'); + cy.get('#doaction').click(); + + cy.visitAdminPage('edit.php?post_status=trash&post_type=ep-pointer'); + cy.get('.tablenav.top #delete_all').click(); + }); + + it('Can change post position and verify the result on search', () => { + const searchResult = []; + const searchTerm = 'Feature'; + + cy.login(); + cy.visitAdminPage('post-new.php?post_type=ep-pointer'); + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest'); + + cy.get('#titlewrap input').type(searchTerm); + cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200); + + cy.dragAndDrop( + '.pointers .pointer:first-of-type .dashicons-menu', + '.pointers .pointer:last-of-type .dashicons-menu', + ).then(() => { + cy.get('.pointers .pointer .title').each((post) => { + cy.wrap(post) + .invoke('text') + .then((text) => searchResult.push(text)); + }); + cy.get('#publish').click(); + }); + + cy.visit(`?s=${searchTerm}`); + + cy.get('article .entry-title').each((post, index) => { + cy.wrap(post).invoke('text').should('eq', searchResult[index]); + }); + }); + + it('Can add the post in result and verify the result on search', () => { + const searchResult = []; + const searchTerm = 'Fantastic'; + + cy.login(); + + cy.activatePlugin('woocommerce'); + cy.maybeEnableFeature('woocommerce'); + + cy.visitAdminPage('post-new.php?post_type=ep-pointer'); + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest'); + + cy.get('#titlewrap input').type(searchTerm); + cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200); + + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_search*').as('ajaxRequest'); + cy.get('.search-pointers').type('Small Aluminum Wallet'); + cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200); + + cy.get('.dashicons-plus.add-pointer').click(); + cy.get('.pointers .pointer .title').each((post) => { + cy.wrap(post) + .invoke('text') + .then((text) => searchResult.push(text)); + }); + + cy.get('#publish').click(); + + cy.visit(`?s=${searchTerm}`); + + cy.get('article .entry-title').each((post, index) => { + cy.wrap(post).invoke('text').should('eq', searchResult[index]); + }); + }); +}); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 233db50237..f7a34d5a6f 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -289,3 +289,50 @@ Cypress.Commands.add('deactivatePlugin', (slug, method = 'dashboard', mode = 'si } cy.wpCli(command); }); + +// Command to drag and drop React DnD element. Original code from: https://github.com/cypress-io/cypress/issues/3942#issuecomment-485648100 +Cypress.Commands.add('dragAndDrop', (subject, target) => { + Cypress.log({ + name: 'DRAGNDROP', + message: `Dragging element ${subject} to ${target}`, + consoleProps: () => { + return { + subject, + target, + }; + }, + }); + const BUTTON_INDEX = 0; + const SLOPPY_CLICK_THRESHOLD = 10; + cy.get(target) + .first() + .then(($target) => { + const coordsDrop = $target[0].getBoundingClientRect(); + cy.get(subject) + .first() + .then((subject) => { + const coordsDrag = subject[0].getBoundingClientRect(); + cy.wrap(subject) + .trigger('mousedown', { + button: BUTTON_INDEX, + clientX: coordsDrag.x, + clientY: coordsDrag.y, + force: true, + }) + .trigger('mousemove', { + button: BUTTON_INDEX, + clientX: coordsDrag.x + SLOPPY_CLICK_THRESHOLD, + clientY: coordsDrag.y, + force: true, + }); + cy.get('body') + .trigger('mousemove', { + button: BUTTON_INDEX, + clientX: coordsDrop.x, + clientY: coordsDrop.y, + force: true, + }) + .trigger('mouseup'); + }); + }); +});