Skip to content

Commit

Permalink
Write own getTable cypress command
Browse files Browse the repository at this point in the history
Using the `getTable` package has caused our build to fail as it's trying
to install multiple versions of Cypress, so we're adding the
implementation to our codebase instead.

This implementation has been shamelessly copied from
https://github.com/roggerfe/cypress-get-table/blob/master/src/index.js.
  • Loading branch information
Gweaton committed Feb 3, 2021
1 parent 8604425 commit 1c49658
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 24 additions & 0 deletions integration_tests/support/commands.js
Expand Up @@ -6,3 +6,27 @@ Cypress.Commands.add('login', () => {
Cypress.Commands.add('withinFieldsetThatContains', (text, action) => {
cy.contains(text).parent('fieldset').within(action)
})

const getTable = (subject, options = {}) => {
if (subject.get().length > 1) {
throw new Error(`Selector "${subject.selector}" returned more than 1 element.`)
}

const tableElement = subject.get()[0]
const headers = [...tableElement.querySelectorAll('thead th')].map(e => e.textContent)

const rows = [...tableElement.querySelectorAll('tbody tr')].map(row => {
return [...row.querySelectorAll('td')].map(e => e.textContent)
})

return rows.map(row =>
row.reduce((acc, curr, index) => {
if (options.onlyColumns && !options.onlyColumns.includes(headers[index])) {
return { ...acc }
}
return { ...acc, [headers[index]]: curr }
}, {})
)
}

Cypress.Commands.add('getTable', { prevSubject: true }, getTable)
2 changes: 0 additions & 2 deletions integration_tests/support/index.js
@@ -1,5 +1,3 @@
import './commands'
import './interventionsServiceStubs'
import './communityApiStubs'

require('cypress-get-table')

0 comments on commit 1c49658

Please sign in to comment.