Skip to content

Commit

Permalink
Merge pull request #1019 from hborham/bugfix/eachLike-v3-differs-from-v2
Browse files Browse the repository at this point in the history
fix: missing min in v3 eachLike #958
  • Loading branch information
mefellows committed Dec 8, 2022
2 parents a3e6b65 + 18dbfd4 commit 974d247
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
43 changes: 36 additions & 7 deletions src/v3/matchers.spec.ts
Expand Up @@ -35,17 +35,46 @@ describe('V3 Matchers', () => {
});

describe('#eachLike', () => {
it('returns a JSON representation of an eachLike matcher', () => {
const result = MatchersV3.eachLike({
a: 'b',
describe('with no min', () => {
it('returns a JSON representation of an eachLike matcher', () => {
const result = MatchersV3.eachLike({
a: 'b',
});
expect(result).to.deep.equal({
min: 1,
'pact:matcher:type': 'type',
value: [
{
a: 'b',
},
],
});
});
expect(result).to.deep.equal({
'pact:matcher:type': 'type',
value: [
});

describe('with min', () => {
it('returns a JSON representation of an eachLike matcher', () => {
const result = MatchersV3.eachLike(
{
a: 'b',
},
],
3
);
expect(result).to.deep.equal({
min: 3,
'pact:matcher:type': 'type',
value: [
{
a: 'b',
},
{
a: 'b',
},
{
a: 'b',
},
],
});
});
});
});
Expand Down
16 changes: 12 additions & 4 deletions src/v3/matchers.ts
Expand Up @@ -57,11 +57,19 @@ export const eachKeyLike = <T extends AnyTemplate>(
/**
* Array where each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
*/
export const eachLike = <T extends AnyTemplate>(template: T): Matcher<T[]> => ({
'pact:matcher:type': 'type',
value: [template],
});
export const eachLike = <T extends AnyTemplate>(
template: T,
min = 1
): MinLikeMatcher<T[]> => {
const elements = min;
return {
min,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};

/**
* Like Matcher with a minimum number of required values
Expand Down

0 comments on commit 974d247

Please sign in to comment.