Skip to content

Commit 420ca30

Browse files
committed
Fix additional promise methods and expect.it
1 parent 0cbe790 commit 420ca30

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

lib/addAdditionalPromiseMethods.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const Context = require('./Context');
2+
13
function addAdditionalPromiseMethods(promise, expect, subject) {
24
promise.and = function (...args) {
35
function executeAnd() {
@@ -19,6 +21,47 @@ function addAdditionalPromiseMethods(promise, expect, subject) {
1921
}
2022
};
2123

24+
const camelCaseMethods = expect._camelCaser(expect.context || new Context());
25+
Object.keys(camelCaseMethods).forEach((methodName) => {
26+
promise[methodName] = function (...args) {
27+
function execute(shiftedSubject) {
28+
if (expect.findTypeOf(args[0]).is('expect.it')) {
29+
return addAdditionalPromiseMethods(
30+
args[0](shiftedSubject),
31+
expect,
32+
subject
33+
);
34+
} else {
35+
return camelCaseMethods[methodName](shiftedSubject)(...args);
36+
}
37+
}
38+
39+
if (this.isFulfilled()) {
40+
return execute(this.value());
41+
} else {
42+
return addAdditionalPromiseMethods(this.then(execute), expect, subject);
43+
}
44+
};
45+
46+
promise[
47+
`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
48+
] = function (...args) {
49+
function execute() {
50+
if (expect.findTypeOf(args[0]).is('expect.it')) {
51+
return addAdditionalPromiseMethods(args[0](subject), expect, subject);
52+
} else {
53+
return camelCaseMethods[methodName](subject)(...args);
54+
}
55+
}
56+
57+
if (this.isFulfilled()) {
58+
return execute(this.value());
59+
} else {
60+
return addAdditionalPromiseMethods(this.then(execute), expect, subject);
61+
}
62+
};
63+
});
64+
2265
return promise;
2366
}
2467

lib/createTopLevelExpect.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ function createExpectIt(expect, expectations, forwardedFlags) {
244244
copiedExpectations.push(OR, args);
245245
return createExpectIt(expect, copiedExpectations, forwardedFlags);
246246
};
247+
248+
const camelCaseMethods = expect._camelCaser(expect.context || new Context());
249+
Object.keys(camelCaseMethods).forEach((methodName) => {
250+
expectIt[
251+
`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
252+
] = function (...args) {
253+
const copiedExpectations = expectations.slice();
254+
copiedExpectations.push([
255+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
256+
...args,
257+
]);
258+
return createExpectIt(expect, copiedExpectations, forwardedFlags);
259+
};
260+
261+
expectIt[
262+
`or${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
263+
] = function (...args) {
264+
const copiedExpectations = expectations.slice();
265+
copiedExpectations.push(OR, [
266+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
267+
...args,
268+
]);
269+
return createExpectIt(expect, copiedExpectations, forwardedFlags);
270+
};
271+
});
272+
247273
return expectIt;
248274
}
249275

@@ -730,7 +756,7 @@ expectPrototype.addAssertion = function (
730756
] = (...args) =>
731757
createExpectIt(
732758
this._topLevelExpect,
733-
[[testDescriptionString, ...args]],
759+
[[handler.testDescriptionString, ...args]],
734760
this.flags
735761
);
736762

@@ -1323,7 +1349,11 @@ expectPrototype._createWrappedExpect = function (
13231349
if (arguments.length === 0) {
13241350
throw new Error('The expect function requires at least one parameter.');
13251351
} else if (arguments.length === 1) {
1326-
return parentExpect._camelCaser(subject, context);
1352+
return parentExpect._camelCaser(
1353+
context,
1354+
parentExpect.findTypeOf(subject),
1355+
subject
1356+
);
13271357
} else if (typeof testDescriptionString === 'function') {
13281358
wrappedExpect.errorMode = 'nested';
13291359
return wrappedExpect.withError(
@@ -1460,28 +1490,34 @@ expectPrototype._executeExpect = function (
14601490
return oathbreaker(assertionRule.handler(wrappedExpect, subject, ...args));
14611491
};
14621492

1463-
expectPrototype._camelCaser = function (subject, context) {
1464-
const subjectType = this.findTypeOf(subject);
1493+
expectPrototype._camelCaser = function (context, subjectType, subject) {
14651494
const methods = {};
14661495
let instance = this;
14671496
while (instance) {
14681497
Object.keys(instance.assertions).forEach((testDescriptionString) => {
14691498
if (
1499+
!subjectType ||
14701500
instance.assertions[testDescriptionString].some((assertion) =>
14711501
subjectType.is(assertion.subject.type)
14721502
)
14731503
) {
1474-
methods[
1475-
testDescriptionString.replace(/ [a-z]/g, ($0) =>
1476-
$0.charAt(1).toUpperCase()
1477-
)
1478-
] = (...args) => {
1504+
let method = (subject) => (...args) => {
14791505
return this._expect(context, [
14801506
subject,
14811507
testDescriptionString,
14821508
...args,
14831509
]);
14841510
};
1511+
1512+
if (subjectType) {
1513+
method = method(subject);
1514+
}
1515+
1516+
methods[
1517+
testDescriptionString.replace(/ [a-z]/g, ($0) =>
1518+
$0.charAt(1).toUpperCase()
1519+
)
1520+
] = method;
14851521
}
14861522
});
14871523
instance = instance.parent;
@@ -1496,7 +1532,7 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
14961532
if (args.length < 1) {
14971533
throw new Error('The expect function requires at least one parameter.');
14981534
} else if (args.length === 1) {
1499-
return this._camelCaser(subject, context);
1535+
return this._camelCaser(context, this.findTypeOf(subject), subject);
15001536
} else if (typeof testDescriptionString === 'function') {
15011537
return this.withError(
15021538
() => testDescriptionString(subject),

0 commit comments

Comments
 (0)