Skip to content

Commit

Permalink
Fix incorrect matching (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge authored Jan 11, 2021
2 parents 2f93b4c + e183f85 commit 065b098
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function replicateWildcards<T>(actual: T[], expected: T[]): T[][] {
const n = expected.filter(isWildcard).length;
const k = actual.length - (expected.length - n);

if (k === 0) {
if (k === 0 || n === 0) {
return [expected.filter((e) => !isWildcard(e))];
}

Expand Down
30 changes: 30 additions & 0 deletions test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ describe('structure', () => {
assert(!looksLike(vnode2, vnode1));
});

it('should match replicated wildcards', () => {
const actual = h('div', [
h('div', {}, 'A_1'),
h('div', {}, 'A_2'),
h('div', {}, 'A_3'),
h('div', {}, 'A_4')
]);

const wrong1 = h('div', [Wildcard(), h('div', {}, 'B_2')]);
const wrong2 = h('div', [h('div', {}, 'B_2')]);

const expected1 = h('div', [
Wildcard(),
h('div', {}, 'A_3'),
h('div', {}, 'A_4')
]);
const expected2 = h('div', [Wildcard(), h('div', {}, 'A_4')]);
const expected3 = h('div', [
Wildcard(),
h('div', {}, 'A_2'),
Wildcard()
]);

assert.throws(() => assertLooksLike(actual, wrong1));
assert.throws(() => assertLooksLike(actual, wrong2));
assertLooksLike(actual, expected1);
assertLooksLike(actual, expected2);
assertLooksLike(actual, expected3);
});

it('should match Wildcards with JSX', () => {
const vnode1 = h('div', {}, [
h('span', {}, 'Hello'),
Expand Down

0 comments on commit 065b098

Please sign in to comment.