Skip to content

Commit

Permalink
feat: update suite tree once on Accept all
Browse files Browse the repository at this point in the history
  • Loading branch information
sipayRT committed Apr 22, 2018
1 parent 5ee7a3f commit 4c10f37
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 89 deletions.
2 changes: 1 addition & 1 deletion lib/gui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = class App {
}

updateReferenceImage(failedTests = []) {
this._tool.updateReferenceImage([].concat(failedTests));
return this._tool.updateReferenceImage(failedTests);
}

addClient(connection) {
Expand Down
1 change: 0 additions & 1 deletion lib/gui/constants/client-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
BEGIN_STATE: 'beginState',

TEST_RESULT: 'testResult',
UPDATE_RESULT: 'updateResult',

RETRY: 'retry',
ERROR: 'err',
Expand Down
6 changes: 3 additions & 3 deletions lib/gui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ exports.start = ({paths, tool, guiApi, configs}) => {
});

server.post('/update-reference', (req, res) => {
app.updateReferenceImage(req.body);

res.sendStatus(200);
app.updateReferenceImage(req.body)
.then((updatedTests) => res.json(updatedTests))
.catch(({message}) => res.status(500).send({error: message}));
});

onExit(() => {
Expand Down
20 changes: 20 additions & 0 deletions lib/gui/tool-runner-factory/base-tool-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
const path = require('path');
const _ = require('lodash');
const chalk = require('chalk');
const Promise = require('bluebird');
const ReportBuilderFactory = require('../../report-builder-factory');
const EventSource = require('../event-source');
const utils = require('../../server-utils');
const {findTestResult} = require('./utils');
const {findNode} = require('../../../lib/static/modules/utils');
const reporterHelper = require('../../reporter-helpers');

module.exports = class ToolRunner {
static create(paths, tool, configs) {
Expand Down Expand Up @@ -53,6 +56,23 @@ module.exports = class ToolRunner {
this._eventSource.emit(event, data);
}

updateReferenceImage(tests) {
const reportBuilder = this._reportBuilder;

return Promise.map(tests, (test) => {
const updateResult = this._prepareUpdateResult(test);
const formattedResult = reportBuilder.addUpdated(updateResult);

Object.assign(formattedResult, {actualPath: test.actualPath});
return reporterHelper.updateReferenceImage(formattedResult, this._reportPath)
.then(() => {
this._tool.emit(this._tool.events.UPDATE_RESULT, updateResult);

return findTestResult(reportBuilder.getSuites(), formattedResult.prepareTestResult());
});
});
}

_loadReuseData() {
try {
return utils.require(path.resolve(this._reportPath, 'data'));
Expand Down
7 changes: 0 additions & 7 deletions lib/gui/tool-runner-factory/gemini/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ module.exports = class GeminiRunner extends BaseToolRunner {
.run((collection) => this._tool.test(collection, {reporters: ['vflat']}));
}

updateReferenceImage(tests) {
tests.forEach((test) => {
const updateResult = this._prepareUpdateResult(test);
this._tool.emit(this._tool.events.UPDATE_RESULT, updateResult);
});
}

_readTests() {
const {grep, set, browser} = this._globalOpts;
const {autoRun} = this._guiOpts;
Expand Down
27 changes: 4 additions & 23 deletions lib/gui/tool-runner-factory/gemini/report-subscriber.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const _ = require('lodash');
const clientEvents = require('../../constants/client-events');
const {RUNNING} = require('../../../constants/test-statuses');
const {findNode} = require('../../../../lib/static/modules/utils');
const {saveTestImages, saveTestCurrentImage, updateReferenceImage} = require('../../../reporter-helpers');
const {saveTestImages, saveTestCurrentImage} = require('../../../reporter-helpers');
const {findTestResult} = require('../utils');

module.exports = (gemini, reportBuilder, client, reportPath) => {
gemini.on(gemini.events.BEGIN_SUITE, ({suite, browserId}) => {
Expand Down Expand Up @@ -34,16 +33,15 @@ module.exports = (gemini, reportBuilder, client, reportPath) => {
? reportBuilder.addSuccess(data)
: reportBuilder.addFail(data);

const testResult = prepareTestResult(data);
const testResult = findTestResult(reportBuilder.getSuites(), result.prepareTestResult());

saveTestImages(result, reportPath)
.then(() => client.emit(clientEvents.TEST_RESULT, testResult));
});

gemini.on(gemini.events.ERROR, (error) => {
const result = reportBuilder.addError(error);

const testResult = prepareTestResult(error);
const testResult = findTestResult(reportBuilder.getSuites(), result.prepareTestResult());

saveTestCurrentImage(result, reportPath)
.then(() => client.emit(gemini.events.ERROR, testResult));
Expand All @@ -59,24 +57,7 @@ module.exports = (gemini, reportBuilder, client, reportPath) => {
actionFn(result, reportPath);
});

gemini.on(gemini.events.UPDATE_RESULT, (data) => {
const result = reportBuilder.addUpdated(data);
const testResult = prepareTestResult(data);

updateReferenceImage(_.extend(result, {actualPath: data.actualPath}), reportPath)
.then(() => client.emit(clientEvents.UPDATE_RESULT, testResult));
});

gemini.on(gemini.events.END_RUNNER, () => {
return reportBuilder.save().then(() => client.emit(clientEvents.END));
});

function prepareTestResult(test) {
const {state: {name}, suite, browserId} = test;
const suitePath = suite.path.concat(name);
const nodeResult = findNode(reportBuilder.getSuites(), suitePath);
const browserResult = _.find(nodeResult.browsers, {name: browserId});

return {name, suitePath, browserId, browserResult};
}
};
8 changes: 0 additions & 8 deletions lib/gui/tool-runner-factory/hermione/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const crypto = require('crypto');
const _ = require('lodash');
const clientEvents = require('../../constants/client-events');
const BaseToolRunner = require('../base-tool-runner');
const subscribeOnToolEvents = require('./report-subscriber');
const {formatTests} = require('../utils');
Expand All @@ -21,13 +20,6 @@ module.exports = class HermioneRunner extends BaseToolRunner {
return this._tool.run(testPaths, {grep, sets, browsers});
}

updateReferenceImage(tests) {
tests.forEach((test) => {
const updateResult = this._prepareUpdateResult(test);
this._tool.emit(clientEvents.UPDATE_RESULT, updateResult);
});
}

_readTests() {
const {browser} = this._globalOpts;
const {autoRun} = this._guiOpts;
Expand Down
30 changes: 5 additions & 25 deletions lib/gui/tool-runner-factory/hermione/report-subscriber.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

const _ = require('lodash');
const clientEvents = require('../../constants/client-events');
const {RUNNING} = require('../../../constants/test-statuses');
const {findNode} = require('../../../../lib/static/modules/utils');
const {getSuitePath} = require('../../../plugin-utils').getHermioneUtils();
const {findTestResult} = require('../utils');
const {
saveTestImages, saveTestCurrentImage, updateReferenceImage, saveBase64Screenshot
saveTestImages, saveTestCurrentImage, saveBase64Screenshot
} = require('../../../reporter-helpers');

module.exports = (hermione, reportBuilder, client, reportPath) => {
Expand All @@ -33,15 +32,13 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
});

hermione.on(hermione.events.TEST_PASS, (data) => {
reportBuilder.addSuccess(data);

const testResult = prepareTestResult(data);
const formattedTest = reportBuilder.addSuccess(data);
const testResult = findTestResult(reportBuilder.getSuites(), formattedTest.prepareTestResult());

client.emit(clientEvents.TEST_RESULT, testResult);
});

hermione.on(hermione.events.TEST_FAIL, (data) => {
const testResult = prepareTestResult(data);
const formattedResult = reportBuilder.format(data);
const saveImageFn = getSaveImageFn(formattedResult);
const {assertViewState} = formattedResult;
Expand All @@ -50,6 +47,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
? reportBuilder.addFail(data, {assertViewState})
: reportBuilder.addError(data, {assertViewState});

const testResult = findTestResult(reportBuilder.getSuites(), formattedResult.prepareTestResult());
saveImageFn(result, reportPath)
.then(() => client.emit(clientEvents.TEST_RESULT, testResult));
});
Expand All @@ -61,28 +59,10 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
saveImageFn(result, reportPath);
});

// TODO: subscribe on hermione event when it will be added
hermione.on(clientEvents.UPDATE_RESULT, (data) => {
const result = reportBuilder.addUpdated(data);
const testResult = prepareTestResult(data);

updateReferenceImage(_.extend(result, {actualPath: data.actualPath}), reportPath)
.then(() => client.emit(clientEvents.UPDATE_RESULT, testResult));
});

hermione.on(hermione.events.RUNNER_END, () => {
return reportBuilder.save()
.then(() => client.emit(clientEvents.END));
});

function prepareTestResult(test) {
const {title: name, browserId, imagePath} = test;
const suitePath = getSuitePath(test);
const nodeResult = findNode(reportBuilder.getSuites(), suitePath);
const browserResult = _.find(nodeResult.browsers, {name: browserId});

return {name, suitePath, browserId, browserResult, imagePath};
}
};

function getSaveImageFn(formattedResult) {
Expand Down
9 changes: 9 additions & 0 deletions lib/gui/tool-runner-factory/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const _ = require('lodash');
const {findNode} = require('../../static/modules/utils');

exports.formatTests = (test, testHandler) => {
if (test.children) {
Expand All @@ -13,3 +14,11 @@ exports.formatTests = (test, testHandler) => {

return _.flatMap(test.browsers, (browser) => testHandler(browser, test));
};

exports.findTestResult = (suites = [], test) => {
const {name, suitePath, browserId} = test;
const nodeResult = findNode(suites, suitePath);
const browserResult = _.find(nodeResult.browsers, {name: browserId});

return {name, suitePath, browserId, browserResult};
};
9 changes: 2 additions & 7 deletions lib/static/components/suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {connect} from 'react-redux';
import SectionCommon from './section/section-common';
import {bindActionCreators} from 'redux';
import clientEvents from '../../gui/constants/client-events';
import {suiteBegin, testBegin, testResult, updateResult, testsEnd} from '../modules/actions';
import {suiteBegin, testBegin, testResult, testsEnd} from '../modules/actions';

class Suites extends Component {
static propTypes = {
Expand Down Expand Up @@ -38,11 +38,6 @@ class Suites extends Component {
});
});

eventSource.addEventListener(clientEvents.UPDATE_RESULT, (e) => {
const data = JSON.parse(e.data);
actions.updateResult(data);
});

eventSource.addEventListener(clientEvents.END, () => {
this.props.actions.testsEnd();
});
Expand All @@ -61,7 +56,7 @@ class Suites extends Component {
}
}

const actions = {testBegin, suiteBegin, testResult, updateResult, testsEnd};
const actions = {testBegin, suiteBegin, testResult, testsEnd};

export default connect(
(state) => ({
Expand Down
2 changes: 0 additions & 2 deletions lib/static/modules/action-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export default {
TEST_RESULT: 'TEST_RESULT',
TESTS_END: 'TEST_END',
UPDATE_RESULT: 'UPDATE_RESULT',
ACCEPT_ALL: 'ACCEPT_ALL',
ACCEPT_TEST: 'ACCEPT_TEST',
VIEW_INITIAL: 'VIEW_INITIAL',
VIEW_EXPAND_ALL: 'VIEW_EXPAND_ALL',
VIEW_EXPAND_ERRORS: 'VIEW_EXPAND_ERRORS',
Expand Down
13 changes: 5 additions & 8 deletions lib/static/modules/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,28 @@ export const retryTest = (suite, browserId = null) => {
return runFailedTests(assign({browserId}, suite), actionNames.RETRY_TEST);
};

export const acceptAll = (fails, actionName = actionNames.ACCEPT_ALL) => {
if (actionName === actionNames.ACCEPT_ALL) {
fails = filterAcceptableBrowsers([].concat(fails));
}
export const acceptAll = (fails) => {
fails = filterAcceptableBrowsers([].concat(fails));

const formattedFails = flatMap([].concat(fails), formatTests);

return async (dispatch) => {
try {
await axios.post('/update-reference', formattedFails);
dispatch({type: actionName});
const {data: updatedData} = await axios.post('/update-reference', formattedFails);
dispatch({type: actionNames.UPDATE_RESULT, payload: updatedData});
} catch (e) {
console.error('Error while updating references of failed tests:', e);
}
};
};

export const acceptTest = (suite, browserId, attempt) => {
return acceptAll(assign({browserId}, suite, {acceptTestAttempt: attempt}), actionNames.ACCEPT_TEST);
return acceptAll(assign({browserId}, suite, {acceptTestAttempt: attempt}));
};

export const suiteBegin = (suite) => ({type: actionNames.SUITE_BEGIN, payload: suite});
export const testBegin = (test) => ({type: actionNames.TEST_BEGIN, payload: test});
export const testResult = (result) => ({type: actionNames.TEST_RESULT, payload: result});
export const updateResult = (result) => ({type: actionNames.UPDATE_RESULT, payload: result});
export const testsEnd = () => ({type: actionNames.TESTS_END});
export const runFailed = () => ({type: actionNames.RUN_FAILED_TESTS});
export const expandAll = () => ({type: actionNames.VIEW_EXPAND_ALL});
Expand Down
13 changes: 9 additions & 4 deletions lib/static/modules/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,23 @@ export default function reducer(state = getInitialState(compiledData), action) {

function addTestResult(state, action) {
const suites = clone(state.suites);
const {suitePath, browserResult, browserId} = action.payload;
const test = findNode(suites, suitePath);

if (test) {
[].concat(action.payload).forEach((suite) => {
const {suitePath, browserResult, browserId} = suite;
const test = findNode(suites, suitePath);

if (!test) {
return;
}

test.browsers.forEach((b) => {
if (b.name === browserId) {
Object.assign(b, browserResult);
}
});
setStatusForBranch(suites, suitePath, browserResult.result.status);
forceUpdateSuiteData(suites, test);
}
});

const suiteIds = clone(state.suiteIds);
assign(suiteIds, {failed: getFailedSuiteIds(suites)});
Expand Down
7 changes: 7 additions & 0 deletions lib/test-adapter/gemini-test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ module.exports = class GeminiTestResultAdapter extends TestAdapter {
get currentPath() {
return this._testResult.currentPath;
}

prepareTestResult() {
const {state: {name}, suite, browserId} = this._testResult;
const suitePath = suite.path.concat(name);

return {name, suitePath, browserId};
}
};
8 changes: 8 additions & 0 deletions lib/test-adapter/hermione-test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const TestAdapter = require('./test-adapter');
const HermioneSuiteAdapter = require('../suite-adapter/hermione-suite-adapter.js');
const {IMAGE_DIFF_ERROR} = require('../constants/errors').getHermioneErrors();
const {getSuitePath} = require('../plugin-utils').getHermioneUtils();

module.exports = class HermioneTestResultAdapter extends TestAdapter {
constructor(testResult, config = {}) {
Expand Down Expand Up @@ -68,4 +69,11 @@ module.exports = class HermioneTestResultAdapter extends TestAdapter {
get description() {
return this._testResult.description;
}

prepareTestResult() {
const {title: name, browserId} = this._testResult;
const suitePath = getSuitePath(this._testResult);

return {name, suitePath, browserId};
}
};
Loading

0 comments on commit 4c10f37

Please sign in to comment.