Skip to content

Commit

Permalink
Extract log-dom tests
Browse files Browse the repository at this point in the history
They hide console logs which we shouldn't do for pretty-dom since that shouldn't console.log in the first place
  • Loading branch information
eps1lon committed Jul 5, 2024
1 parent 76cb73d commit 5cbf16b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
56 changes: 56 additions & 0 deletions src/__tests__/log-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {getUserCodeFrame} from '../get-user-code-frame'
import {logDOM} from '../pretty-dom'
import {render} from './helpers/test-utils'

jest.mock('../get-user-code-frame')

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

afterEach(() => {
console.log.mockRestore()
})

test('logDOM logs prettyDOM to the console', () => {
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
`)
})

test('logDOM logs prettyDOM with code frame to the console', () => {
getUserCodeFrame.mockImplementationOnce(
() => `"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`,
)
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`)
})
56 changes: 1 addition & 55 deletions src/__tests__/pretty-dom.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import {prettyDOM, logDOM} from '../pretty-dom'
import {getUserCodeFrame} from '../get-user-code-frame'
import {prettyDOM} from '../pretty-dom'
import {render, renderIntoDocument} from './helpers/test-utils'

jest.mock('../get-user-code-frame')

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

afterEach(() => {
console.log.mockRestore()
})

test('prettyDOM prints out the given DOM element tree', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -58,49 +47,6 @@ test('prettyDOM supports receiving the document element', () => {
`)
})

test('logDOM logs prettyDOM to the console', () => {
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
`)
})

test('logDOM logs prettyDOM with code frame to the console', () => {
getUserCodeFrame.mockImplementationOnce(
() => `"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`,
)
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`)
})

describe('prettyDOM fails with first parameter without outerHTML field', () => {
test('with array', () => {
expect(() => prettyDOM(['outerHTML'])).toThrowErrorMatchingInlineSnapshot(
Expand Down

0 comments on commit 5cbf16b

Please sign in to comment.