Skip to content

Commit

Permalink
Abstract controlButton into components/commons
Browse files Browse the repository at this point in the history
Also fixed typescript type errors on yarn start.
  • Loading branch information
ning-y committed May 30, 2018
1 parent 56b4c54 commit 44b5dd2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
19 changes: 19 additions & 0 deletions src/components/commons/controlButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Button, IconName, Intent } from '@blueprintjs/core'
import * as React from 'react'

export const controlButton = (
label: string,
icon: IconName,
handleClick = () => {},
intent = Intent.NONE,
minimal = true
) => (
<Button
onClick={handleClick}
className={(minimal ? 'pt-minimal' : '') + ' col-xs-12'}
intent={intent}
icon={icon}
>
{label}
</Button>
)
1 change: 1 addition & 0 deletions src/components/commons/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './controlButton'
23 changes: 3 additions & 20 deletions src/components/workspace/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react'

import AceEditor from 'react-ace'

import { Button, IconName, Intent } from '@blueprintjs/core'
import { controlButton } from '../commons'

import 'brace/mode/javascript'
import 'brace/theme/cobalt'
Expand All @@ -24,27 +23,11 @@ export interface IEditorProps {

class Editor extends React.Component<IEditorProps, {}> {
public render() {
const genericButton = (
label: string,
icon: IconName,
handleClick = () => {},
intent = Intent.NONE,
notMinimal = false
) => (
<Button
onClick={handleClick}
className={(notMinimal ? '' : 'pt-minimal') + ' col-xs-12'}
intent={intent}
icon={icon}
>
{label}
</Button>
)
const runButton = this.props.isRunning
? null
: genericButton('', 'play', this.props.handleEditorEval)
: controlButton('', 'play', this.props.handleEditorEval)
const stopButton = this.props.isRunning
? genericButton('', 'stop', this.props.handleInterruptEval)
? controlButton('', 'stop', this.props.handleInterruptEval)
: null
return (
<div className="Editor">
Expand Down
19 changes: 1 addition & 18 deletions src/components/workspace/ReplControl.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, IconName, Intent } from '@blueprintjs/core'
import * as React from 'react'

import { sourceChapters } from '../../reducers/states'
import { controlButton } from '../commons'

/**
* @property handleEvalEditor - A callback function for evaluation
Expand All @@ -27,23 +27,6 @@ class ReplControl extends React.Component<IReplControlProps, {}> {
}
}

const controlButton = (
label: string,
icon: IconName,
handleClick = () => {},
intent = Intent.NONE,
minimal = true
) => (
<Button
onClick={handleClick}
className={(minimal ? 'pt-minimal' : '') + ' col-xs-12'}
intent={intent}
icon={icon}
>
{label}
</Button>
)

const chapterSelect = (handleSelect = (e: React.ChangeEvent<HTMLSelectElement>) => {}) => (
<div className="pt-select pt-select">
<select defaultValue={sourceChapters.slice(-1)[0].toString()} onChange={handleSelect}>
Expand Down
17 changes: 12 additions & 5 deletions src/components/workspace/__tests__/Repl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ import { shallow } from 'enzyme'
import * as React from 'react'

import { mockTypeError } from '../../../mocks/context'
import {
CodeOutput,
ErrorOutput,
InterpreterOutput,
ResultOutput,
RunningOutput
} from '../../../reducers/states'
import Repl, { Output } from '../Repl'

const mockRunningOutput = {
const mockRunningOutput: RunningOutput = {
type: 'running',
consoleLogs: ['a', 'bb', 'cccccccccccccccccccccccccccccccc', 'd']
}

const mockCodeOutput = {
const mockCodeOutput: CodeOutput = {
type: 'code',
value: "display('');"
}

const mockResultOutput = {
const mockResultOutput: ResultOutput = {
type: 'result',
value: 42,
consoleLogs: []
}

const mockErrorOutput = {
const mockErrorOutput: ErrorOutput = {
type: 'errors',
errors: [mockTypeError()],
consoleLogs: []
Expand Down Expand Up @@ -85,7 +92,7 @@ test('Error output (with consoleLogs) renders correctly', () => {
})

test('Empty output renders an empty card', () => {
const app = <Output {...{ output: {} }} />
const app = <Output {...{ output: {} as InterpreterOutput }} />
const tree = shallow(app)
expect(tree.debug()).toMatchSnapshot()
})
3 changes: 2 additions & 1 deletion src/mocks/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse } from 'acorn'
import * as es from 'estree'

import { createContext } from '../slang'
Expand Down Expand Up @@ -34,5 +35,5 @@ export function mockClosure(): Closure {
}

export function mockTypeError(): TypeError {
return new TypeError({ loc: 0 } as es.Node, '', '', '')
return new TypeError(parse(''), '', '', '')
}

0 comments on commit 44b5dd2

Please sign in to comment.