Skip to content

Commit

Permalink
Fixed remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
kirovboris committed Jan 9, 2017
1 parent 56a88db commit ed40d0c
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 85 deletions.
34 changes: 17 additions & 17 deletions src/client-functions/selector-builder/add-api.js
Expand Up @@ -90,21 +90,21 @@ async function getSnapshot (getSelector, callsite) {
return node;
}

function assertSnapshotExtensionOptions (extensions) {
if (!isNullOrUndefined(extensions)) {
assertObject('extend', '"extend" option', extensions);
function assertAddCustomDOMPropertiesOptions (properties) {
if (!isNullOrUndefined(properties)) {
assertObject('addCustomDOMProperties', '"addCustomDOMProperties" option', properties);

Object.keys(extensions).forEach(prop => {
assertFunction('extend', `Snapshot extension method '${prop}'`, extensions[prop]);
Object.keys(properties).forEach(prop => {
assertFunction('addCustomDOMProperties', `Custom DOM properties method '${prop}'`, properties[prop]);
});
}
}

function addSnapshotPropertyShorthands (obj, getSelector, snapshotExtensions) {
function addSnapshotPropertyShorthands (obj, getSelector, customDOMProperties) {
var properties = SNAPSHOT_PROPERTIES;

if (snapshotExtensions)
properties = properties.concat(Object.keys(snapshotExtensions));
if (customDOMProperties)
properties = properties.concat(Object.keys(customDOMProperties));

properties.forEach(prop => {
Object.defineProperty(obj, prop, {
Expand Down Expand Up @@ -210,7 +210,7 @@ function convertFilterToClientFunctionIfNecessary (callsiteName, filter, depende

function createDerivativeSelectorWithFilter (getSelector, SelectorBuilder, selectorFn, filter, additionalDependencies) {
var collectionModeSelectorBuilder = new SelectorBuilder(getSelector(), { collectionMode: true });
var snapshotExtensions = collectionModeSelectorBuilder.options.snapshotExtensions;
var customDOMProperties = collectionModeSelectorBuilder.options.customDOMProperties;

var dependencies = {
selector: collectionModeSelectorBuilder.getFunction(),
Expand All @@ -220,7 +220,7 @@ function createDerivativeSelectorWithFilter (getSelector, SelectorBuilder, selec

dependencies = assign(dependencies, additionalDependencies);

var builder = new SelectorBuilder(selectorFn, { dependencies, snapshotExtensions }, { instantiation: 'Selector' });
var builder = new SelectorBuilder(selectorFn, { dependencies, customDOMProperties }, { instantiation: 'Selector' });

return builder.getFunction();
}
Expand Down Expand Up @@ -262,11 +262,11 @@ function addFilterMethods (obj, getSelector, SelectorBuilder) {
};
}

function addCustomDomProperty (obj, getSelector, SelectorBuilder) {
obj.extend = extensions => {
assertSnapshotExtensionOptions(extensions);
function addCustomDOMPropertiesMethod (obj, getSelector, SelectorBuilder) {
obj.addCustomDOMProperties = properties => {
assertAddCustomDOMPropertiesOptions(properties);

var builder = new SelectorBuilder(getSelector(), { snapshotExtensions: extensions }, { instantiation: 'Selector' });
var builder = new SelectorBuilder(getSelector(), { customDOMProperties: properties }, { instantiation: 'Selector' });

return builder.getFunction();
};
Expand Down Expand Up @@ -397,9 +397,9 @@ function addHierarchicalSelectors (obj, getSelector, SelectorBuilder) {
};
}

export default function addAPI (obj, getSelector, SelectorBuilder, snapshotExtensions) {
addSnapshotPropertyShorthands(obj, getSelector, snapshotExtensions);
addCustomDomProperty(obj, getSelector, SelectorBuilder);
export default function addAPI (obj, getSelector, SelectorBuilder, customDOMProperties) {
addSnapshotPropertyShorthands(obj, getSelector, customDOMProperties);
addCustomDOMPropertiesMethod(obj, getSelector, SelectorBuilder);
addFilterMethods(obj, getSelector, SelectorBuilder);
addHierarchicalSelectors(obj, getSelector, SelectorBuilder);
addCounterProperties(obj, getSelector, SelectorBuilder);
Expand Down
14 changes: 7 additions & 7 deletions src/client-functions/selector-builder/index.js
Expand Up @@ -103,15 +103,15 @@ export default class SelectorBuilder extends ClientFunctionBuilder {
this._addBoundArgsSelectorGetter(lazyPromise, args);

// OPTIMIZATION: use buffer function as selector not to trigger lazy property ahead of time
addAPI(lazyPromise, () => lazyPromise.selector, SelectorBuilder, this.options.snapshotExtensions);
addAPI(lazyPromise, () => lazyPromise.selector, SelectorBuilder, this.options.customDOMProperties);

return lazyPromise;
}

getFunctionDependencies () {
var dependencies = super.getFunctionDependencies();
var text = this.options.text;
var snapshotExtensions = this.options.snapshotExtensions;
var dependencies = super.getFunctionDependencies();
var text = this.options.text;
var customDOMProperties = this.options.customDOMProperties;

if (typeof text === 'string')
text = new RegExp(escapeRe(text));
Expand All @@ -124,8 +124,8 @@ export default class SelectorBuilder extends ClientFunctionBuilder {
text: text
},

boundArgs: this.options.boundArgs,
snapshotExtensions: snapshotExtensions
boundArgs: this.options.boundArgs,
customDOMProperties: customDOMProperties
});
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export default class SelectorBuilder extends ClientFunctionBuilder {
_decorateFunction (selectorFn) {
super._decorateFunction(selectorFn);

addAPI(selectorFn, () => selectorFn, SelectorBuilder, this.options.snapshotExtensions);
addAPI(selectorFn, () => selectorFn, SelectorBuilder, this.options.customDOMProperties);
}

_decorateFunctionResult (nodeSnapshot, selectorArgs) {
Expand Down
Expand Up @@ -6,13 +6,10 @@ import { UncaughtErrorInClientFunctionCode } from '../../../../errors/test-run';

export default class ClientFunctionExecutor {
constructor (command) {
this.command = command;
this.replicator = createReplicator([new FunctionTransform()]);

this.command = command;
this.replicator = this._createReplicator();
this.dependencies = this.replicator.decode(this.command.dependencies);

this._addReplicatorTransform();

this.fn = evalFunction(this.command.fnCode, this.dependencies);
}

Expand Down Expand Up @@ -47,8 +44,11 @@ export default class ClientFunctionExecutor {
}

//Overridable methods
_addReplicatorTransform () {
this.replicator.addTransforms([new ClientFunctionNodeTransform(this.command.instantiationCallsiteName)]);
_createReplicator () {
return createReplicator([
new ClientFunctionNodeTransform(this.command.instantiationCallsiteName),
new FunctionTransform()
]);
}

_executeFn (args) {
Expand Down
@@ -1,7 +1,7 @@
import Replicator from 'replicator';
import evalFunction from './eval-function';
import { NodeSnapshot, ElementSnapshot } from './selector-executor/node-snapshots';
import { DomNodeClientFunctionResultError, UncaughtErrorInSnapshotExtensionCode } from '../../../../errors/test-run';
import { DomNodeClientFunctionResultError, UncaughtErrorInCustomDOMPropertyCode } from '../../../../errors/test-run';

// NOTE: save original ctors because they may be overwritten by page code
var Node = window.Node;
Expand Down Expand Up @@ -39,18 +39,18 @@ export class FunctionTransform {
}

export class SelectorNodeTransform {
constructor (extensions) {
constructor (customDOMProperties) {
this.type = 'Node';
this.extensions = extensions || {};
this.customDOMProperties = customDOMProperties || {};
}

_extend (snapshot, node) {
Object.keys(this.extensions).forEach(prop => {
Object.keys(this.customDOMProperties).forEach(prop => {
try {
snapshot[prop] = this.extensions[prop](node);
snapshot[prop] = this.customDOMProperties[prop](node);
}
catch (err) {
throw new UncaughtErrorInSnapshotExtensionCode(this.instantiationCallsiteName, err, prop);
throw new UncaughtErrorInCustomDOMPropertyCode(this.instantiationCallsiteName, err, prop);
}
});
}
Expand Down
Expand Up @@ -2,7 +2,7 @@ import { Promise } from '../../../deps/hammerhead';
import { delay, positionUtils, domUtils } from '../../../deps/testcafe-core';
import { selectElement as selectElementUI } from '../../../deps/testcafe-ui';
import ClientFunctionExecutor from '../client-function-executor';
import { SelectorNodeTransform } from '../replicator';
import { createReplicator, FunctionTransform, SelectorNodeTransform } from '../replicator';
import './filter';

const CHECK_ELEMENT_DELAY = 200;
Expand Down Expand Up @@ -38,12 +38,16 @@ export default class SelectorExecutor extends ClientFunctionExecutor {

this.timeout = Math.max(this.timeout - elapsed, 0);
}
}

_addReplicatorTransform () {
var snapshotExtensions = this.dependencies && this.dependencies.snapshotExtensions;
var customDOMProperties = this.dependencies && this.dependencies.customDOMProperties;

this.replicator.addTransforms([new SelectorNodeTransform(customDOMProperties)]);
}

this.replicator.addTransforms([new SelectorNodeTransform(snapshotExtensions)]);
_createReplicator () {
return createReplicator([
new FunctionTransform()
]);
}

_checkElement (el, startTime, condition, createTimeoutError, reCheck) {
Expand Down
4 changes: 2 additions & 2 deletions src/errors/test-run/index.js
Expand Up @@ -127,9 +127,9 @@ export class UncaughtErrorInClientFunctionCode extends TestRunErrorBase {
}
}

export class UncaughtErrorInSnapshotExtensionCode extends TestRunErrorBase {
export class UncaughtErrorInCustomDOMPropertyCode extends TestRunErrorBase {
constructor (instantiationCallsiteName, err, prop) {
super(TYPE.uncaughtErrorInSnapshotExtensionCode, err, prop);
super(TYPE.uncaughtErrorInCustomDOMPropertyCode, err, prop);

this.errMsg = String(err);
this.property = prop;
Expand Down
4 changes: 2 additions & 2 deletions src/errors/test-run/templates.js
Expand Up @@ -78,8 +78,8 @@ export default {
${escapeHtml(err.errMsg)}
`),

[TYPE.uncaughtErrorInSnapshotExtensionCode]: err => markup(err, `
An error occurred when trying to calculate a custom ${err.property} property:
[TYPE.uncaughtErrorInCustomDOMPropertyCode]: err => markup(err, `
An error occurred when trying to calculate a custom Selector property ${err.property}:
${escapeHtml(err.errMsg)}
`),
Expand Down
2 changes: 1 addition & 1 deletion src/errors/test-run/type.js
Expand Up @@ -8,7 +8,7 @@ export default {
uncaughtErrorInTestCode: 'uncaughtErrorInTestCode',
uncaughtNonErrorObjectInTestCode: 'uncaughtNonErrorObjectInTestCode',
uncaughtErrorInClientFunctionCode: 'uncaughtErrorInClientFunctionCode',
uncaughtErrorInSnapshotExtensionCode: 'uncaughtErrorInSnapshotExtensionCode',
uncaughtErrorInCustomDOMPropertyCode: 'uncaughtErrorInCustomDOMPropertyCode',
missingAwaitError: 'missingAwaitError',
actionIntegerOptionError: 'actionIntegerOptionError',
actionPositiveIntegerOptionError: 'actionPositiveIntegerOptionError',
Expand Down
22 changes: 11 additions & 11 deletions test/functional/fixtures/api/es-next/selector/test.js
Expand Up @@ -243,45 +243,45 @@ describe('[API] Selector', function () {
});
});

it('Should raise error if snapshot extension argument is not object',
it('Should raise error if addCustomDOMProperties method argument is not object',
function () {
return runTests('./testcafe-fixtures/selector-test.js', 'Snapshot extend method - argument is not object', {
return runTests('./testcafe-fixtures/selector-test.js', 'Add custom DOM properties method - argument is not object', {
shouldFail: true,
only: 'chrome'
})
.catch(function (errs) {
expect(errs[0]).contains(
'"extend" option is expected to be an object, but it was number.'
'"addCustomDOMProperties" option is expected to be an object, but it was number.'
);
expect(errs[0]).contains("> 932 | await Selector('rect').extend(42);");
expect(errs[0]).contains("> 932 | await Selector('rect').addCustomDOMProperties(42);");
});
}
);

it('Should raise error if at least one of snapshot extensions is not function',
it('Should raise error if at least one of custom DOM properties is not function',
function () {
return runTests('./testcafe-fixtures/selector-test.js', 'Snapshot extend method - extension is not function', {
return runTests('./testcafe-fixtures/selector-test.js', 'Add custom DOM properties method - property is not function', {
shouldFail: true,
only: 'chrome'
})
.catch(function (errs) {
expect(errs[0]).contains(
"Snapshot extension method \'prop1\' is expected to be a function, but it was number"
"Custom DOM properties method \'prop1\' is expected to be a function, but it was number"
);
expect(errs[0]).contains("> 936 | await Selector('rect').extend({ prop1: 1, prop2: () => 42 });");
expect(errs[0]).contains("> 936 | await Selector('rect').addCustomDOMProperties({ prop1: 1, prop2: () => 42 });");
});
}
);

it('Should raise error if snapshot extension throws an error',
it('Should raise error if custom DOM property throws an error',
function () {
return runTests('./testcafe-fixtures/selector-test.js', 'Snapshot extend method - extension throws an error', {
return runTests('./testcafe-fixtures/selector-test.js', 'Add custom DOM properties method - property throws an error', {
shouldFail: true,
only: 'chrome'
})
.catch(function (errs) {
expect(errs[0]).contains(
'An error occurred when trying to calculate a custom prop property: Error: test'
'An error occurred when trying to calculate a custom Selector property prop: Error: test'
);
expect(errs[0]).contains('> 946 | await el();');
});
Expand Down
Expand Up @@ -885,59 +885,59 @@ test('Selector filter origin node argument', async t => {
}).id).eql('el3');
});

test('Selector `extend` method', async () => {
test('Selector `extend` method', async t => {
let el = Selector('rect')
.extend({
.addCustomDOMProperties({
prop1: () => 42,
prop2: node => 'tagName: ' + node.tagName
});

expect(await el.prop1).eql(42);
expect(await el.parent().filter(() => true).prop2).eql('tagName: svg');
expect(await el.exists).to.be.true;
expect(await el.count).eql(1);
await t.expect(await el.prop1).eql(42)
.expect(await el.parent().filter(() => true).prop2).eql('tagName: svg')
.expect(await el.exists).ok()
.expect(await el.count).eql(1);

el = el.extend({
el = el.addCustomDOMProperties({
prop2: () => 'other value',
prop3: () => 'test'
});

expect(await el.prop1).eql(42);
expect(await el.prop2).eql('other value');
expect(await el.prop3).eql('test');
await t.expect(await el.prop1).eql(42)
.expect(await el.prop2).eql('other value')
.expect(await el.prop3).eql('test');

const elSnapshot = await Selector('rect')
.extend({
.addCustomDOMProperties({
prop1: () => 1,
prop2: () => 2
})();

expect(elSnapshot.prop1).eql(1);
expect(elSnapshot.prop2).eql(2);
await t.expect(elSnapshot.prop1).eql(1)
.expect(elSnapshot.prop2).eql(2);

const nonExistingElement = await Selector('nonExistingElement').extend({
const nonExistingElement = await Selector('nonExistingElement').addCustomDOMProperties({
prop: () => 'value'
})();

expect(nonExistingElement).eql(null);
await t.expect(nonExistingElement).eql(null);

const getSecondEl = Selector('div').extend({
const getSecondEl = Selector('div').addCustomDOMProperties({
prop: () => 'second'
}).nth(1);

expect(await getSecondEl.prop).eql('second');
await t.expect(await getSecondEl.prop).eql('second');
});

test('Snapshot extend method - argument is not object', async () => {
await Selector('rect').extend(42);
test('Add custom DOM properties method - argument is not object', async () => {
await Selector('rect').addCustomDOMProperties(42);
});

test('Snapshot extend method - extension is not function', async () => {
await Selector('rect').extend({ prop1: 1, prop2: () => 42 });
test('Add custom DOM properties method - property is not function', async () => {
await Selector('rect').addCustomDOMProperties({ prop1: 1, prop2: () => 42 });
});

test('Snapshot extend method - extension throws an error', async () => {
const el = Selector('rect').extend({
test('Add custom DOM properties method - property throws an error', async () => {
const el = Selector('rect').addCustomDOMProperties({
prop: () => {
throw new Error('test');
}
Expand Down
@@ -1,4 +1,4 @@
An error occurred when trying to calculate a custom prop property:
An error occurred when trying to calculate a custom Selector property prop:

Error: Custom script error

Expand Down

0 comments on commit ed40d0c

Please sign in to comment.