Skip to content

Commit d02db0f

Browse files
authored
test: add test scripts (#34)
* test: add test scripts * ci: update github workflows * ci: format gh-pages.yml * ci: format gh-pages.yml * ci: update gh-pages.yml * docs: update README.md * ci: update gh-pages.yml
1 parent 54d96c0 commit d02db0f

17 files changed

+2256
-67
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = {
2929
'@typescript-eslint/explicit-member-accessibility': OFF,
3030
'@typescript-eslint/no-namespace': OFF,
3131
'@typescript-eslint/explicit-module-boundary-types': OFF,
32-
'react/display-name': OFF
32+
'react/display-name': OFF,
33+
'react/prop-types': OFF
3334
},
3435
settings: {
3536
react: {

.github/workflows/gh-pages.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-20.04
14+
permissions:
15+
contents: write
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.ref }}
18+
steps:
19+
- uses: actions/checkout@v2
20+
21+
- name: Setup node
22+
uses: actions/setup-node@v2
23+
with:
24+
node-version: 'lts/*'
25+
26+
- name: Install
27+
run: npm install
28+
29+
- name: Build
30+
run: npm run build:docs
31+
32+
- name: Deploy
33+
uses: peaceiris/actions-gh-pages@v3
34+
if: github.ref == 'refs/heads/master'
35+
with:
36+
github_token: ${{ secrets.GITHUB_TOKEN }}
37+
publish_dir: ./docs/assets

.github/workflows/nodejs-ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches:
9+
- master
10+
pull_request:
11+
branches:
12+
- master
13+
jobs:
14+
test:
15+
name: 'Test'
16+
runs-on: ubuntu-latest
17+
container: node:16
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
- run: npm install
22+
- run: npm test
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# see https://help.github.com/cn/actions/language-and-framework-guides/publishing-nodejs-packages
2+
3+
name: Node.js Package
4+
5+
on:
6+
push:
7+
tags: ['*']
8+
9+
jobs:
10+
publish:
11+
name: 'Publish'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
# Setup .npmrc file to publish to npm
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: '12.x'
19+
registry-url: 'https://registry.npmjs.org'
20+
21+
- name: Install dependencies
22+
run: npm install
23+
24+
- name: Build
25+
run: npm run build
26+
27+
- name: Npm Publish
28+
run: npm publish dist
29+
env:
30+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-code-view
22

3-
Let the React code in Markdown be rendered to the page, and support online editing.
3+
`react-code-view` is a React component based on `codemirror` and `marked`. It enables `jsx` code in `markdown` to be rendered to the page.
44

55
![preview](https://user-images.githubusercontent.com/1203827/44707274-a30c0f80-aad6-11e8-8cc5-9cf7daf4d9e2.gif)
66

__tests__/CodeEditor-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { render, act } from '@testing-library/react';
3+
import CodeEditor from '../src/CodeEditor';
4+
5+
it('renders without crashing', async () => {
6+
await act(async () => {
7+
render(<CodeEditor />);
8+
});
9+
});
10+
11+
it('editor should be initialized', async () => {
12+
await act(async () => {
13+
const { container } = render(
14+
<CodeEditor
15+
onInitialized={() => {
16+
expect(container.querySelector('.rcv-editor-loader')).toBeFalsy();
17+
}}
18+
/>
19+
);
20+
expect(container.querySelector('.rcv-editor-loader')).toBeTruthy();
21+
});
22+
});
23+
24+
it('should be initialized code', async () => {
25+
await act(async () => {
26+
const { container } = render(
27+
<CodeEditor
28+
code={'const a = 1;'}
29+
onInitialized={() => {
30+
expect(container.querySelector('textarea').value).toBe('const a = 1;');
31+
expect(container.querySelector('.CodeMirror').textContent).toBe('const a = 1;');
32+
}}
33+
/>
34+
);
35+
expect(container.querySelector('.rcv-editor-loader')).toBeTruthy();
36+
});
37+
});
38+
39+
it('should call onChange callback', async () => {
40+
await act(async () => {
41+
const { container } = render(
42+
<CodeEditor
43+
code={'const a = 1;'}
44+
onChange={value => {
45+
expect(container.querySelector('textarea').value).toBe('const a = 1;');
46+
expect(value).toBe('const a = 2;');
47+
}}
48+
onInitialized={editor => {
49+
editor.setValue('const a = 2;');
50+
}}
51+
/>
52+
);
53+
});
54+
});

__tests__/ErrorBoundary-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { render } from '@testing-library/react';
2+
import ErrorBoundary from '../src/ErrorBoundary';
3+
4+
it('renders error message', () => {
5+
const { getByText } = render(<ErrorBoundary hasError errorMessage="test message" />);
6+
7+
expect(getByText('test message')).toBeTruthy();
8+
});
9+
10+
it('render child elements', () => {
11+
const { getByText } = render(<ErrorBoundary>child</ErrorBoundary>);
12+
expect(getByText('child')).toBeTruthy();
13+
});

__tests__/Preview-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { render } from '@testing-library/react';
2+
import Preview from '../src/Preview';
3+
4+
it('renders without crashing', () => {
5+
const { container } = render(<Preview />);
6+
expect(container).toBeTruthy();
7+
});
8+
9+
it('render a loader', () => {
10+
const { container } = render(<Preview />);
11+
expect(container.querySelector('.rcv-render-loader')).toBeTruthy();
12+
});
13+
14+
it('render child elements', () => {
15+
const { getByText } = render(
16+
<Preview>
17+
<button>test</button>
18+
</Preview>
19+
);
20+
expect(getByText('test')).toBeTruthy();
21+
});

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = (api, options) => {
1010
return {
1111
presets: [
1212
['@babel/preset-env', { modules, loose: true }],
13-
['@babel/preset-react', { development: dev }],
13+
['@babel/preset-react', { development: dev, runtime: 'automatic' }],
1414
'@babel/typescript'
1515
],
1616
plugins: [

docs/example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
---
44

5-
Let the React code in Markdown be rendered to the page, and support online editing.
5+
`react-code-view` is a React component based on `codemirror` and `marked`. It enables `jsx` code in `markdown` to be rendered to the page.
66

77
## Install
88

0 commit comments

Comments
 (0)