Skip to content

Commit

Permalink
Optimize print calls in spy matches (#5596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha authored and cpojer committed Feb 19, 2018
1 parent 402a6fe commit e2bb8f4
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,27 @@ const ensureMock = (mockOrSpy, matcherName) => {
}
};

const getPrintedCalls = (
calls: any[],
limit: number,
sep: string,
fn: Function,
): string => {
const result = [];
let i = calls.length;

while (--i >= 0 && --limit >= 0) {
result.push(fn(calls[i]));
}

return result.join(sep);
};

const formatReceivedCalls = (calls, limit, options) => {
if (calls.length) {
const but = options && options.sameSentence ? 'but' : 'But';
const count = calls.length - limit;
const printedCalls = calls
.slice(-limit)
.reverse()
.map(printReceived)
.join(', ');
const printedCalls = getPrintedCalls(calls, limit, ', ', printReceived);
return (
`${but} it was ${options && options.isLast ? 'last ' : ''}called ` +
`with:\n ` +
Expand All @@ -196,11 +208,12 @@ const formatReceivedCalls = (calls, limit, options) => {

const formatMismatchedCalls = (calls, expected, limit) => {
if (calls.length) {
return calls
.slice(-limit)
.reverse()
.map(formatMismatchedArgs.bind(null, expected))
.join('\n\n');
return getPrintedCalls(
calls,
limit,
'\n\n',
formatMismatchedArgs.bind(null, expected),
);
} else {
return (
` ${printExpected(expected)}\n` +
Expand Down

0 comments on commit e2bb8f4

Please sign in to comment.