Skip to content

Commit

Permalink
Indicate entry html filename
Browse files Browse the repository at this point in the history
  • Loading branch information
kuu12 committed Aug 24, 2018
1 parent 32e87e4 commit fdb6298
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 27 deletions.
41 changes: 20 additions & 21 deletions demo/app.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import Routing from '../src';
import TV from './tv';
import Trending from './trending';
import Movie from './movie';
import TV from './tv';
import Purchased from './purchased';
import Play from './play';
import NotFound from './notfound';
Expand All @@ -22,29 +23,27 @@ class App extends React.Component {
return (
<div id="app">
<div id="main">
<Movie // test basic use
path="/movie"
title="Movies"
foo={11}
bar={22}
/>
<TV // test dynamic routing
path="/tv/"
title="TV Series"
/>
{/* test html file */}
<Trending path="/" />

{/* test basic use */}
<Movie path="/movie" title="Movies" foo={11} bar={22} />

{/* test dynamic routing */}
<TV path="/tv/" title="TV Series" />

{/* test exclusive route */}
<Routing>
<Purchased // test exclusive route / test caching
path="/library/purchased"
autoCache
/>
<Play // test exclusive route / test url parameters
path="/library/:id"
/>
{/* test caching */}
<Purchased path="/library/purchased" autoCache />
{/* test url parameters */}
<Play path="/library/:id" />
</Routing>
<NotFound // test not found
notFound
/>

{/* test not found */}
<NotFound notFound />
</div>

<div id="sidebar">
<button
id="button-movie"
Expand Down
15 changes: 13 additions & 2 deletions demo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ import React from 'react';
import { render } from 'react-dom';
import App from './app';

history.pushState({}, null, '/cinima');
/*
* Mocking. This is not production code.
* Suppose your app has been deployed to www.domain.com/cinima
* And the entry html file is my-index.html
*/
history.pushState({}, null, '/cinima/my-index.html');
/**
* Mocking.
*/

const root = document.createElement('div');
root.id = root;
document.body.appendChild(root);

render(
<App basename="/cinima" />,
<App
basename="/cinima"
htmlFile="my-index.html"
/>,
root
);

Expand Down
10 changes: 10 additions & 0 deletions demo/trending.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import Routing from '../src';

const Trending = () => (
<div id="trending">
Most popular videos here...
</div>
);

export default Routing(Trending);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "less-router",
"version": "1.6.12",
"version": "1.6.14",
"description": "A very easy React router component but full functionally.",
"main": "src/index.jsx",
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions src/path/regex.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import proxy from '../proxy';
import { cacheable } from '../helper';
import { join } from './path';

Expand All @@ -16,8 +17,12 @@ const PARAMS_REPLACEMENT = '([\\w-~]+)';
*/
const regexFromString = cacheable(
string => {
if ('/' === string) {
string += '(?=(index.html)?$)';
if (
'/' === string &&
proxy.router &&
proxy.router.htmlFile
) {
string += `(?=(${proxy.router.htmlFile})?$)`;
} else if (string.endsWith('/')) {
string = string.slice(0, string.length - 1);
string += '(?=\\/?)';
Expand Down
6 changes: 5 additions & 1 deletion src/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class Router extends React.Component {
: '';
}
get pathname() {
return this.state.pathname === '/index.html' ?
return this.state.pathname === this.htmlFile ?
'/' : this.state.pathname;
}
get htmlFile() {
return 'htmlFile' in this.props ?
this.props.htmlFile : '/index.html';
}

push(pathname, cb) {
return this.__change__('pushState', pathname, cb);
Expand Down
7 changes: 7 additions & 0 deletions test/demo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const delay = time => new Promise(resolve =>
setTimeout(resolve, time)
);

describe('homepage', () => {
expect(document.getElementById('trending'))
.toBeInstanceOf(HTMLElement);
});

describe('basic use', () => {
beforeAll(() =>
document
Expand All @@ -26,6 +31,8 @@ describe('basic use', () => {
});

test('route rendering', () => {
expect(document.getElementById('trending'))
.toBeFalsy();
expect(document.getElementById('movie'))
.toBeInstanceOf(HTMLElement);
expect(document.getElementById('tv'))
Expand Down

0 comments on commit fdb6298

Please sign in to comment.