Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LCV container tests #809

Merged
merged 21 commits into from May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 131 additions & 0 deletions packages/lib-classifier/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/lib-classifier/package.json
Expand Up @@ -75,6 +75,7 @@
"mobx-state-tree": "~3.2.4",
"mocha": "~5.2.0",
"mst-middlewares": "~3.7.1",
"nock": "~10.0.6",
"panoptes-client": "~2.12.0",
"peer-deps-externals-webpack-plugin": "~1.0.4",
"polished": "~2.3.0",
Expand Down
@@ -1,6 +1,6 @@
import asyncStates from '@zooniverse/async-states'
import * as d3 from 'd3'
import { get, zip } from 'lodash'
import { zip } from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import request from 'superagent'
Expand All @@ -15,7 +15,6 @@ class LightCurveViewerContainer extends Component {
super()
this.viewer = React.createRef()
this.state = {
loading: asyncStates.initialized,
dataExtent: {
x: [],
y: []
Expand All @@ -24,28 +23,28 @@ class LightCurveViewerContainer extends Component {
}
}

componentDidMount () {
if (this.props.subject) {
this.handleSubject()
async componentDidMount () {
const { subject } = this.props
if (subject) {
await this.handleSubject()
}
}

componentDidUpdate (prevProps) {
async componentDidUpdate (prevProps) {
const { subject } = this.props
const prevSubjectId = prevProps.subject && prevProps.subject.id
const subjectChanged = subject && (subject.id !== prevSubjectId)

if (subjectChanged) {
this.handleSubject()
await this.handleSubject()
}
}

getSubjectUrl () {
// Find the first location that has a JSON MIME type.
// NOTE: we also temporarily accept plain text, due to quirks with the
// Panoptes CLI uploading wonky MIME types (@shaun 20181024)
const locations = get(this, 'props.subject.locations', [])
const jsonLocation = locations.find(l => l['application/json'] || l['text/plain']) || {}
const jsonLocation = this.props.subject.locations.find(l => l['application/json'] || l['text/plain']) || {}
const url = Object.values(jsonLocation)[0]
if (url) {
return url
Expand All @@ -55,28 +54,27 @@ class LightCurveViewerContainer extends Component {
}

async requestData () {
const { onError } = this.props
try {
this.setState({ loading: asyncStates.loading })
const url = this.getSubjectUrl()
const response = await request.get(url)
if (!response.ok) {
throw new Error('Invalid response')
} else {
// Get the JSON data, or (as a failsafe) parse the JSON data if the
// response is returned as a string
return response.body || JSON.parse(response.text)
}

// Get the JSON data, or (as a failsafe) parse the JSON data if the
// response is returned as a string
return response.body || JSON.parse(response.text)
} catch (error) {
return this.onError(error)
onError(error)
eatyourgreens marked this conversation as resolved.
Show resolved Hide resolved
return { x: [], y: [] }
}
}

async handleSubject () {
const { onError } = this.props
try {
const rawData = await this.requestData()
this.onLoad(rawData)
if (rawData) this.onLoad(rawData)
eatyourgreens marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
console.error(error)
onError(error)
}
}

Expand All @@ -89,24 +87,15 @@ class LightCurveViewerContainer extends Component {
y: d3.extent(rawData.y)
},
dataPoints: zip(rawData.x, rawData.y),
loading: asyncStates.success
},
function () {
onReady({ target })
})
}

onError (error) {
this.setState({
loading: asyncStates.error,
data: null
})
return error
}

render () {
const { subject } = this.props
if (!subject) {
if (!subject.id) {
return null
}

Expand All @@ -123,16 +112,24 @@ class LightCurveViewerContainer extends Component {
}
}

LightCurveViewerContainer.defaultProps = {
onReady: () => true
LightCurveViewerContainer.wrappedComponent.defaultProps = {
loadingState: asyncStates.initialized,
onError: () => true,
onReady: () => true,
subject: {
eatyourgreens marked this conversation as resolved.
Show resolved Hide resolved
id: '',
locations: []
}
}

LightCurveViewerContainer.propTypes = {
LightCurveViewerContainer.wrappedComponent.propTypes = {
loadingState: PropTypes.string,
onError: PropTypes.func,
onReady: PropTypes.func,
subject: PropTypes.shape({
id: PropTypes.string,
locations: PropTypes.arrayOf(locationValidator)
}),
subjectId: PropTypes.string
})
}

export default LightCurveViewerContainer