@pact-foundation/pact
Version:
Pact for all things Javascript
107 lines • 4.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const logger_1 = __importDefault(require("../../../../common/logger"));
const setupStates_1 = require("./setupStates");
describe('#setupStates', () => {
const state = {
state: 'thing exists',
action: 'setup',
params: {},
};
const state2 = {
state: 'another thing exists',
action: 'setup',
params: {
id: 1,
},
};
const providerBaseUrl = 'http://not.exists';
let executed;
let setup;
let teardown;
const DEFAULT_OPTIONS = () => ({
providerBaseUrl,
requestFilter: (_req, _res, next) => {
next();
},
stateHandlers: {
[state.state]: () => {
executed = true;
return Promise.resolve({});
},
[state2.state]: {
setup: (params) => {
setup = true;
return Promise.resolve(params);
},
teardown: (params) => {
teardown = true;
return Promise.resolve(params);
},
},
},
});
let opts;
beforeEach(() => {
opts = DEFAULT_OPTIONS();
executed = false;
setup = false;
teardown = false;
});
afterEach(() => {
vitest_1.vi.restoreAllMocks();
});
describe('when there are provider states on the pact', () => {
describe('and there are handlers associated with those states', () => {
describe('that return provider state injected values', () => {
it('executes the handler and returns the data', async () => {
opts.stateHandlers = {
[state.state]: () => {
executed = true;
return Promise.resolve({ data: true });
},
};
const res = await (0, setupStates_1.setupStates)(state, opts);
expect(res).toHaveProperty('data', true);
expect(executed).toBe(true);
});
});
describe('that do not return a value', () => {
it('executes the handler and returns an empty Promise', async () => {
await (0, setupStates_1.setupStates)(state, opts);
expect(executed).toBe(true);
});
});
describe('that specify a setup and teardown function', () => {
it('executes the lifecycle specific handler and returns any provider state injected values', async () => {
const res = await (0, setupStates_1.setupStates)(state2, opts);
expect(res).toBe(state2.params);
expect(setup).toBe(true);
expect(teardown).toBe(false);
setup = false;
const res2 = await (0, setupStates_1.setupStates)({
...state2,
action: 'teardown',
}, opts);
expect(res2).toBe(state2.params);
expect(teardown).toBe(true);
expect(setup).toBe(false);
});
});
});
describe('and there are no handlers associated with those states', () => {
it('does not execute the handler and returns an empty Promise', async () => {
const spy = vitest_1.vi.spyOn(logger_1.default, 'warn');
delete opts.stateHandlers;
await (0, setupStates_1.setupStates)(state, opts);
expect(spy).toHaveBeenCalledTimes(1);
expect(executed).toBe(false);
});
});
});
});
//# sourceMappingURL=setupStates.spec.js.map