Skip to content

Commit

Permalink
feat: add "scaleImages" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy-kiselyov committed Apr 4, 2018
1 parent 5fd1f37 commit e5371aa
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ directory.
* `all` - show all tests. Default value.
* `failed` - show only failed tests.
* **baseHost** (optional) - `String` - it changes original host for view in the browser; by default original host does not change
* **scaleImages** (optional) – `Boolean` – fit images into page width; `false` by default

Also there is ability to override plugin parameters by CLI options or environment variables
(see [configparser](https://github.com/gemini-testing/configparser)).
Expand Down
26 changes: 15 additions & 11 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,41 @@ const option = configParser.option;
const ENV_PREFIX = 'html_reporter_';
const CLI_PREFIX = '--html-reporter-';

const isString = (name) => {
const assertType = (name, validationFn, type) => {
return (v) => {
if (!_.isString(v)) {
throw new Error(`"${name}" option must be string, but got ${typeof v}`);
if (!validationFn(v)) {
throw new Error(`"${name}" option must be ${type}, but got ${typeof v}`);
}
};
};
const assertString = (name) => assertType(name, _.isString, 'string');
const assertBoolean = (name) => assertType(name, _.isBoolean, 'boolean');

const getParser = () => {
return root(section({
enabled: option({
defaultValue: true,
parseEnv: JSON.parse,
parseCli: JSON.parse,
validate: (v) => {
if (!_.isBoolean(v)) {
throw new Error(`"enabled" option must be boolean, but got ${typeof v}`);
}
}
validate: assertBoolean('enabled')
}),
path: option({
defaultValue: 'html-report',
validate: isString('path')
validate: assertString('path')
}),
defaultView: option({
defaultValue: 'all',
validate: isString('defaultView')
validate: assertString('defaultView')
}),
baseHost: option({
defaultValue: '',
validate: isString('baseHost')
validate: assertString('baseHost')
}),
scaleImages: option({
defaultValue: false,
parseEnv: JSON.parse,
parseCli: JSON.parse,
validate: assertBoolean('scaleImages')
})
}), {envPrefix: ENV_PREFIX, cliPrefix: CLI_PREFIX});
};
Expand Down
4 changes: 2 additions & 2 deletions lib/report-builder-factory/report-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ module.exports = class ReportBuilder {
}

getResult() {
const {defaultView, baseHost} = this._pluginConfig;
const {defaultView, baseHost, scaleImages} = this._pluginConfig;

return _.extend({
skips: _.uniq(this._skips, JSON.stringify),
suites: this._tree.children,
config: {defaultView, baseHost}
config: {defaultView, baseHost, scaleImages}
}, this._stats);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/static/components/controls/common-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ControlButtons extends Component {
/>
<ControlButton
label="Scale images"
isActive={view.scaleImages}
isActive={view.scaleImagesPressed}
handler={actions.toggleScaleImages}
/>
<BaseHostInput/>
Expand Down
6 changes: 3 additions & 3 deletions lib/static/components/state/state-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class StateFail extends Component {
actual: PropTypes.string.isRequired,
diff: PropTypes.string.isRequired,
showOnlyDiff: PropTypes.bool.isRequired,
scaleImages: PropTypes.bool.isRequired
scaleImagesPressed: PropTypes.bool.isRequired
}

render() {
const {expected, actual, diff} = this.props;
const className = classNames(
'image-box__container',
{'image-box__container_scale': this.props.scaleImages}
{'image-box__container_scale': this.props.scaleImagesPressed}
);

return (
Expand Down Expand Up @@ -56,6 +56,6 @@ class StateFail extends Component {
export default connect(
({view}) => ({
showOnlyDiff: view.showOnlyDiff,
scaleImages: view.scaleImages
scaleImagesPressed: view.scaleImagesPressed
})
)(StateFail);
3 changes: 2 additions & 1 deletion lib/static/modules/default-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
},
config: {
defaultView: 'all',
scaleImages: false,
baseHost: ''
},
stats: {
Expand All @@ -29,7 +30,7 @@ export default {
showSkipped: false,
showRetries: false,
showOnlyDiff: false,
scaleImages: false,
scaleImagesPressed: false,
baseHost: ''
}
};
7 changes: 4 additions & 3 deletions lib/static/modules/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function getInitialState(compiledData) {
stats: {total, updated, passed, failed, skipped, retries, warned},
view: {
viewMode: config.defaultView,
scaleImagesPressed: config.scaleImages,
..._loadBaseHost(config.baseHost, localStorage)
}
}, formattedSuites);
Expand All @@ -29,10 +30,10 @@ function getInitialState(compiledData) {
export default function reducer(state = getInitialState(compiledData), action) {
switch (action.type) {
case actionNames.VIEW_INITIAL: {
const {gui, autoRun, suites, skips} = action.payload;
const {gui, autoRun, suites, skips, config: {scaleImages}} = action.payload;
const formattedSuites = formatSuitesData(suites);

return merge({}, state, {gui, autoRun, skips}, formattedSuites);
return merge({}, state, {gui, autoRun, skips, view: {scaleImagesPressed: scaleImages}}, formattedSuites);
}
case actionNames.RUN_ALL_TESTS: {
const suites = clone(state.suites);
Expand Down Expand Up @@ -100,7 +101,7 @@ export default function reducer(state = getInitialState(compiledData), action) {
return _mutateStateView(state, {showOnlyDiff: !state.view.showOnlyDiff});
}
case actionNames.VIEW_TOGGLE_SCALE_IMAGES: {
return _mutateStateView(state, {scaleImages: !state.view.scaleImages});
return _mutateStateView(state, {scaleImagesPressed: !state.view.scaleImagesPressed});
}
case actionNames.VIEW_UPDATE_BASE_HOST: {
const baseHost = action.host;
Expand Down
106 changes: 101 additions & 5 deletions test/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,49 @@
const parseConfig = require('../../lib/config');

describe('config', () => {
afterEach(() => delete process.env['html_reporter_path']);
beforeEach(function() {
this.oldArgv = process.argv;
});

afterEach(function() {
process.argv = this.oldArgv;

delete process.env['html_reporter_enabled'];
delete process.env['html_reporter_path'];
delete process.env['html_reporter_default_view'];
delete process.env['html_reporter_base_host'];
delete process.env['html_reporter_scale_images'];
});

describe('"enabled" option', () => {
it('should be true by default', () => {
assert.isTrue(parseConfig({}).enabled);
});

it('should set from configuration file', () => {
const config = parseConfig({enabled: false});

assert.isFalse(config.enabled);
});

it('should set from environment variable', () => {
process.env['html_reporter_enabled'] = 'false';

assert.isFalse(parseConfig({}).enabled);
});

it('should set from cli', () => {
process.argv = process.argv.concat('--html-reporter-enabled', 'false');

it('should be enabled by default', () => {
assert.equal(parseConfig({}).enabled, true);
assert.isFalse(parseConfig({}).enabled);
});
});

describe('html report path', () => {
describe('"path" option', () => {
it('should be "html-report" by default', () => {
assert.equal(parseConfig({}).path, 'html-report');
});

it('should set from configuration file', () => {
const config = parseConfig({path: 'some/report/path'});

Expand All @@ -21,9 +57,15 @@ describe('config', () => {

assert.equal(parseConfig({}).path, 'env/report/path');
});

it('should set from cli', () => {
process.argv = process.argv.concat('--html-reporter-path', 'cli/report/path');

assert.equal(parseConfig({}).path, 'cli/report/path');
});
});

describe('default view settings', () => {
describe('"defaultView" option', () => {
it('should show all suites by default', () => {
assert.equal(parseConfig({}).defaultView, 'all');
});
Expand All @@ -39,5 +81,59 @@ describe('config', () => {

assert.equal(parseConfig({}).defaultView, 'env/some-view');
});

it('should set from cli', () => {
process.argv = process.argv.concat('--html-reporter-default-view', 'cli/some-view');

assert.equal(parseConfig({}).defaultView, 'cli/some-view');
});
});

describe('"baseHost" option', () => {
it('should be empty by default', () => {
assert.equal(parseConfig({}).baseHost, '');
});

it('should set from configuration file', () => {
const config = parseConfig({baseHost: 'some-host'});

assert.equal(config.baseHost, 'some-host');
});

it('should set from environment variable', () => {
process.env['html_reporter_base_host'] = 'env/some-host';

assert.equal(parseConfig({}).baseHost, 'env/some-host');
});

it('should set from cli', () => {
process.argv = process.argv.concat('--html-reporter-base-host', 'cli/some-host');

assert.equal(parseConfig({}).baseHost, 'cli/some-host');
});
});

describe('"scaleImages" option', () => {
it('should be false by default', () => {
assert.isFalse(parseConfig({}).scaleImages);
});

it('should set from configuration file', () => {
const config = parseConfig({scaleImages: true});

assert.isTrue(config.scaleImages);
});

it('should set from environment variable', () => {
process.env['html_reporter_scale_images'] = 'true';

assert.isTrue(parseConfig({}).scaleImages);
});

it('should set from cli', () => {
process.argv = process.argv.concat('--html-reporter-scale-images', 'true');

assert.isTrue(parseConfig({}).scaleImages);
});
});
});

0 comments on commit e5371aa

Please sign in to comment.