Skip to content

Commit

Permalink
Make t.ctx assignable
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Jan 9, 2017
1 parent f62d05e commit 6b59c21
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/api/test-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default class TestController {
this.testRun = testRun;
this.executionChain = Promise.resolve();
this.callsiteWithoutAwait = null;
this.ctxStorage = testRun.ctxStorage;
}

// NOTE: we track missing `awaits` by exposing a special custom Promise to user code.
Expand Down Expand Up @@ -116,7 +115,13 @@ export default class TestController {
// methods in chained wrappers then we will have callsite for the wrapped method
// in this file instead of chained method callsite in user code.
_ctx$getter () {
return this.ctxStorage;
return this.testRun.ctx;
}

_ctx$setter (val) {
this.testRun.ctx = val;

return this.testRun.ctx;
}

_click$ (selector, options) {
Expand Down
2 changes: 1 addition & 1 deletion src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class TestRun extends Session {
this.pendingRequest = null;
this.pendingPageError = null;

this.ctxStorage = {};
this.ctx = Object.create(null);

this.errs = [];

Expand Down
13 changes: 8 additions & 5 deletions src/utils/delegated-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const API_IMPLEMENTATION_METHOD_RE = /^_(\S+)\$(getter)?$/;
const API_IMPLEMENTATION_METHOD_RE = /^_(\S+)\$(getter|setter)?$/;

export function getDelegatedAPIList (src) {
return Object
Expand All @@ -10,7 +10,7 @@ export function getDelegatedAPIList (src) {
return {
srcProp: prop,
apiProp: match[1],
isGetter: match[2]
accessor: match[2]
};
}

Expand All @@ -20,7 +20,7 @@ export function getDelegatedAPIList (src) {
}

export function delegateAPI (src, dest, apiList, proxyMethod, useDynamicMethodCtx) {
apiList.forEach(({ srcProp, apiProp, isGetter }) => {
apiList.forEach(({ srcProp, apiProp, accessor }) => {
var fn = function (...args) {
if (proxyMethod)
proxyMethod();
Expand All @@ -30,8 +30,11 @@ export function delegateAPI (src, dest, apiList, proxyMethod, useDynamicMethodCt
return ctx[srcProp](...args);
};

if (isGetter)
Object.defineProperty(dest, apiProp, { get: fn });
if (accessor === 'getter')
Object.defineProperty(dest, apiProp, { get: fn, configurable: true });

else if (accessor === 'setter')
Object.defineProperty(dest, apiProp, { set: fn, configurable: true });

else
dest[apiProp] = fn;
Expand Down
13 changes: 8 additions & 5 deletions test/functional/fixtures/api/es-next/hooks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ describe('[API] t.ctx', function () {
var browsers = [];

Object.keys(errs).forEach(function (browser) {
var ctxJson = errs[browser][0].match(/###(.+)###/)[1];
var ctx = JSON.parse(ctxJson);
var dataJson = errs[browser][0].match(/###(.+)###/)[1];
var data = JSON.parse(dataJson);

// NOTE: check context assignment
expect(data.ctx).eql(123);

// NOTE: check that we have same browser for each stage
expect(uniq(ctx.browsers).length).eql(1);
expect(ctx.steps).eql(['before', 'test', 'after']);
expect(uniq(data.val.browsers).length).eql(1);
expect(data.val.steps).eql(['before', 'test', 'after']);

browsers.push(ctx.browsers[0]);
browsers.push(data.val.browsers[0]);
});

// NOTE: check that each context is from different browsers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ test
t.ctx.val.browsers.push(await t.eval(()=>navigator.userAgent));
t.ctx.val.steps.push('after');

throw `###${JSON.stringify(t.ctx.val)}###`;
var val = t.ctx.val;

t.ctx = 123;

throw `###${JSON.stringify({ val, ctx: t.ctx })}###`;
});

0 comments on commit 6b59c21

Please sign in to comment.