|
| 1 | +/* globals describe, it, assert, expect */ |
| 2 | + |
| 3 | +import React from 'react/addons'; |
| 4 | +import PageItemLink from '../src/PageItemLink'; |
| 5 | +import Router, { Route } from 'react-router'; |
| 6 | +import { Foo } from './TestHandlers'; |
| 7 | +import TestLocation from 'react-router/lib/locations/TestLocation'; |
| 8 | +let { click } = React.addons.TestUtils.Simulate; |
| 9 | + |
| 10 | +describe('A PageItemLink', function () { |
| 11 | + describe('with params and a query', function () { |
| 12 | + it('knows how to make its href', function () { |
| 13 | + let PageItemLinkHandler = React.createClass({ |
| 14 | + render() { |
| 15 | + return <PageItemLink to="foo" params={{bar: 'baz'}} query={{qux: 'quux'}}>PageItemLink</PageItemLink>; |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + let routes = [ |
| 20 | + <Route name="foo" path="foo/:bar" handler={Foo} />, |
| 21 | + <Route name="link" handler={PageItemLinkHandler} /> |
| 22 | + ]; |
| 23 | + |
| 24 | + let div = document.createElement('div'); |
| 25 | + let testLocation = new TestLocation(); |
| 26 | + testLocation.history = ['/link']; |
| 27 | + |
| 28 | + Router.run(routes, testLocation, function (Handler) { |
| 29 | + React.render(<Handler/>, div, function () { |
| 30 | + let a = div.querySelector('a'); |
| 31 | + expect(a.getAttribute('href')).to.equal('/foo/baz?qux=quux'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('when clicked', function () { |
| 38 | + it('calls a user defined click handler', function (done) { |
| 39 | + let PageItemLinkHandler = React.createClass({ |
| 40 | + handleClick(event) { |
| 41 | + assert.ok(true); |
| 42 | + done(); |
| 43 | + }, |
| 44 | + |
| 45 | + render() { |
| 46 | + return <PageItemLink to="foo" onClick={this.handleClick}>PageItemLink</PageItemLink>; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + let routes = [ |
| 51 | + <Route name="foo" handler={Foo} />, |
| 52 | + <Route name="link" handler={PageItemLinkHandler} /> |
| 53 | + ]; |
| 54 | + let div = document.createElement('div'); |
| 55 | + let testLocation = new TestLocation(); |
| 56 | + testLocation.history = ['/link']; |
| 57 | + |
| 58 | + Router.run(routes, testLocation, function (Handler) { |
| 59 | + React.render(<Handler/>, div, function () { |
| 60 | + click(div.querySelector('a')); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('transitions to the correct route', function (done) { |
| 66 | + let div = document.createElement('div'); |
| 67 | + let testLocation = new TestLocation(); |
| 68 | + testLocation.history = ['/link']; |
| 69 | + |
| 70 | + let PageItemLinkHandler = React.createClass({ |
| 71 | + handleClick() { |
| 72 | + // just here to make sure click handlers don't prevent it from happening |
| 73 | + }, |
| 74 | + |
| 75 | + render() { |
| 76 | + return <PageItemLink to="foo" onClick={this.handleClick}>PageItemLink</PageItemLink>; |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + let routes = [ |
| 81 | + <Route name="foo" handler={Foo} />, |
| 82 | + <Route name="link" handler={PageItemLinkHandler} /> |
| 83 | + ]; |
| 84 | + |
| 85 | + let steps = []; |
| 86 | + |
| 87 | + steps.push(function () { |
| 88 | + click(div.querySelector('a'), {button: 0}); |
| 89 | + }); |
| 90 | + |
| 91 | + steps.push(function () { |
| 92 | + expect(div.innerHTML).to.match(/Foo/); |
| 93 | + done(); |
| 94 | + }); |
| 95 | + |
| 96 | + Router.run(routes, testLocation, function (Handler) { |
| 97 | + React.render(<Handler/>, div, function () { |
| 98 | + steps.shift()(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + }); |
| 104 | + |
| 105 | +}); |
0 commit comments