react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / __tests__ / ReactRenderDocument-test.js
81159 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @emails react-core9*/1011/*jslint evil: true */1213"use strict";1415var React;16var ReactMount;1718var getTestDocument;1920var testDocument;2122var UNMOUNT_INVARIANT_MESSAGE =23'Invariant Violation: ReactFullPageComponenthtml tried to unmount. ' +24'Because of cross-browser quirks it is impossible to unmount some ' +25'top-level components (eg <html>, <head>, and <body>) reliably and ' +26'efficiently. To fix this, have a single top-level component that ' +27'never unmounts render these elements.';2829describe('rendering React components at document', function() {30beforeEach(function() {31require('mock-modules').dumpCache();3233React = require('React');34ReactMount = require('ReactMount');35getTestDocument = require('getTestDocument');3637testDocument = getTestDocument();38});3940it('should be able to get root component id for document node', function() {41expect(testDocument).not.toBeUndefined();4243var Root = React.createClass({44render: function() {45return (46<html>47<head>48<title>Hello World</title>49</head>50<body>51Hello world52</body>53</html>54);55}56});5758var markup = React.renderToString(<Root />);59testDocument = getTestDocument(markup);60var component = React.render(<Root />, testDocument);61expect(testDocument.body.innerHTML).toBe('Hello world');6263var componentID = ReactMount.getReactRootID(testDocument);64expect(componentID).toBe(component._rootNodeID);65});6667it('should not be able to unmount component from document node', function() {68expect(testDocument).not.toBeUndefined();6970var Root = React.createClass({71render: function() {72return (73<html>74<head>75<title>Hello World</title>76</head>77<body>78Hello world79</body>80</html>81);82}83});8485var markup = React.renderToString(<Root />);86testDocument = getTestDocument(markup);87React.render(<Root />, testDocument);88expect(testDocument.body.innerHTML).toBe('Hello world');8990expect(function() {91React.unmountComponentAtNode(testDocument);92}).toThrow(UNMOUNT_INVARIANT_MESSAGE);9394expect(testDocument.body.innerHTML).toBe('Hello world');95});9697it('should not be able to switch root constructors', function() {98expect(testDocument).not.toBeUndefined();99100var Component = React.createClass({101render: function() {102return (103<html>104<head>105<title>Hello World</title>106</head>107<body>108Hello world109</body>110</html>111);112}113});114115var Component2 = React.createClass({116render: function() {117return (118<html>119<head>120<title>Hello World</title>121</head>122<body>123Goodbye world124</body>125</html>126);127}128});129130var markup = React.renderToString(<Component />);131testDocument = getTestDocument(markup);132133React.render(<Component />, testDocument);134135expect(testDocument.body.innerHTML).toBe('Hello world');136137// Reactive update138expect(function() {139React.render(<Component2 />, testDocument);140}).toThrow(UNMOUNT_INVARIANT_MESSAGE);141142expect(testDocument.body.innerHTML).toBe('Hello world');143});144145it('should be able to mount into document', function() {146expect(testDocument).not.toBeUndefined();147148var Component = React.createClass({149render: function() {150return (151<html>152<head>153<title>Hello World</title>154</head>155<body>156{this.props.text}157</body>158</html>159);160}161});162163var markup = React.renderToString(164<Component text="Hello world" />165);166testDocument = getTestDocument(markup);167168React.render(<Component text="Hello world" />, testDocument);169170expect(testDocument.body.innerHTML).toBe('Hello world');171});172173it('should give helpful errors on state desync', function() {174expect(testDocument).not.toBeUndefined();175176var Component = React.createClass({177render: function() {178return (179<html>180<head>181<title>Hello World</title>182</head>183<body>184{this.props.text}185</body>186</html>187);188}189});190191var markup = React.renderToString(192<Component text="Goodbye world" />193);194testDocument = getTestDocument(markup);195196expect(function() {197// Notice the text is different!198React.render(<Component text="Hello world" />, testDocument);199}).toThrow(200'Invariant Violation: ' +201'You\'re trying to render a component to the document using ' +202'server rendering but the checksum was invalid. This usually ' +203'means you rendered a different component type or props on ' +204'the client from the one on the server, or your render() methods ' +205'are impure. React cannot handle this case due to cross-browser ' +206'quirks by rendering at the document root. You should look for ' +207'environment dependent code in your components and ensure ' +208'the props are the same client and server side.'209);210});211212it('should throw on full document render w/ no markup', function() {213expect(testDocument).not.toBeUndefined();214215var container = testDocument;216217var Component = React.createClass({218render: function() {219return (220<html>221<head>222<title>Hello World</title>223</head>224<body>225{this.props.text}226</body>227</html>228);229}230});231232expect(function() {233React.render(<Component />, container);234}).toThrow(235'Invariant Violation: You\'re trying to render a component to the ' +236'document but you didn\'t use server rendering. We can\'t do this ' +237'without using server rendering due to cross-browser quirks. See ' +238'renderComponentToString() for server rendering.'239);240});241242});243244245