Skip to content

Commit 49ff316

Browse files
committed
Fix additional promise methods and expect.it
1 parent 3e18d2f commit 49ff316

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

@@ -714,7 +740,7 @@ expectPrototype.addAssertion = function (
714740
] = (...args) =>
715741
createExpectIt(
716742
this._topLevelExpect,
717-
[[testDescriptionString, ...args]],
743+
[[handler.testDescriptionString, ...args]],
718744
this.flags
719745
);
720746

@@ -1312,7 +1338,11 @@ expectPrototype._createWrappedExpect = function (
13121338
if (arguments.length === 0) {
13131339
throw new Error('The expect function requires at least one parameter.');
13141340
} else if (arguments.length === 1) {
1315-
return parentExpect._camelCaser(subject, context);
1341+
return parentExpect._camelCaser(
1342+
context,
1343+
parentExpect.findTypeOf(subject),
1344+
subject
1345+
);
13161346
} else if (typeof testDescriptionString === 'function') {
13171347
wrappedExpect.errorMode = 'nested';
13181348
return wrappedExpect.withError(
@@ -1449,28 +1479,34 @@ expectPrototype._executeExpect = function (
14491479
return oathbreaker(assertionRule.handler(wrappedExpect, subject, ...args));
14501480
};
14511481

1452-
expectPrototype._camelCaser = function (subject, context) {
1453-
const subjectType = this.findTypeOf(subject);
1482+
expectPrototype._camelCaser = function (context, subjectType, subject) {
14541483
const methods = {};
14551484
let instance = this;
14561485
while (instance) {
14571486
Object.keys(instance.assertions).forEach((testDescriptionString) => {
14581487
if (
1488+
!subjectType ||
14591489
instance.assertions[testDescriptionString].some((assertion) =>
14601490
subjectType.is(assertion.subject.type)
14611491
)
14621492
) {
1463-
methods[
1464-
testDescriptionString.replace(/ [a-z]/g, ($0) =>
1465-
$0.charAt(1).toUpperCase()
1466-
)
1467-
] = (...args) => {
1493+
let method = (subject) => (...args) => {
14681494
return this._expect(context, [
14691495
subject,
14701496
testDescriptionString,
14711497
...args,
14721498
]);
14731499
};
1500+
1501+
if (subjectType) {
1502+
method = method(subject);
1503+
}
1504+
1505+
methods[
1506+
testDescriptionString.replace(/ [a-z]/g, ($0) =>
1507+
$0.charAt(1).toUpperCase()
1508+
)
1509+
] = method;
14741510
}
14751511
});
14761512
instance = instance.parent;
@@ -1485,7 +1521,7 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
14851521
if (args.length < 1) {
14861522
throw new Error('The expect function requires at least one parameter.');
14871523
} else if (args.length === 1) {
1488-
return this._camelCaser(subject, context);
1524+
return this._camelCaser(context, this.findTypeOf(subject), subject);
14891525
} else if (typeof testDescriptionString === 'function') {
14901526
return this.withError(
14911527
() => testDescriptionString(subject),

0 commit comments

Comments
 (0)