Skip to content

Commit

Permalink
update(remark): remark-reactのテストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Oct 20, 2018
1 parent 368b82d commit 9d0af15
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.language": "en"
}
6 changes: 4 additions & 2 deletions src/react-starter/oneshot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import * as remark from 'remark';
import html = require('remark-html');
import * as html from 'remark-html';
import reactRenderer from 'remark-react';

export class App extends React.Component<{}, { text: string }> {
Expand All @@ -11,13 +11,15 @@ export class App extends React.Component<{}, { text: string }> {
};
this.onChange = this.onChange.bind(this);
}

public onChange(e: React.ChangeEvent<HTMLTextAreaElement>): void {
this.setState({ text: e.target.value });
}

public render() {
// @ts-ignore
const markUp = { __html: remark().use(reactRenderer, {
sanitize: false
sanitize: false,
}).use(html).processSync(this.state.text).contents };
return (
<div>
Expand Down
1 change: 0 additions & 1 deletion src/react-starter/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const resolveApp = (relativePath: string) =>
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

const webpackModule: webpack.Configuration[] = [
// rendererで利用するscript。entryはwindowの種類の数だけある
{
stats: 'errors-only',
entry: {
Expand Down
16 changes: 10 additions & 6 deletions src/remark-react/__tests__/oneshot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import * as OneShot from '../oneshot';


test('React.MyComponent', () => {
const comp = React.createElement(OneShot.App);
expect(comp).toEqual(<OneShot.App />);
const comp = React.createElement(OneShot.App, { title: '' });
expect(comp).toEqual(<OneShot.App title='' />);
});

test('レンダリングを行った結果が一致する', () => {
test('MarkdownからHTMLがレンダリングされていることを確認する', () => {
const component = renderer.create(
<OneShot.App />
<OneShot.App title='# Hello world' />
);
const tree = component.toJSON();
expect(tree).toEqual(`# hello world
const componentJSON = component.toJSON()!;
expect(componentJSON).not.toBeNull();
expect(componentJSON.props.dangerouslySetInnerHTML).not.toBeUndefined();
expect(componentJSON.props.dangerouslySetInnerHTML.__html).not.toBeUndefined();
const html = componentJSON.props.dangerouslySetInnerHTML.__html;
expect(html).toBe(`<h1>Hello world</h1>
`);
})
28 changes: 10 additions & 18 deletions src/remark-react/oneshot.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import * as React from 'react';
import * as remark from 'remark';
import reactRenderer from 'remark-react';
import * as html from 'remark-html';
import remarkReact from 'remark-react';

export interface AppState {
text: string;
export interface AppProps {
title: string;
}

// @ts-ignore
const processor = remark().use(reactRenderer, {
const processor = remark().use(remarkReact, {
prefix: 'md-',
sanitize: true
})

export class App extends React.Component<{}, AppState> {
constructor(props: {}) {
super(props);
this.state = {
text: '# hello world'
};
}

public onChange(e: React.ChangeEvent<HTMLTextAreaElement>): void {
this.setState({ text: e.target.value });
}

export class App extends React.Component<AppProps, {}> {
public render() {
// @ts-ignore
const content = processor.processSync(this.state.text).contents
return content;
const content = { __html: remark().use(remarkReact, {
sanitize: false
}).use(html).processSync(this.props.title).contents };
return <div dangerouslySetInnerHTML={content} />;
}
}

3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"remark": [
"./typings/remark/index.d.ts"
],
"remark-html": [
"./typings/remark-html/index.d.ts"
],
"remark-react": [
"./typings/remark-react/index.d.ts"
],
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"linterOptions": {
"exclude": [
"node_modules/**/*.ts"
"node_modules/**"
]
},
"rules": {
Expand Down
5 changes: 5 additions & 0 deletions typings/remark-html/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Option {}

declare function plugin(option: Option): any

export default plugin
4 changes: 3 additions & 1 deletion typings/remark/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
interface Parser {}
interface Parser {
use: any
}
interface Options {}


Expand Down

0 comments on commit 9d0af15

Please sign in to comment.