@pact-foundation/pact
Version:
Pact for all things Javascript
82 lines • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const interactionWithRequest_1 = require("./interactionWithRequest");
const matchers_1 = require("../../v3/matchers");
describe('InteractionWithRequest', () => {
let withStatus;
let withResponseMatchingRules;
let interaction;
let pact;
let cleanupFn;
beforeEach(() => {
withStatus = vitest_1.vi.fn();
withResponseMatchingRules = vitest_1.vi.fn();
interaction = {
withStatus,
withResponseMatchingRules,
};
pact = {
pactffiCreateMockServerForTransport: vitest_1.vi.fn().mockReturnValue(1234),
mockServerMatchedSuccessfully: vitest_1.vi.fn().mockReturnValue(true),
mockServerMismatches: vitest_1.vi.fn().mockReturnValue([]),
cleanupMockServer: vitest_1.vi.fn().mockReturnValue(true),
writePactFile: vitest_1.vi.fn(),
cleanupPlugins: vitest_1.vi.fn(),
};
cleanupFn = vitest_1.vi.fn();
});
afterEach(() => {
vitest_1.vi.restoreAllMocks();
});
describe('#willRespondWith', () => {
it('calls withStatus with a plain number', () => {
const req = new interactionWithRequest_1.InteractionWithRequest(pact, interaction, { consumer: 'A', provider: 'B' }, cleanupFn);
req.willRespondWith(200);
expect(withStatus).toHaveBeenCalledOnce();
expect(withStatus).toHaveBeenCalledWith(200);
expect(withResponseMatchingRules).not.toHaveBeenCalled();
});
it('calls withStatus with the example value from a StatusCodeMatcher', () => {
const req = new interactionWithRequest_1.InteractionWithRequest(pact, interaction, { consumer: 'A', provider: 'B' }, cleanupFn);
const matcher = (0, matchers_1.matchStatus)(200, matchers_1.HTTPResponseStatusClass.Success);
req.willRespondWith(matcher);
expect(withStatus).toHaveBeenCalledOnce();
expect(withStatus).toHaveBeenCalledWith(200);
});
it('calls withResponseMatchingRules with status class matcher FFI format', () => {
const req = new interactionWithRequest_1.InteractionWithRequest(pact, interaction, { consumer: 'A', provider: 'B' }, cleanupFn);
const matcher = (0, matchers_1.matchStatus)(200, matchers_1.HTTPResponseStatusClass.Success);
req.willRespondWith(matcher);
expect(withResponseMatchingRules).toHaveBeenCalledOnce();
const rulesJson = JSON.parse(withResponseMatchingRules.mock.calls[0][0]);
expect(rulesJson).toEqual({
status: {
$: {
matchers: [{ match: 'statusCode', status: 'success' }],
},
},
});
});
it('calls withResponseMatchingRules with specific status code matcher format', () => {
const req = new interactionWithRequest_1.InteractionWithRequest(pact, interaction, { consumer: 'A', provider: 'B' }, cleanupFn);
const matcher = (0, matchers_1.matchStatus)(200, [200, 201]);
req.willRespondWith(matcher);
expect(withResponseMatchingRules).toHaveBeenCalledOnce();
const rulesJson = JSON.parse(withResponseMatchingRules.mock.calls[0][0]);
expect(rulesJson).toEqual({
status: {
$: {
matchers: [{ match: 'statusCode', status: [200, 201] }],
},
},
});
});
it('does not call withResponseMatchingRules for a plain number', () => {
const req = new interactionWithRequest_1.InteractionWithRequest(pact, interaction, { consumer: 'A', provider: 'B' }, cleanupFn);
req.willRespondWith(201);
expect(withResponseMatchingRules).not.toHaveBeenCalled();
});
});
});
//# sourceMappingURL=interactionWithRequest.spec.js.map