Skip to content

Commit 3a26bdf

Browse files
committed
Merge pull request #38 from NickStefan/fullDOM
Allow rendering into the document.body
2 parents f97380d + bddce53 commit 3a26bdf

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "legit-tests",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "a chainable testing library for React",
55
"main": "legit-tests.js",
66
"scripts": {

src/middleware.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Find from './middleware/find'
22
import SetState from './middleware/setState'
33
import Simulate from './middleware/simulate'
4+
import Clean from './middleware/clean'
45

56
export default {
67
Find,
78
SetState,
8-
Simulate
9+
Simulate,
10+
Clean
911
}

src/middleware/clean.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Clean(){
2+
this.instance = null
3+
this.elements = null
4+
if (global.window){
5+
global.window.document.body.innerHTML = ''
6+
}
7+
}

src/tests.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import TestUtils from 'react-addons-test-utils'
22
import ReactDOMServer from 'react-dom/server'
3+
import ReactDOM from 'react-dom'
34
import React from 'react'
45
global.React = React
56

6-
import { Find, SetState, Simulate } from './middleware'
7+
import { Find, SetState, Simulate, Clean } from './middleware'
78

89
function Test(component, config) {
910

@@ -12,6 +13,10 @@ function Test(component, config) {
1213
const shallowRenderer = TestUtils.createRenderer()
1314
shallowRenderer.render(component)
1415
instance = shallowRenderer.getRenderOutput()
16+
} else if (config && config.fullDOM && global.window) {
17+
var div = global.window.document.createElement('div')
18+
global.window.document.body.appendChild(div)
19+
instance = ReactDOM.render(component, div)
1520
} else {
1621
instance = TestUtils.renderIntoDocument(component)
1722
}
@@ -69,7 +74,8 @@ function Test(component, config) {
6974
return testComponent.mixin({
7075
find: Find,
7176
setState: SetState,
72-
simulate: Simulate
77+
simulate: Simulate,
78+
clean: Clean
7379
})
7480
}
7581

tests/full-dom.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Test from '../src/legit-tests'
2+
import { expect } from 'chai'
3+
4+
describe('Render into document.body', () => {
5+
6+
it('should render and clean up component', () => {
7+
Test(<section />, {fullDOM: true})
8+
.test(function() {
9+
expect(global.window.document.querySelector('section'))
10+
.to.not.equal(null)
11+
})
12+
.clean()
13+
14+
// clean should clean up the document.body
15+
expect(global.window.document.body.innerHTML).to.equal('')
16+
})
17+
18+
})

0 commit comments

Comments
 (0)