Skip to content

Commit

Permalink
test: add test files for typescript definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 13, 2016
1 parent fd76dc6 commit 4cca243
Show file tree
Hide file tree
Showing 17 changed files with 3,605 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ dist
coverage
.nyc_output
npm-debug.log
test/typescript/*.map
test/typescript/*.jsx
14 changes: 10 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ export interface SubscriptionsMapObject {
export interface Model {
namespace: string,
state?: any,
reducers?: ReducersMapObject,
reducers?: ReducersMapObject | ReducersMapObjectWithEnhancer,
effects?: EffectsMapObject,
subscriptions?: SubscriptionsMapObject,
}

export interface Router {
(api?: { history: HistoryModule.History }): JSX.Element | Object;
export interface RouterAPI {
history: HistoryModule.History;
app: DvaInstance;
}

export default function dva(opts?: DvaOption): {
export interface Router {
(api?: RouterAPI): JSX.Element | Object;
}

export interface DvaInstance {
/**
* Register an object of hooks on the application.
*
Expand Down Expand Up @@ -103,6 +107,8 @@ export default function dva(opts?: DvaOption): {
start: (selector?: HTMLElement | string) => any,
}

export default function dva(opts?: DvaOption): DvaInstance;

/**
* Connects a React component to Dva.
*/
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"react": "^15.3.1",
"react-dom": "^15.3.1",
"rimraf": "^2.5.4",
"typescript": "^2.0.3",
"typescript-definition-tester": "^0.0.4",
"uglifyjs": "^2.4.10"
},
"pre-commit": [
Expand Down
16 changes: 16 additions & 0 deletions test/typescript/fetch-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fetch from '../../fetch';

fetch('', {
headers: {},
method: 'post',
redirect: 'follow',
}).then(res => {
res.type;
res.url;
res.body;
res.json();
res.headers;
res.statusText;
});

fetch('');
97 changes: 97 additions & 0 deletions test/typescript/index-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import dva, { connect } from '../../index';
import { routerRedux } from '../../router';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

/**
* dva()
*/
const app = dva({
onError(e) {
e.message;
e.stack;
},
onAction: [],
onStateChange: () => {},
onReducer: () => {},
onEffect: () => {},
onHmr: () => {},
extraReducers: {
form() {},
},
});
// no option
dva();

/**
* app.use()
*/
app.use({
onError() {}
})

/**
* app.model()
*/
app.model({
namespace: 'count',
state: 0,
subscriptions: {
setup({ dispatch, history }, done) {},
},
effects: {
*addRemote({ type }, { call, put, select, take, cancel }) {
yield put({
type: 'a',
});
yield put(routerRedux.push('/'));
},
// Support effect with type
minusRemote: [
function*() {},
{ type: 'takeLatest' }
],
},
reducers: {
add(state) { return state + 1 },
minus(state) { return state - 1 },
},
});
// Support reducers with reducer enhancer
app.model({
namespace: 'x',
reducers: [{}, () => {}],
});

/**
* connect()
*/
function App() {
return <div>App</div>;
}
connect()(App);
const AppContainer = connect((state) => ({ count: state.count }))(App);

/**
* app.router()
*/
app.router(() => <div />);
app.router(({ history }) => <div />);

// dynamic route
app.router(({ history, app }) => {
app.model({
namespace: 'x'
});
return {};
});

/**
* app.start()
*/
app.start('#root');
app.start(document.getElementById('#root'));

// with no arguments
const jsx = app.start();
ReactDOM.render(jsx, document.getElementById('#root'));
9 changes: 9 additions & 0 deletions test/typescript/mobile-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dva, { connect } from '../../mobile';

const app = dva();
app.use({});
app.model({ namespace: 'count' });
app.router(() => ({}));
app.start();

connect()();
23 changes: 23 additions & 0 deletions test/typescript/router-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Router,
Route,
Link,
routerRedux,
} from '../../router';

function App() {
return (
<div><Link to="/404">1</Link></div>
);
}

function getRouter() {
return (
<Router>
<Route path="/" component={App} />
</Router>
);
}

routerRedux.push('/');
routerRedux.go('/');
12 changes: 12 additions & 0 deletions test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"moduleResolution": "node",
"jsx": "preserve",
"module": "commonjs",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
70 changes: 70 additions & 0 deletions test/typescript/typings/globals/react-dom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/b9642fb8ac07f7164dc643ddd1fa99b58ae9be8b/react/react-dom.d.ts
declare namespace __React {
namespace __DOM {
function findDOMNode<E extends Element>(instance: ReactInstance): E;
function findDOMNode(instance: ReactInstance): Element;

function render<P extends DOMAttributes, T extends Element>(
element: DOMElement<P, T>,
container: Element,
callback?: (element: T) => any): T;
function render<P>(
element: SFCElement<P>,
container: Element,
callback?: () => any): void;
function render<P, T extends Component<P, ComponentState>>(
element: CElement<P, T>,
container: Element,
callback?: (component: T) => any): T;
function render<P>(
element: ReactElement<P>,
container: Element,
callback?: (component?: Component<P, ComponentState> | Element) => any): Component<P, ComponentState> | Element | void;

function unmountComponentAtNode(container: Element): boolean;

var version: string;

function unstable_batchedUpdates<A, B>(callback: (a: A, b: B) => any, a: A, b: B): void;
function unstable_batchedUpdates<A>(callback: (a: A) => any, a: A): void;
function unstable_batchedUpdates(callback: () => any): void;

function unstable_renderSubtreeIntoContainer<P extends DOMAttributes, T extends Element>(
parentComponent: Component<any, any>,
element: DOMElement<P, T>,
container: Element,
callback?: (element: T) => any): T;
function unstable_renderSubtreeIntoContainer<P, T extends Component<P, ComponentState>>(
parentComponent: Component<any, any>,
element: CElement<P, T>,
container: Element,
callback?: (component: T) => any): T;
function render<P>(
parentComponent: Component<any, any>,
element: SFCElement<P>,
container: Element,
callback?: () => any): void;
function unstable_renderSubtreeIntoContainer<P>(
parentComponent: Component<any, any>,
element: ReactElement<P>,
container: Element,
callback?: (component?: Component<P, ComponentState> | Element) => any): Component<P, ComponentState> | Element | void;
}

namespace __DOMServer {
function renderToString(element: ReactElement<any>): string;
function renderToStaticMarkup(element: ReactElement<any>): string;
var version: string;
}
}

declare module "react-dom" {
import DOM = __React.__DOM;
export = DOM;
}

declare module "react-dom/server" {
import DOMServer = __React.__DOMServer;
export = DOMServer;
}
8 changes: 8 additions & 0 deletions test/typescript/typings/globals/react-dom/typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"resolution": "main",
"tree": {
"src": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/b9642fb8ac07f7164dc643ddd1fa99b58ae9be8b/react/react-dom.d.ts",
"raw": "registry:dt/react-dom#0.14.0+20160412154040",
"typings": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/b9642fb8ac07f7164dc643ddd1fa99b58ae9be8b/react/react-dom.d.ts"
}
}
Loading

0 comments on commit 4cca243

Please sign in to comment.