-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Console presentational component (#704)
* make jest work again * make initial console presentational component * fix js doc string * update yarn.lock file * add babel-plugin-transform-class-properties * Add IDEConsole component with LogEntries * Allocate a larger area for the console * Update snapshot tests * Merge branch 'master' into console_component * insert logs param * Add shape for log entries proptypes * Update snapshots for new array
- Loading branch information
1 parent
2937452
commit 9e9cc5a
Showing
16 changed files
with
1,146 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"alias": { | ||
"features": "./src/redux/features" | ||
} | ||
}] | ||
}], | ||
"transform-class-properties" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
'testURL': 'http://localhost/', | ||
'setupTestFrameworkScriptFile': '<rootDir>/src/setupTests.js', | ||
'snapshotSerializers': ['enzyme-to-json/serializer'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 43 additions & 1 deletion
44
game_frontend/src/components/IDEConsole/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,70 @@ | ||
import styled from 'styled-components' | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import LogEntries from 'components/LogEntries' | ||
|
||
export const IDEConsole = styled.div` | ||
background-color: ${props => props.theme.palette.background.default} | ||
grid-area: ide-console | ||
export const StyledConsole = styled.div` | ||
grid-area: ide-console; | ||
color: ${props => props.theme.palette.text.primary}; | ||
background-color: ${props => props.theme.palette.background.default}; | ||
font-family: ${props => props.theme.additionalVariables.typography.code.fontFamily}; | ||
overflow: auto; | ||
white-space: pre-line; | ||
padding-bottom: 0px; | ||
::-webkit-scrollbar { | ||
background-color: ${props => props.theme.palette.divider}; | ||
} | ||
::-webkit-scrollbar-track { | ||
background: ${props => props.theme.palette.divider}; | ||
} | ||
::-webkit-scrollbar-thumb { | ||
background: ${props => props.theme.palette.grey['A200']}; | ||
border-radius: 100px; | ||
border: ${props => props.theme.spacing.unit / 4}px solid transparent; | ||
background-clip: content-box; | ||
} | ||
` | ||
|
||
export default IDEConsole | ||
export default class IDEConsole extends Component { | ||
state = { | ||
scrolledToBottom: true | ||
} | ||
|
||
handleScroll = event => { | ||
let srcElement = event.srcElement | ||
this.setState({...this.state, scrolledToBottom: srcElement.offsetHeight + srcElement.scrollTop === srcElement.scrollHeight}) | ||
} | ||
|
||
componentDidMount () { | ||
if (this.consoleRef) { | ||
this.consoleRef.addEventListener('scroll', this.handleScroll) | ||
} | ||
} | ||
|
||
componentWillUnmount () { | ||
this.consoleRef.removeEventListener('scroll', this.handleScroll) | ||
} | ||
|
||
componentDidUpdate () { | ||
if (this.lastLogRef && this.state.scrolledToBottom) { | ||
this.lastLogRef.scrollIntoView(false) | ||
} | ||
} | ||
|
||
render () { | ||
return ( | ||
<StyledConsole innerRef={ref => { this.consoleRef = ref }}> | ||
<LogEntries | ||
logs={this.props.logs} | ||
lastLogRef={ref => { this.lastLogRef = ref }} /> | ||
</StyledConsole> | ||
) | ||
} | ||
} | ||
|
||
IDEConsole.propTypes = { | ||
logs: PropTypes.arrayOf(PropTypes.object) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
/* eslint-env jest */ | ||
import React from 'react' | ||
import IDEConsole from 'components/IDEConsole' | ||
import IDEConsole, { StyledConsole } from 'components/IDEConsole' | ||
import createShallowWithTheme from 'testHelpers/createShallow' | ||
|
||
describe('<IDEConsole />', () => { | ||
it('renders correctly', () => { | ||
const tree = createShallowWithTheme(<IDEConsole />) | ||
const tree = createShallowWithTheme(<IDEConsole logs={[{ timestamp: '1', log: 'hello' }, { timestamp: 2, log: 'bye' }]} />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('<StyledConsole />', () => { | ||
it('renders correctly', () => { | ||
const tree = createShallowWithTheme(<StyledConsole />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
45 changes: 45 additions & 0 deletions
45
game_frontend/src/components/LogEntries/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<LogEntries /> renders correctly with logs 1`] = ` | ||
<styled.ul> | ||
<styled.li | ||
innerRef={[Function]} | ||
key="1" | ||
> | ||
hello | ||
</styled.li> | ||
<styled.li | ||
innerRef={[Function]} | ||
key="2" | ||
> | ||
bye | ||
</styled.li> | ||
</styled.ul> | ||
`; | ||
|
||
exports[`<LogEntries /> renders correctly without logs 1`] = `<styled.ul />`; | ||
|
||
exports[`<LogEntry /> renders correctly 1`] = ` | ||
.c0 { | ||
list-style: none; | ||
padding: 8px; | ||
} | ||
<li | ||
className="c0" | ||
> | ||
hello | ||
</li> | ||
`; | ||
|
||
exports[`<StyledLogEntries /> renders correctly 1`] = ` | ||
.c0 { | ||
list-style: none; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
<ul | ||
className="c0" | ||
/> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import styled from 'styled-components' | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
export const LogEntry = styled.li` | ||
list-style: none; | ||
padding: ${props => props.theme.spacing.unit}px; | ||
` | ||
|
||
export const StyledLogEntries = styled.ul` | ||
list-style: none; | ||
margin: 0; | ||
padding: 0; | ||
` | ||
|
||
export default class LogEntries extends Component { | ||
generateLogEntries () { | ||
const lastLogEntry = this.props.logs[this.props.logs.length - 1] | ||
const logEntries = this.props.logs.map(logEntry => | ||
<LogEntry | ||
key={logEntry.timestamp} | ||
innerRef={this.setLastLogEntryRef(logEntry, lastLogEntry)}> | ||
{logEntry.log} | ||
</LogEntry> | ||
) | ||
return logEntries | ||
} | ||
|
||
setLastLogEntryRef (logEntry, lastLogEntry) { | ||
return node => { | ||
if (logEntry.timestamp === lastLogEntry.timestamp) { | ||
this.props.lastLogRef(node) | ||
} | ||
} | ||
} | ||
|
||
render () { | ||
return ( | ||
<StyledLogEntries> | ||
{this.generateLogEntries()} | ||
</StyledLogEntries> | ||
) | ||
} | ||
} | ||
|
||
LogEntries.propTypes = { | ||
logs: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
timestamp: PropTypes.int, | ||
log: PropTypes.string | ||
})), | ||
lastLogRef: PropTypes.func | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-env jest */ | ||
import React from 'react' | ||
import LogEntries, { StyledLogEntries, LogEntry } from 'components/LogEntries' | ||
import createShallowWithTheme from 'testHelpers/createShallow' | ||
|
||
describe('<LogEntries />', () => { | ||
it('renders correctly with logs', () => { | ||
const tree = createShallowWithTheme(<LogEntries logs={[{ timestamp: '1', log: 'hello' }, { timestamp: 2, log: 'bye' }]} />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
|
||
it('renders correctly without logs', () => { | ||
const tree = createShallowWithTheme(<LogEntries logs={[]} />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('<StyledLogEntries />', () => { | ||
it('renders correctly', () => { | ||
const tree = createShallowWithTheme(<StyledLogEntries logs={[{ timestamp: '1', log: 'hello' }, { timestamp: 2, log: 'bye' }]} />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('<LogEntry />', () => { | ||
it('renders correctly', () => { | ||
const tree = createShallowWithTheme(<LogEntry>hello</LogEntry>) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.