|
| 1 | +var assert = require('assert'); |
| 2 | +var expect = require('expect'); |
| 3 | +var React = require('react/addons'); |
| 4 | +var ReactTestUtils = React.addons.TestUtils; |
| 5 | +var PathStore = require('../../stores/PathStore'); |
| 6 | +var Routes = require('../Routes'); |
| 7 | +var Route = require('../Route'); |
| 8 | + |
| 9 | +function getRootMatch(matches) { |
| 10 | + return matches[matches.length - 1]; |
| 11 | +} |
| 12 | + |
| 13 | +describe('A Routes', function () { |
| 14 | + |
| 15 | + var App = React.createClass({ |
| 16 | + render: function () { |
| 17 | + return null; |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + describe('that matches a URL', function () { |
| 22 | + var component; |
| 23 | + beforeEach(function () { |
| 24 | + component = ReactTestUtils.renderIntoDocument( |
| 25 | + Routes(null, |
| 26 | + Route({ handler: App }, |
| 27 | + Route({ path: '/a/b/c', handler: App }) |
| 28 | + ) |
| 29 | + ) |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(function () { |
| 34 | + React.unmountComponentAtNode(component.getDOMNode()); |
| 35 | + // For some reason unmountComponentAtNode doesn't call componentWillUnmount :/ |
| 36 | + PathStore.removeAllChangeListeners(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns an array', function () { |
| 40 | + var matches = component.match('/a/b/c'); |
| 41 | + assert(matches); |
| 42 | + expect(matches.length).toEqual(2); |
| 43 | + |
| 44 | + var rootMatch = getRootMatch(matches); |
| 45 | + expect(rootMatch.params).toEqual({}); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('that matches a URL with dynamic segments', function () { |
| 50 | + var component; |
| 51 | + beforeEach(function () { |
| 52 | + component = ReactTestUtils.renderIntoDocument( |
| 53 | + Routes(null, |
| 54 | + Route({ handler: App }, |
| 55 | + Route({ path: '/posts/:id/edit', handler: App }) |
| 56 | + ) |
| 57 | + ) |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(function () { |
| 62 | + React.unmountComponentAtNode(component.getDOMNode()); |
| 63 | + // For some reason unmountComponentAtNode doesn't call componentWillUnmount :/ |
| 64 | + PathStore.removeAllChangeListeners(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns an array with the correct params', function () { |
| 68 | + var matches = component.match('/posts/abc/edit'); |
| 69 | + assert(matches); |
| 70 | + expect(matches.length).toEqual(2); |
| 71 | + |
| 72 | + var rootMatch = getRootMatch(matches); |
| 73 | + expect(rootMatch.params).toEqual({ id: 'abc' }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | +}); |
0 commit comments