|
| 1 | +export function toIncludeAllMembersInOrder(actual, expected) { |
| 2 | + const options = { |
| 3 | + isNot: this.isNot, |
| 4 | + promise: this.promise, |
| 5 | + }; |
| 6 | + |
| 7 | + // First, check contents. |
| 8 | + if (this.isNot) { |
| 9 | + expect(actual).not.toIncludeAllMembers(expected); |
| 10 | + } else { |
| 11 | + expect(actual).toIncludeAllMembers(expected); |
| 12 | + } |
| 13 | + |
| 14 | + // Second, check order. At this point, they must have the same length, so we |
| 15 | + // can go element by element and fail at the first elements that don't |
| 16 | + // match. |
| 17 | + const differingElementIdxs = expected |
| 18 | + .map((element, idx) => { |
| 19 | + return this.equals(element, actual[idx]) ? false : idx; |
| 20 | + }) |
| 21 | + .filter(idx => idx !== false); |
| 22 | + |
| 23 | + const pass = differingElementIdxs.length === 0; |
| 24 | + |
| 25 | + const message = pass |
| 26 | + ? () => |
| 27 | + // eslint-disable-next-line prefer-template |
| 28 | + this.utils.matcherHint('toIncludeAllMembersInOrder', undefined, undefined, options) + |
| 29 | + '\n\n' + |
| 30 | + `Expected: not ${this.utils.printExpected(expected)}\n` + |
| 31 | + `Received: ${this.utils.printReceived(actual)}` |
| 32 | + : () => { |
| 33 | + const firstDifferingElementIdx = differingElementIdxs[0]; |
| 34 | + const firstDifferingElementInActual = actual[firstDifferingElementIdx]; |
| 35 | + const firstDifferingElementInExpected = expected[firstDifferingElementIdx]; |
| 36 | + |
| 37 | + return ( |
| 38 | + // eslint-disable-next-line prefer-template |
| 39 | + this.utils.matcherHint('toIncludeAllMembersInOrder', undefined, undefined, options) + |
| 40 | + '\n\n' + |
| 41 | + `First differing element (index ${firstDifferingElementIdx}):\n\n` + |
| 42 | + `Expected: ${this.utils.printExpected(firstDifferingElementInExpected)}\n` + |
| 43 | + `Received: ${this.utils.printReceived(firstDifferingElementInActual)}` |
| 44 | + ); |
| 45 | + }; |
| 46 | + |
| 47 | + return { actual, message, pass }; |
| 48 | +} |
0 commit comments