Skip to content

Commit

Permalink
Merge pull request #38 from NickStefan/fullDOM
Browse files Browse the repository at this point in the history
Allow rendering into the document.body
  • Loading branch information
zackify committed Feb 19, 2016
2 parents f97380d + bddce53 commit 3a26bdf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "legit-tests",
"version": "1.0.0",
"version": "1.1.0",
"description": "a chainable testing library for React",
"main": "legit-tests.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Find from './middleware/find'
import SetState from './middleware/setState'
import Simulate from './middleware/simulate'
import Clean from './middleware/clean'

export default {
Find,
SetState,
Simulate
Simulate,
Clean
}
7 changes: 7 additions & 0 deletions src/middleware/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Clean(){
this.instance = null
this.elements = null
if (global.window){
global.window.document.body.innerHTML = ''
}
}
10 changes: 8 additions & 2 deletions src/tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import TestUtils from 'react-addons-test-utils'
import ReactDOMServer from 'react-dom/server'
import ReactDOM from 'react-dom'
import React from 'react'
global.React = React

import { Find, SetState, Simulate } from './middleware'
import { Find, SetState, Simulate, Clean } from './middleware'

function Test(component, config) {

Expand All @@ -12,6 +13,10 @@ function Test(component, config) {
const shallowRenderer = TestUtils.createRenderer()
shallowRenderer.render(component)
instance = shallowRenderer.getRenderOutput()
} else if (config && config.fullDOM && global.window) {
var div = global.window.document.createElement('div')
global.window.document.body.appendChild(div)
instance = ReactDOM.render(component, div)
} else {
instance = TestUtils.renderIntoDocument(component)
}
Expand Down Expand Up @@ -69,7 +74,8 @@ function Test(component, config) {
return testComponent.mixin({
find: Find,
setState: SetState,
simulate: Simulate
simulate: Simulate,
clean: Clean
})
}

Expand Down
18 changes: 18 additions & 0 deletions tests/full-dom.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Test from '../src/legit-tests'
import { expect } from 'chai'

describe('Render into document.body', () => {

it('should render and clean up component', () => {
Test(<section />, {fullDOM: true})
.test(function() {
expect(global.window.document.querySelector('section'))
.to.not.equal(null)
})
.clean()

// clean should clean up the document.body
expect(global.window.document.body.innerHTML).to.equal('')
})

})

0 comments on commit 3a26bdf

Please sign in to comment.