Skip to content

Instantly share code, notes, and snippets.

@kpunith8
Last active July 4, 2020 05:38
Show Gist options
  • Save kpunith8/0bf687ef8f7622499b6b8a7dcaba4942 to your computer and use it in GitHub Desktop.
Save kpunith8/0bf687ef8f7622499b6b8a7dcaba4942 to your computer and use it in GitHub Desktop.
React Testing Library Helpers
// A helper for RTL
export const expandSelect = selectInput => {
if (_.toLower(selectInput.tagName) !== 'input') {
selectInput = selectInput.querySelector('input')
}
fireEvent.mouseDown(selectInput)
fireEvent.focus(selectInput)
}
export const showInfoTip = async infoTip => {
const infoTipTrigger = infoTip.querySelector('div')
fireEvent.mouseEnter(infoTipTrigger)
let toolTip
await waitFor(() => {
const toolTipId = infoTipTrigger.getAttribute('aria-describedby')
toolTip = global.document.querySelector(`#${toolTipId}`)
// eslint-disable-next-line no-undef
expect(toolTip).to.be.ok
})
return toolTip
}
// Usage in the test file
const infoTip = await showInfoTip(find('.name-tooltip'))
expect(infoTip.textContent).to.include('Hello There')
export const editTableJSON = table => {
const result = []
_.map(table.rows, row => {
const rowData = {}
_.map(row.cells, cell => {
const className = cell.classList[0]
// Assuming that the cell has the classname ending with '-value'
// <tr><td class="name-value">Name</td><tr>
if (_.endsWith(className, '-value')) {
// Convert className to camelCase so that first-name-value becomes -> firstName
const columnName = _.camelCase(_.replace(className, '-value', ''))
rowData[columnName] = cell.textContent
}
})
result.push(rowData)
})
return _.drop(result)
}
export const editTableJSON = table => {
const result = []
_.map(table.rows, row => {
const rowData = {}
_.map(row.cells, cell => {
const className = cell.classList[0]
// Assuming that the cell has the classname ending with '-value'
// <tr><td class="name-value">Name</td><tr>
if (_.endsWith(className, '-value')) {
// Convert className to camelCase so that first-name-value becomes -> firstName
const columnName = _.camelCase(_.replace(className, '-value', ''))
const select = cell.querySelector('.select-input')
if (select) {
const selectedText = select.querySelector('.single-value') // SingleValue Component of react-select
.textContent
rowData[columnName] = selectedText
} else {
rowData[columnName] = cell.textContent
}
}
})
result.push(rowData)
})
return _.drop(result)
}
@kpunith8
Copy link
Author

kpunith8 commented May 8, 2020

This was useful when firing an event for table rows with React-Testing-Library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment