UNPKG

@pact-foundation/pact

Version:
223 lines 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const request_1 = require("../common/request"); const interaction_1 = require("./interaction"); const matchers_1 = require("./matchers"); describe('Interaction', () => { describe('#given', () => { it('creates Interaction with provider state', () => { const actual = new interaction_1.Interaction() .uponReceiving('r') .given('provider state'); expect(actual.state).toEqual({ description: 'r', providerState: 'provider state', }); }); describe('without provider state', () => { it('creates Interaction when blank', () => { const actual = new interaction_1.Interaction().uponReceiving('r').given('').state; expect(actual).toEqual({ description: 'r' }); }); it('creates Interaction when nothing is passed', () => { const actual = new interaction_1.Interaction().uponReceiving('r').state; expect(actual).toEqual({ description: 'r' }); }); }); }); describe('#uponReceiving', () => { const interaction = new interaction_1.Interaction(); it('throws error when no description provided', () => { expect(interaction.uponReceiving).toThrow('You must provide a description for the interaction.'); }); it('has a state with description', () => { interaction.uponReceiving('an interaction description'); expect(interaction.state).toEqual({ description: 'an interaction description', }); }); }); describe('#withRequest', () => { const interaction = new interaction_1.Interaction(); it('throws error when method is not provided', () => { expect(interaction.withRequest.bind(interaction, {})).toThrow('You must provide an HTTP method.'); }); it('throws error when an invalid method is provided', () => { expect(interaction.withRequest.bind(interaction, { method: 'FOO', })).toThrow('You must provide a valid HTTP method: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, COPY, LOCK, MKCOL, MOVE, PROPFIND, PROPPATCH, UNLOCK, REPORT.'); }); it('throws error when method is not provided but path is provided', () => { expect(interaction.withRequest.bind(interaction, { path: '/', })).toThrow('You must provide an HTTP method.'); }); it('throws error when path is not provided', () => { expect(interaction.withRequest.bind(interaction, { method: request_1.HTTPMethods.GET, })).toThrow('You must provide a path.'); }); it('throws error when query object is not a string', () => { expect(interaction.withRequest.bind(interaction, { method: request_1.HTTPMethods.GET, path: '/', query: { string: false, query: 'false' }, })).toThrow('Query must only contain strings.'); }); describe('with only mandatory params', () => { const actual = new interaction_1.Interaction() .uponReceiving('a request') .withRequest({ method: request_1.HTTPMethods.GET, path: '/search' }).state; it('has a state containing only the given keys', () => { expect(actual).toHaveProperty('request'); expect(actual.request).toHaveProperty('method'); expect(actual.request).toHaveProperty('path'); }); it('request has no other keys', () => { expect(actual.request).not.toHaveProperty('query'); expect(actual.request).not.toHaveProperty('headers'); expect(actual.request).not.toHaveProperty('body'); }); }); describe('with all other parameters', () => { const actual = new interaction_1.Interaction().uponReceiving('request').withRequest({ body: { id: 1, name: 'Test', due: 'tomorrow' }, headers: { 'Content-Type': 'application/json' }, method: request_1.HTTPMethods.GET, path: '/search', query: 'q=test', }).state; it('has a full state all available keys', () => { expect(actual).toHaveProperty('request'); expect(actual.request).toHaveProperty('method'); expect(actual.request).toHaveProperty('path'); expect(actual.request).toHaveProperty('query'); expect(actual.request).toHaveProperty('headers'); expect(actual.request).toHaveProperty('body'); }); }); describe('query type', () => { const request = { body: { id: 1, name: 'Test', due: 'tomorrow' }, headers: { 'Content-Type': 'application/json' }, method: request_1.HTTPMethods.GET, path: '/search', query: {}, }; it('is passed with matcher', () => { request.query = (0, matchers_1.term)({ generate: 'limit=50&status=finished&order=desc', matcher: '^limit=[0-9]+&status=(finished)&order=(desc|asc)$', }); expect(new interaction_1.Interaction().uponReceiving('request').withRequest(request).state .request).toHaveProperty('query'); }); it('is passed with matcher as the value', () => { request.query = { 'id[]': (0, matchers_1.eachLike)('1'), }; expect(new interaction_1.Interaction().uponReceiving('request').withRequest(request).state .request).toHaveProperty('query'); }); it('is passed with object', () => { request.query = { id: '1', }; expect(new interaction_1.Interaction().uponReceiving('request').withRequest(request).state .request).toHaveProperty('query'); }); it('is passed with array', () => { request.query = { id: ['1', '2'], }; expect(new interaction_1.Interaction().uponReceiving('request').withRequest(request).state .request?.query).toEqual({ id: ['1', '2'] }); }); }); describe('request body', () => { it('is included when an empty string is specified', () => { const actual = new interaction_1.Interaction().uponReceiving('request').withRequest({ body: '', method: request_1.HTTPMethods.GET, path: '/path', }).state; expect(actual.request).toHaveProperty('body'); }); it('is not included when explicitly set to undefined', () => { const actual = new interaction_1.Interaction().uponReceiving('request').withRequest({ body: undefined, method: request_1.HTTPMethods.GET, path: '/path', }).state; expect(actual.request).not.toHaveProperty('body'); }); }); }); describe('#willRespondWith', () => { let interaction; beforeEach(() => { interaction = new interaction_1.Interaction(); }); it('throws error when status is not provided', () => { expect(interaction.willRespondWith.bind(interaction, {})).toThrow('You must provide a status code.'); }); it('throws error when status is blank', () => { expect(interaction.willRespondWith.bind(interaction, { status: '', })).toThrow('You must provide a status code.'); }); describe('with only mandatory params', () => { let actual; beforeEach(() => { interaction.uponReceiving('request'); interaction.willRespondWith({ status: 200 }); actual = interaction.state; }); it('has a state compacted with only present keys', () => { expect(actual).toHaveProperty('response'); expect(actual.response).toHaveProperty('status'); }); it('request has no other keys', () => { expect(actual.response).not.toHaveProperty('headers'); expect(actual.response).not.toHaveProperty('body'); }); }); describe('with all other parameters', () => { let actual; beforeEach(() => { interaction.uponReceiving('request'); interaction.willRespondWith({ body: { id: 1, name: 'Test', due: 'tomorrow' }, headers: { 'Content-Type': 'application/json' }, status: 404, }); actual = interaction.state; }); it('has a full state all available keys', () => { expect(actual).toHaveProperty('response'); expect(actual.response).toHaveProperty('status'); expect(actual.response).toHaveProperty('headers'); expect(actual.response).toHaveProperty('body'); }); }); describe('response body', () => { it('is included when an empty string is specified', () => { interaction.uponReceiving('request').willRespondWith({ body: '', status: 204, }); const actual = interaction.state; expect(actual.response).toHaveProperty('body'); }); it('is not included when explicitly set to undefined', () => { interaction.uponReceiving('request').willRespondWith({ body: undefined, status: 204, }); const actual = interaction.state; expect(actual.response).not.toHaveProperty('body'); }); }); }); }); //# sourceMappingURL=interaction.spec.js.map