UNPKG

@pact-foundation/pact

Version:
675 lines 30.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const matchers_1 = require("./matchers"); describe('Matcher', () => { describe('can compile the types', () => { describe('with interfaces', () => { it('compiles with nested examples from issue 1054', () => { const f = { a: 'working example' }; (0, matchers_1.like)(f); (0, matchers_1.like)({ ...f }); (0, matchers_1.like)(Object.freeze(f)); const r = { id: 'some guid', foo: { a: 'example' } }; (0, matchers_1.like)(r); (0, matchers_1.like)({ ...r }); (0, matchers_1.like)(Object.freeze(f)); }); it('compiles when InterfaceToTemplate is used', () => { const template = { someArray: ['one', 'two'], someNumber: 1, someString: "it's a string", someObject: { foo: 'some string', bar: 'some other string', }, }; const _unused = (0, matchers_1.like)(template); }); }); describe('with types', () => { it('compiles', () => { const template = { someArray: ['one', 'two'], someNumber: 1, someString: "it's a string", someObject: { foo: 'some string', bar: 'some other string', }, }; const _unused = (0, matchers_1.like)(template); }); }); it('compiles nested likes', () => { const _unused = (0, matchers_1.like)({ someArray: ['one', 'two'], someNumber: (0, matchers_1.like)(1), someString: "it's a string", someObject: (0, matchers_1.like)({ foo: (0, matchers_1.like)('some string'), bar: 'some other string', }), }); }); }); describe('#validateExample', () => { describe('when given a valid regex', () => { describe('and a matching example', () => { it('returns true', () => { expect((0, matchers_1.validateExample)('2010-01-01', matchers_1.ISO8601_DATE_FORMAT)).toBe(true); }); }); describe('and a failing example', () => { it('returns false', () => { expect((0, matchers_1.validateExample)('not a date', matchers_1.ISO8601_DATE_FORMAT)).toBe(false); }); }); }); describe('when given an invalid regex', () => { it('returns an error', () => { expect(() => { (0, matchers_1.validateExample)('', 'abc('); }).toThrow(Error); }); }); }); describe('#term', () => { describe('when given a valid regular expression and example', () => { it('returns a serialized Ruby object', () => { const expected = { value: 'myawesomeword', regex: '\\w+', 'pact:matcher:type': 'regex', }; const match = (0, matchers_1.term)({ generate: 'myawesomeword', matcher: '\\w+', }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when not provided with a valid expression', () => { // biome-ignore lint/suspicious/noExplicitAny: intentionally passing invalid argument types to test error handling const createTheTerm = (badArg) => () => { (0, matchers_1.term)(badArg); }; describe('when no term is provided', () => { it('throws an Error', () => { expect(createTheTerm.call({}, undefined)).toThrow(Error); }); }); describe('when an invalid term is provided', () => { it('throws an Error', () => { expect(createTheTerm({})).toThrow(Error); expect(createTheTerm('')).toThrow(Error); expect(createTheTerm({ value: 'foo' })).toThrow(Error); expect(createTheTerm({ matcher: '\\w+' })).toThrow(Error); }); }); }); describe("when given an example that doesn't match the regular expression", () => { it('fails with an error', () => { expect(() => { (0, matchers_1.term)({ generate: 'abc', matcher: matchers_1.ISO8601_DATE_FORMAT, }); }).toThrow(Error); }); }); }); describe('#somethingLike', () => { describe('when provided a value', () => { it('returns a serialized Ruby object', () => { const expected = { value: 'myspecialvalue', 'pact:matcher:type': 'type', }; const match = (0, matchers_1.somethingLike)('myspecialvalue'); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when not provided with a valid value', () => { // biome-ignore lint/suspicious/noExplicitAny: intentionally passing invalid argument types to test error handling const createTheValue = (badArg) => () => { (0, matchers_1.somethingLike)(badArg); }; describe('when no value is provided', () => { it('`throws an Error', () => { expect(createTheValue.call({}, undefined)).toThrow(Error); }); }); describe('when an invalid value is provided', () => { it('throws an Error', () => { expect(createTheValue(undefined)).toThrow(Error); expect(createTheValue(() => { })).toThrow(Error); }); }); }); }); describe('#eachLike', () => { describe('when content is null', () => { it('provides null as contents', () => { const expected = { value: [null], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)(null, { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when an object is provided', () => { it('provides the object as contents', () => { const expected = { value: [{ a: 1 }], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)({ a: 1 }, { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when object.min is invalid', () => { it('throws an Error message', () => { expect(() => { (0, matchers_1.eachLike)({ a: 1 }, { min: 0 }); }).toThrow(Error); }); }); describe('when an array is provided', () => { it('provides the array as contents', () => { const expected = { value: [[1, 2, 3]], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)([1, 2, 3], { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when a value is provided', () => { it('adds the value in contents', () => { const expected = { value: ['test'], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)('test', { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('when the content has Pact.Macters', () => { describe('of type somethingLike', () => { it('nests somethingLike correctly', () => { const expected = { value: [ { id: { value: 10, 'pact:matcher:type': 'type', }, }, ], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)({ id: (0, matchers_1.somethingLike)(10) }, { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('of type term', () => { it('nests term correctly', () => { const expected = { value: [ { colour: { value: 'red', regex: 'red|green', 'pact:matcher:type': 'regex', }, }, ], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)({ colour: (0, matchers_1.term)({ generate: 'red', matcher: 'red|green', }), }, { min: 1 }); // expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('of type eachLike', () => { it('nests eachlike in contents', () => { const expected = { value: [ { value: ['blue'], 'pact:matcher:type': 'type', min: 1, }, ], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)((0, matchers_1.eachLike)('blue', { min: 1 }), { min: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('complex object with multiple Pact.Matchers', () => { it('nests objects correctly', () => { const expected = { value: [ { value: [ { colour: { value: 'red', 'pact:matcher:type': 'regex', regex: 'red|green|blue', }, size: { value: 10, 'pact:matcher:type': 'type', }, tag: { value: [ [ { value: 'jumper', 'pact:matcher:type': 'type', }, { value: 'shirt', 'pact:matcher:type': 'type', }, ], [ { value: 'jumper', 'pact:matcher:type': 'type', }, { value: 'shirt', 'pact:matcher:type': 'type', }, ], ], 'pact:matcher:type': 'type', min: 2, }, }, ], 'pact:matcher:type': 'type', min: 1, }, ], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)((0, matchers_1.eachLike)({ colour: (0, matchers_1.term)({ generate: 'red', matcher: 'red|green|blue' }), size: (0, matchers_1.somethingLike)(10), tag: (0, matchers_1.eachLike)([(0, matchers_1.somethingLike)('jumper'), (0, matchers_1.somethingLike)('shirt')], { min: 2 }), }, { min: 1 }), { min: 1 }); expect(JSON.parse(JSON.stringify(match))).toMatchObject(JSON.parse(JSON.stringify(expected))); }); }); }); describe('When no options.min is not provided', () => { it('defaults to a min of 1', () => { const expected = { value: [{ a: 1 }], 'pact:matcher:type': 'type', min: 1, }; const match = (0, matchers_1.eachLike)({ a: 1 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); describe('When a options.min is provided', () => { it('provides the object as contents', () => { const expected = { value: [{ a: 1 }, { a: 1 }, { a: 1 }], 'pact:matcher:type': 'type', min: 3, }; const match = (0, matchers_1.eachLike)({ a: 1 }, { min: 3 }); expect(JSON.stringify(match)).toContain(JSON.stringify(expected)); }); }); }); describe('#email', () => { describe('when given a valid Email address', () => { it('creates a valid matcher', () => { expect((0, matchers_1.email)('hello@world.com')).toBeTypeOf('object'); expect((0, matchers_1.email)('hello@world.com.au')).toBeTypeOf('object'); expect((0, matchers_1.email)('hello@a.co')).toBeTypeOf('object'); expect((0, matchers_1.email)()).toBeTypeOf('object'); }); }); describe('when given an invalid Email address', () => { it('returns an error', () => { expect(() => { (0, matchers_1.email)('hello.world.c'); }).toThrow(Error); }); }); }); describe('#uuid', () => { describe('when given a valid UUID', () => { it('creates a valid matcher', () => { expect((0, matchers_1.uuid)('ce118b6e-d8e1-11e7-9296-cec278b6b50a')).toBeTypeOf('object'); expect((0, matchers_1.uuid)()).toBeTypeOf('object'); }); }); describe('when given an invalid UUID', () => { it('returns an error', () => { expect(() => { (0, matchers_1.uuid)('abc'); }).toThrow(Error); }); }); }); describe('#ipv4Address', () => { describe('when given a valid ipv4Address', () => { it('creates a valid matcher', () => { expect((0, matchers_1.ipv4Address)('127.0.0.1')).toBeTypeOf('object'); expect((0, matchers_1.ipv4Address)()).toBeTypeOf('object'); }); }); describe('when given an invalid ipv4Address', () => { it('returns an error', () => { expect(() => { (0, matchers_1.ipv4Address)('abc'); }).toThrow(Error); }); }); }); describe('#ipv6Address', () => { describe('when given a valid ipv6Address', () => { it('creates a valid matcher', () => { expect((0, matchers_1.ipv6Address)('::1')).toBeTypeOf('object'); expect((0, matchers_1.ipv6Address)('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBeTypeOf('object'); expect((0, matchers_1.ipv6Address)()).toBeTypeOf('object'); }); }); describe('when given an invalid ipv6Address', () => { it('returns an error', () => { expect(() => { (0, matchers_1.ipv6Address)('abc'); }).toThrow(Error); }); }); }); describe('#hexadecimal', () => { describe('when given a valid hexadecimal', () => { it('creates a valid matcher', () => { expect((0, matchers_1.hexadecimal)('6F')).toBeTypeOf('object'); expect((0, matchers_1.hexadecimal)()).toBeTypeOf('object'); }); }); describe('when given an invalid hexadecimal', () => { it('returns an error', () => { expect(() => { (0, matchers_1.hexadecimal)('x1'); }).toThrow(Error); }); }); }); describe('#boolean', () => { describe('when used it should create a JSON object', () => { it('creates a valid matcher', () => { expect((0, matchers_1.boolean)()).toBeTypeOf('object'); expect((0, matchers_1.boolean)().value).toBe(true); }); it('sets value=false', () => { expect((0, matchers_1.boolean)(false)).toBeTypeOf('object'); expect((0, matchers_1.boolean)(false).value).toBe(false); }); it('sets value=true', () => { expect((0, matchers_1.boolean)(true)).toBeTypeOf('object'); expect((0, matchers_1.boolean)(true).value).toBe(true); }); }); }); describe('#string', () => { describe('when given a valid string', () => { it('creates a valid matcher', () => { expect((0, matchers_1.string)('test')).toBeTypeOf('object'); expect((0, matchers_1.string)()).toBeTypeOf('object'); expect((0, matchers_1.string)('test').value).toBe('test'); }); }); }); describe('#decimal', () => { describe('when given a valid decimal', () => { it('creates a valid matcher', () => { expect((0, matchers_1.decimal)(10.1)).toBeTypeOf('object'); expect((0, matchers_1.decimal)()).toBeTypeOf('object'); expect((0, matchers_1.decimal)(0.0).value).toBe(0.0); }); }); }); describe('#integer', () => { describe('when given a valid integer', () => { it('creates a valid matcher', () => { expect((0, matchers_1.integer)(10)).toBeTypeOf('object'); expect((0, matchers_1.integer)()).toBeTypeOf('object'); expect((0, matchers_1.integer)(0).value).toBe(0); }); }); }); describe('Date Matchers', () => { describe('#rfc1123Timestamp', () => { describe('when given a valid rfc1123Timestamp', () => { it('creates a valid matcher', () => { expect((0, matchers_1.rfc1123Timestamp)('Mon, 31 Oct 2016 15:21:41 -0400')).toBeTypeOf('object'); expect((0, matchers_1.rfc1123Timestamp)()).toBeTypeOf('object'); }); }); describe('when given an invalid rfc1123Timestamp', () => { it('returns an error', () => { expect(() => { (0, matchers_1.rfc1123Timestamp)('abc'); }).toThrow(Error); }); }); }); describe('#iso8601Time', () => { describe('when given a valid iso8601Time', () => { it('creates a valid matcher', () => { expect((0, matchers_1.iso8601Time)('T22:44:30.652Z')).toBeTypeOf('object'); expect((0, matchers_1.iso8601Time)()).toBeTypeOf('object'); }); }); describe('when given an invalid iso8601Time', () => { it('returns an error', () => { expect(() => { (0, matchers_1.iso8601Time)('abc'); }).toThrow(Error); }); }); }); describe('#iso8601Date', () => { describe('when given a valid iso8601Date', () => { it('creates a valid matcher', () => { expect((0, matchers_1.iso8601Date)('2017-12-05')).toBeTypeOf('object'); expect((0, matchers_1.iso8601Date)()).toBeTypeOf('object'); }); }); describe('when given an invalid iso8601Date', () => { it('returns an error', () => { expect(() => { (0, matchers_1.iso8601Date)('abc'); }).toThrow(Error); }); }); }); describe('#iso8601DateTime', () => { describe('when given a valid iso8601DateTime', () => { it('creates a valid matcher', () => { expect((0, matchers_1.iso8601DateTime)('2015-08-06T16:53:10+01:00')).toBeTypeOf('object'); expect((0, matchers_1.iso8601DateTime)()).toBeTypeOf('object'); }); }); describe('when given an invalid iso8601DateTime', () => { it('returns an error', () => { expect(() => { (0, matchers_1.iso8601DateTime)('abc'); }).toThrow(Error); }); }); }); describe('#iso8601DateTimeWithMillis', () => { describe('when given a valid iso8601DateTimeWithMillis', () => { it('creates a valid matcher', () => { expect((0, matchers_1.iso8601DateTimeWithMillis)('2015-08-06T16:53:10.123+01:00')).toBeTypeOf('object'); expect((0, matchers_1.iso8601DateTimeWithMillis)('2015-08-06T16:53:10.537357Z')).toBeTypeOf('object'); expect((0, matchers_1.iso8601DateTimeWithMillis)('2020-12-10T09:01:29.06Z')).toBeTypeOf('object'); expect((0, matchers_1.iso8601DateTimeWithMillis)('2020-12-10T09:01:29.1Z')).toBeTypeOf('object'); expect((0, matchers_1.iso8601DateTimeWithMillis)()).toBeTypeOf('object'); }); }); describe('when given an invalid iso8601DateTimeWithMillis', () => { it('returns an error', () => { expect(() => { (0, matchers_1.iso8601DateTimeWithMillis)('abc'); }).toThrow(Error); }); }); }); describe('#extractPayload', () => { describe('when given an object with no matchers', () => { const object = { some: 'data', more: 'strings', an: ['array'], someObject: { withData: true, withNumber: 1, }, }; it('returns just that object', () => { expect((0, matchers_1.extractPayload)(object)).toEqual(object); }); }); describe('when given an object with null values', () => { const object = { some: 'data', more: null, an: [null], someObject: { withData: true, withNumber: 1, andNull: null, }, }; it('returns just that object', () => { expect((0, matchers_1.extractPayload)(object)).toEqual(object); }); }); describe('when given an object with some matchers', () => { const someMatchers = { some: (0, matchers_1.somethingLike)('data'), more: 'strings', an: ['array'], another: (0, matchers_1.eachLike)('this'), someObject: { withData: (0, matchers_1.somethingLike)(true), withTerm: (0, matchers_1.term)({ generate: 'this', matcher: 'this|that' }), withNumber: 1, withAnotherNumber: (0, matchers_1.somethingLike)(2), }, }; const expected = { some: 'data', more: 'strings', an: ['array'], another: ['this'], someObject: { withData: true, withTerm: 'this', withNumber: 1, withAnotherNumber: 2, }, }; it('returns without matching guff', () => { expect((0, matchers_1.extractPayload)(someMatchers)).toEqual(expected); }); }); describe('when given a simple matcher', () => { it('removes all matching guff', () => { const expected = 'myawesomeword'; const matcher = (0, matchers_1.term)({ generate: 'myawesomeword', matcher: '\\w+', }); expect((0, matchers_1.isMatcher)(matcher)).toBe(true); expect((0, matchers_1.extractPayload)(matcher)).toEqual(expected); }); }); describe('when given a complex nested object with matchers', () => { it('removes all matching guff', () => { const o = (0, matchers_1.somethingLike)({ stringMatcher: { awesomeSetting: (0, matchers_1.somethingLike)('a string'), }, anotherStringMatcher: { nestedSetting: { anotherStringMatcherSubSetting: (0, matchers_1.somethingLike)(true), }, anotherSetting: (0, matchers_1.term)({ generate: 'this', matcher: 'this|that' }), }, arrayMatcher: { lotsOfValueregex: (0, matchers_1.eachLike)('useful', { min: 3 }), }, arrayOfMatcherregex: { lotsOfValueregex: (0, matchers_1.eachLike)({ foo: 'bar', baz: (0, matchers_1.somethingLike)('bat'), }, { min: 3 }), }, }); const expected = { stringMatcher: { awesomeSetting: 'a string', }, anotherStringMatcher: { nestedSetting: { anotherStringMatcherSubSetting: true, }, anotherSetting: 'this', }, arrayMatcher: { lotsOfValueregex: ['useful', 'useful', 'useful'], }, arrayOfMatcherregex: { lotsOfValueregex: [ { baz: 'bat', foo: 'bar', }, { baz: 'bat', foo: 'bar', }, { baz: 'bat', foo: 'bar', }, ], }, }; expect((0, matchers_1.extractPayload)(o)).toEqual(expected); }); }); }); }); }); //# sourceMappingURL=matchers.spec.js.map