Skip to content

feat(queries): allow overriding queries in getQueriesForElement #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/__tests__/get-queries-for-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {getQueriesForElement} from '../get-queries-for-element'
import {queries} from '..'

test('uses default queries', () => {
const container = document.createElement('div')
const boundQueries = getQueriesForElement(container)
expect(Object.keys(boundQueries)).toEqual(Object.keys(queries))
})

test('accepts custom queries', () => {
const container = document.createElement('div')
const customQuery = jest.fn()
const boundQueries = getQueriesForElement(container, {
customQuery,
})
expect(boundQueries.customQuery).toBeDefined()
})

test('binds functions to container', () => {
const container = document.createElement('div')
const mock = jest.fn()
function customQuery(element) {
return mock(element)
}
const boundQueries = getQueriesForElement(container, {
customQuery,
})
boundQueries.customQuery()
expect(mock).toHaveBeenCalledWith(container)
})

test('accepts an array of queries', () => {
const container = document.createElement('div')
const customQuery = jest.fn()
const boundQueries = getQueriesForElement(container, [
queries,
{
customQuery,
},
])
expect(boundQueries.getAllByText).toBeDefined()
expect(boundQueries.customQuery).toBeDefined()
})
21 changes: 18 additions & 3 deletions src/get-queries-for-element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import * as queries from './queries'
import * as defaultQueries from './queries'

function getQueriesForElement(element) {
return Object.entries(queries).reduce((helpers, [key, fn]) => {
/**
* @typedef {{[key: string]: Function}} FuncMap
*/

/**
* @param {HTMLElement} element container
* @param {FuncMap|FuncMap[]} queries object of functions, or array of objects
* @returns {FuncMap} returns object of functions bound to container
*/
function getQueriesForElement(element, queries = defaultQueries) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that passing custom queries overrides the default instead of merging. If you want to merge you need to import queries and add it to the array, too.

Open to changing this but I think it makes sense; wrappers like react-testing-library's render can merge queries by default on top of this API if they prefer.

let flattenedQueries
if (Array.isArray(queries)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not have this logic here. If someone wants to merge the built in queries they can be responsible for doing that themselves when they call the function.

flattenedQueries = Object.assign({}, ...queries)
} else {
flattenedQueries = queries
}
return Object.entries(flattenedQueries).reduce((helpers, [key, fn]) => {
helpers[key] = fn.bind(null, element)
return helpers
}, {})
Expand Down
3 changes: 3 additions & 0 deletions typings/get-queries-for-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}

export function getQueriesForElement(
element: HTMLElement,
queriesToBind:
| BoundFunctions<typeof queries>
| BoundFunctions<typeof queries>[],
): BoundFunctions<typeof queries>