-
Notifications
You must be signed in to change notification settings - Fork 608
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
Feature/approve button #1150
Merged
Merged
Feature/approve button #1150
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7aa49ad
fix(remote): fix eslint errors in remote/index.js
db8e2ba
fix(remote): change request to '/approve' from get to post since it i…
9a7afe3
feature(report): add a button to allow approving tests from the repor…
c5ee8e5
refactor(report): remove unnecessary availableStatus state and reducer
a6c251d
docs(readme): update the readme file for approve button
2cff312
chore(report|remote): typos and convert then to async/await for reada…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import styled from 'styled-components'; | ||
import { approveTest, filterTests } from '../../actions'; | ||
import { colors, fonts } from '../../styles'; | ||
|
||
const REMOTE_HOST = 'http://127.0.0.1'; | ||
const REMOTE_PORT = 3000; | ||
const APPROVE_STATUS_TO_LABEL_MAP = Object.freeze({ | ||
INITIAL: 'Approve', | ||
PENDING: 'Pending...', | ||
APPROVED: 'Approved', | ||
FAILED: 'Approve' | ||
}); | ||
|
||
const Button = styled.button` | ||
font-size: 20px; | ||
line-height: 40px; | ||
font-family: ${fonts.latoRegular}; | ||
background-color: ${colors.green}; | ||
border: none; | ||
border-radius: 3px; | ||
color: ${colors.white}; | ||
padding: 0px 30px; | ||
margin-bottom: 10px; | ||
|
||
&:hover { | ||
cursor: pointer; | ||
} | ||
|
||
&:disabled { | ||
background-color: ${colors.bodyColor}; | ||
color: ${colors.secondaryText}; | ||
cursor: default; | ||
} | ||
`; | ||
|
||
const ErrorMsg = styled.p` | ||
word-wrap: break-word; | ||
font-family: monospace; | ||
background: rgb(251, 234, 234); | ||
padding: 2ex; | ||
color: brown; | ||
`; | ||
|
||
class ApproveButton extends React.Component { | ||
constructor (props) { | ||
super(props); | ||
this.approve = this.approve.bind(this); | ||
this.state = { | ||
approveStatus: 'INITIAL', | ||
errorMsg: null | ||
}; | ||
} | ||
|
||
async approve () { | ||
const { fileName } = this.props; | ||
const url = `${REMOTE_HOST}:${REMOTE_PORT}/approve?filter=${fileName}`; | ||
this.setState({ approveStatus: 'PENDING' }); | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: 'POST' | ||
}); | ||
|
||
if (response.ok) { | ||
this.setState({ approveStatus: 'APPROVED' }); | ||
this.props.approveTest(this.props.currentId, this.props.filterStatus); | ||
} else { | ||
const body = await response.json(); | ||
this.setState({ approveStatus: 'FAILED', errorMsg: body.error }); | ||
} | ||
} catch (err) { | ||
this.setState({ | ||
approveStatus: 'FAILED', | ||
errorMsg: `${err.message}. Please check your network.` | ||
}); | ||
} | ||
} | ||
|
||
render () { | ||
const { approveStatus, errorMsg } = this.state; | ||
|
||
return ( | ||
<div> | ||
<Button onClick={this.approve} disabled={approveStatus === 'APPROVED'}> | ||
{APPROVE_STATUS_TO_LABEL_MAP[this.state.approveStatus]} | ||
</Button> | ||
{approveStatus === 'FAILED' && ( | ||
<ErrorMsg>BACKSTOP ERROR: {errorMsg}</ErrorMsg> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
filterStatus: state.tests.filterStatus | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
approveTest: (id, filterStatus) => { | ||
dispatch(approveTest(id)); | ||
dispatch(filterTests(filterStatus)); | ||
} | ||
}; | ||
}; | ||
|
||
const ApproveButtonContainer = connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(ApproveButton); | ||
export default ApproveButtonContainer; |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const fs = require('./fs'); | ||
|
||
/** | ||
* Extract jsonReport from the jsonpReport | ||
* | ||
* @param {String} jsonpReport - jsonpReport `report(${jsonReport});` | ||
* @return {Object} an object of jsonReport | ||
*/ | ||
function extractReport (jsonpReport) { | ||
const jsonReport = jsonpReport.substring(7, jsonpReport.length - 2); | ||
return JSON.parse(jsonReport); | ||
} | ||
|
||
/** | ||
* Helper function to modify the test status of the JSONP report based on the approved file name. | ||
* | ||
* @param {String} originalJsonpReport - jsonpReport `report(${jsonReport});` | ||
* @param {String} approvedFileName - the name of the screenshot that is approved | ||
* @return {String} jsonpReport - modified jsonpReport | ||
*/ | ||
function modifyJsonpReportHelper (originalJsonpReport, approvedFileName) { | ||
const report = extractReport(originalJsonpReport); | ||
report.tests.forEach(test => { | ||
if (test.pair.fileName === approvedFileName) { | ||
test.status = 'pass'; | ||
delete test.pair.diffImage; | ||
1276stella marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return test; | ||
}); | ||
|
||
const jsonReport = JSON.stringify(report, null, 2); | ||
const jsonpReport = `report(${jsonReport});`; | ||
return jsonpReport; | ||
} | ||
|
||
/** | ||
* Modify the test status of the JSONP report based on the approved file name. JSONP is used | ||
* to create the Backstop report in browser. This function ensures the UI consistency after | ||
* a user apporves a test in browser and refreshes the report without running a test. | ||
* | ||
* @param {Object} params - the input params | ||
* @param {String} params.reportConfigFilename - the path to the html report config file | ||
* @param {String} params.approvedFileName - the name of the screenshot that is approved | ||
* @return {Promise} | ||
*/ | ||
async function modifyJsonpReport ({ reportConfigFilename, approvedFileName }) { | ||
return fs | ||
.readFile(reportConfigFilename, 'utf8') | ||
.then(content => { | ||
const jsonpReport = modifyJsonpReportHelper(content[0], approvedFileName); | ||
return fs.writeFile(reportConfigFilename, jsonpReport); | ||
}) | ||
.catch(err => { | ||
throw new Error(`Failed to modify the report. ${err.message}.`); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
modifyJsonpReport, | ||
modifyJsonpReportHelper | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an open issue in
ember-backstop
to make backstop run on a port other than the default port 3000. Will having this as a constant make it harder to implement that ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great point. When Backstop opens a report (either internally or via user on cli) it runs openReport.js -- notice here is where the port is defined...
BackstopJS/core/command/openReport.js
Line 9 in 724f16e
So, we should be using your port bar there.
And it is probably just a matter of swapping that hard coded port 3000 with a dynamic one from the passed in config object.
AN ADDITIONAL THING TO NOTE: This also happens to be where we explicitly tell the report UI that it is in REMOTE mode. Some features, like
Approve
will only work in remote mode (i.e. when backstopRemote is running). I don't remember if the UI has a global variable to track this -- but it needs one. And theApprove
option should not be shown unless we are in remote mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@1276stella ☝️