Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/role-helpers.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`logRoles logs expected roles for various dom nodes 1`] = `
exports[`logRoles calls console.log with output from prettyRoles 1`] = `
"region:

<section
Expand Down
16 changes: 10 additions & 6 deletions src/__tests__/role-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import jestSerializerAnsi from 'jest-serializer-ansi'
import {getRoles, logRoles, getImplicitAriaRoles} from '../role-helpers'
import {render, cleanup} from './helpers/test-utils'

Expand Down Expand Up @@ -132,13 +131,18 @@ test('getRoles returns expected roles for various dom nodes', () => {
})
})

test('logRoles logs expected roles for various dom nodes', () => {
Copy link
Member

Choose a reason for hiding this comment

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

To test calls to console.log you could do something like this:

jest.spyOn(console, 'log').mockImplementationOnce(() => {})

const {section} = setup()
logRoles(section)

expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchSnapshot()

console.log.mockRestore()

Copy link
Member

Choose a reason for hiding this comment

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

Nice! Looks like you figured it out without my help. Two things:

  1. Let's move that addSnapshotSerializer stuff to the setup-env.js file.
  2. Make sure to call mockRestore()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Handled 5fefa1

expect.addSnapshotSerializer(jestSerializerAnsi)

test('logRoles calls console.log with output from prettyRoles', () => {
const {section} = setup()
const output = logRoles(section)

expect(output).toMatchSnapshot()
jest.spyOn(console, 'log').mockImplementationOnce(() => {})

logRoles(section)
// eslint-disable-next-line no-console
expect(console.log).toHaveBeenCalledTimes(1)
// eslint-disable-next-line no-console
expect(console.log.mock.calls[0][0]).toMatchSnapshot()
// eslint-disable-next-line no-console
console.log.mockRestore()
})

test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getRoles(container) {
function logRoles(container) {
const roles = getRoles(container)

return Object.entries(roles)
const rolesStr = Object.entries(roles)
.map(([role, elements]) => {
const delimiterBar = '-'.repeat(50)
const elementsString = elements
Expand All @@ -84,6 +84,9 @@ function logRoles(container) {
return `${role}:\n\n${elementsString}\n\n${delimiterBar}`
})
.join('\n')

// eslint-disable-next-line no-console
console.log(rolesStr)
}

export {getRoles, logRoles, getImplicitAriaRoles}
3 changes: 3 additions & 0 deletions tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import 'jest-dom/extend-expect'
import jestSerializerAnsi from 'jest-serializer-ansi'

expect.addSnapshotSerializer(jestSerializerAnsi)