|
| 1 | +import test from 'tape'; |
| 2 | +import cheerio from 'cheerio'; |
| 3 | + |
| 4 | +import { |
| 5 | + Component, |
| 6 | + createElement, |
| 7 | + createFactory, |
| 8 | + PropTypes, |
| 9 | +} from 'react'; |
| 10 | +import { renderToString } from 'react-dom/server'; |
| 11 | + |
| 12 | +import parser from '../build/parser.js'; |
| 13 | +import URLProvider from '../build/URLProvider'; |
| 14 | +import connectURL from '../build/connectURL'; |
| 15 | + |
| 16 | + |
| 17 | +const urls = { |
| 18 | + home: '/', |
| 19 | + discussion: '/discussion/:slug/', |
| 20 | + random: '/random/:param1/rand/:param2/', |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | +class App extends Component { |
| 25 | + render() { |
| 26 | + return createElement('a', { href: this.props.randomURL }, 'click me!'); |
| 27 | + } |
| 28 | +} |
| 29 | +App.propTypes = { |
| 30 | + randomURL: PropTypes.string, |
| 31 | +}; |
| 32 | + |
| 33 | + |
| 34 | +test('url parser', t => { |
| 35 | + t.plan(3); |
| 36 | + |
| 37 | + const homeUrl = parser(urls, 'home'); |
| 38 | + const discussionUrl = parser(urls, 'discussion', { slug: 'mi-discusion' }); |
| 39 | + const randomUrl = parser(urls, 'random', { |
| 40 | + param1: '123', |
| 41 | + param2: 456, |
| 42 | + }); |
| 43 | + |
| 44 | + t.equals( |
| 45 | + homeUrl, |
| 46 | + '/', |
| 47 | + 'it should return the url without parameters' |
| 48 | + ); |
| 49 | + |
| 50 | + t.equals( |
| 51 | + discussionUrl, |
| 52 | + '/discussion/mi-discusion/', |
| 53 | + 'it should return url with only one parameter' |
| 54 | + ); |
| 55 | + |
| 56 | + t.equals( |
| 57 | + randomUrl, |
| 58 | + '/random/123/rand/456/', |
| 59 | + 'it should return the url with multiple parameters and a number as value' |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | + |
| 64 | +test('connected component', t => { |
| 65 | + t.plan(2); |
| 66 | + |
| 67 | + function mapURLToProps(getURL) { |
| 68 | + return { |
| 69 | + randomURL: getURL('random', { |
| 70 | + param1: 123, |
| 71 | + param2: '456', |
| 72 | + }), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + const ConnectedApp = connectURL(mapURLToProps)(App); |
| 77 | + |
| 78 | + function ContainerApp() { |
| 79 | + return createElement( |
| 80 | + URLProvider, |
| 81 | + { |
| 82 | + urls, |
| 83 | + }, |
| 84 | + [ |
| 85 | + createFactory(ConnectedApp)({ key: 1 }), |
| 86 | + ] |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + const html = renderToString(createFactory(ContainerApp)()); |
| 91 | + |
| 92 | + const $ = cheerio.load(html); |
| 93 | + |
| 94 | + t.equals( |
| 95 | + $('a').attr('href'), |
| 96 | + '/random/123/rand/456/', |
| 97 | + 'it should return an anchor with the parsed url as href attribute' |
| 98 | + ); |
| 99 | +}); |
0 commit comments