msw-request-assertions
Version:
Request assertions for MSW
826 lines (793 loc) • 30.9 kB
JavaScript
var msw = require('msw');
function checkMockedHandler(input) {
const isHttpHandler = input instanceof msw.HttpHandler;
const isGraphQLHandler = input instanceof msw.GraphQLHandler;
if (!isHttpHandler && !isGraphQLHandler) {
throw new Error("Expected a HttpHandler or GraphQLHandler");
}
}
const toHaveBeenRequested = {
name: "toHaveBeenRequested",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const requestedAssertion = mockFn();
requestedAssertion.mockName(
typeof path === "string" ? path : path.source
);
const newResolver = (info, ...args) => {
requestedAssertion();
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.requestedAssertion = requestedAssertion;
return handler;
},
interceptGql: (mockFn, original) => (path, resolver, options, ...rest) => {
const requestedAssertion = mockFn();
requestedAssertion.mockName(
typeof path === "string" ? path : "source" in path ? path.source : JSON.stringify(path)
);
const newResolver = (info, ...args) => {
requestedAssertion();
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.requestedAssertion = requestedAssertion;
return handler;
},
assert: function(received) {
checkMockedHandler(received);
if (!received.requestedAssertion)
throw new Error("No request assertion found");
const calls = received.requestedAssertion.mock.calls;
const { isNot } = this;
return {
pass: calls.length > 0,
message: () => `Expected ${received.requestedAssertion?.getMockName()} to${isNot ? " not" : ""} have been requested`
};
}
};
const toHaveBeenRequestedTimes = {
name: "toHaveBeenRequestedTimes",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, expectedTimes) {
checkMockedHandler(received);
if (!received.requestedAssertion)
throw new Error("No request assertion found");
const calls = received.requestedAssertion.mock.calls;
const actualTimes = calls.length;
const { isNot } = this;
return {
pass: actualTimes === expectedTimes,
message: () => `Expected ${received.requestedAssertion?.getMockName()} to${isNot ? " not" : ""} have been requested ${expectedTimes} times, but it was requested ${actualTimes} times`
};
}
};
const isPlainObject = (value) => {
return typeof value === "object" && value !== null && value.constructor === Object;
};
const checkEquality = (expected, actual) => {
if (expected === actual) {
return true;
}
if (typeof expected === "object" && expected !== null && "asymmetricMatch" in expected && typeof expected.asymmetricMatch === "function") {
return expected.asymmetricMatch(actual);
}
if (typeof actual === "object" && actual !== null && "asymmetricMatch" in actual && typeof actual.asymmetricMatch === "function") {
return actual.asymmetricMatch(expected);
}
if (expected === null || actual === null) {
return expected === actual;
}
if (expected === void 0 || actual === void 0) {
return expected === actual;
}
if (typeof expected !== typeof actual) {
return false;
}
if (Array.isArray(expected) && Array.isArray(actual)) {
if (expected.length !== actual.length) {
return false;
}
return expected.every((item, index) => checkEquality(item, actual[index]));
}
if (isPlainObject(expected) && isPlainObject(actual)) {
const expectedKeys = Object.keys(expected);
const actualKeys = Object.keys(actual);
if (expectedKeys.length !== actualKeys.length) {
return false;
}
return expectedKeys.every(
(key) => actualKeys.includes(key) && checkEquality(expected[key], actual[key])
);
}
return false;
};
const ordinalOf = (i) => {
const j = i % 10;
const k = i % 100;
if (j === 1 && k !== 11) {
return `${i}st`;
}
if (j === 2 && k !== 12) {
return `${i}nd`;
}
if (j === 3 && k !== 13) {
return `${i}rd`;
}
return `${i}th`;
};
const formatMockCalls = (name, calls, msg = "") => {
if (calls.length) {
msg += `
Received:
${calls.map((callArg, i) => {
let methodCall = ` ${ordinalOf(i + 1)} ${name} call:
`;
methodCall += JSON.stringify(callArg[0]).split("\n").map((line) => ` ${line}`).join("\n");
methodCall += "\n";
return methodCall;
}).join("\n")}`;
}
msg += `
Number of calls: ${calls.length}
`;
return msg;
};
const getCalls = (received, expected) => {
if (!expected) throw new Error("Expected must be defined");
if (typeof expected !== "object")
throw new Error("Expected must be an object");
const bodyAssertionCalls = received.bodyAssertion?.mock.calls ?? [];
const queryStringAssertionCalls = received.queryStringAssertion?.mock.calls ?? [];
const jsonBodyAssertionCalls = received.jsonBodyAssertion?.mock.calls ?? [];
const headersAssertionCalls = received.headersAssertion?.mock.calls ?? [];
const hashAssertionCalls = received.hashAssertion?.mock.calls ?? [];
const pathParametersAssertionCalls = received.pathParametersAssertion?.mock.calls || [];
const gqlVariablesAssertionCalls = received instanceof msw.GraphQLHandler ? received.gqlVariablesAssertion?.mock.calls ?? [] : [];
const gqlQueryAssertionCalls = received instanceof msw.GraphQLHandler ? received.gqlQueryAssertion?.mock.calls ?? [] : [];
return bodyAssertionCalls.map((bodyAssertionCall, idx) => {
const call = {};
if ("jsonBody" in expected) {
call.jsonBody = jsonBodyAssertionCalls[idx]?.[0];
}
if ("body" in expected) {
call.body = bodyAssertionCall?.[0];
}
if ("queryString" in expected) {
call.queryString = queryStringAssertionCalls[idx]?.[0];
}
if ("headers" in expected) {
call.headers = headersAssertionCalls[idx]?.[0];
}
if ("hash" in expected) {
call.hash = hashAssertionCalls[idx]?.[0];
}
if ("pathParameters" in expected) {
call.pathParameters = pathParametersAssertionCalls[idx]?.[0];
}
if ("gqlVariables" in expected) {
call.gqlVariables = gqlVariablesAssertionCalls[idx]?.[0];
}
if ("gqlQuery" in expected) {
call.gqlQuery = gqlQueryAssertionCalls[idx]?.[0];
}
return call;
});
};
const toHaveBeenNthRequestedWith = {
name: "toHaveBeenNthRequestedWith",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
if (!received.requestedAssertion) throw new Error("No assertion found");
const calls = getCalls(received, expected);
const nthCall = calls[time - 1];
const { isNot } = this;
const name = received.requestedAssertion.getMockName();
return {
pass: checkEquality(expected, nthCall),
message: () => formatMockCalls(
name,
calls.map((call) => [call]),
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with request matching ${this.utils.printExpected(JSON.stringify(expected))}, but it was requested with ${this.utils.printReceived(JSON.stringify(nthCall))}`
)
};
}
};
const toHaveBeenRequestedWith = {
name: "toHaveBeenRequestedWith",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, expected) {
checkMockedHandler(received);
if (!received.requestedAssertion) throw new Error("No assertion found");
const calls = getCalls(received, expected);
const { isNot } = this;
const name = received.requestedAssertion.getMockName();
return {
pass: calls.some((call) => checkEquality(expected, call)),
message: () => formatMockCalls(
name,
calls.map((call) => [call]),
`Expected ${name} to${isNot ? " not" : ""} have been requested with body ${this.utils.printExpected(JSON.stringify(expected))}`
)
};
}
};
const toHaveBeenNthRequestedWithBody = {
name: "toHaveBeenNthRequestedWithBody",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.bodyAssertion;
if (!assertion) throw new Error("No body assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const actualBody = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(actualBody, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with body ${this.utils.printExpected(expected)}, but it was requested with ${this.utils.printReceived(actualBody)}`
)
};
}
};
const toHaveBeenRequestedWithBody = {
name: "toHaveBeenRequestedWithBody",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const bodyAssertion = mockFn();
bodyAssertion.mockName(typeof path === "string" ? path : path.source);
const newResolver = async (info, ...args) => {
const { request } = info;
const clone = request.clone();
const payload = await clone.text();
bodyAssertion(payload);
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.bodyAssertion = bodyAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const bodyAssertion = mockFn();
bodyAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = async (info, ...args) => {
const { request } = info;
const clone = request.clone();
const payload = await clone.text();
bodyAssertion(payload);
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.bodyAssertion = bodyAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.bodyAssertion;
if (!assertion) throw new Error("No body assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${assertion?.getMockName()} to${isNot ? " not" : ""} have been requested with body ${this.utils.printExpected(expected)}`
)
};
}
};
function checkMockedGraphQLHandler(input) {
if (!(input instanceof msw.GraphQLHandler)) {
throw new Error("Expected a GraphQLHandler");
}
}
const toHaveBeenNthRequestedWithGqlQuery = {
name: "toHaveBeenNthRequestedWithGqlQuery",
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedGraphQLHandler(received);
const assertion = received.gqlQueryAssertion;
if (!assertion) throw new Error("No GraphQL query assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${assertion?.getMockName()} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with GraphQL query ${this.utils.printExpected(expected)}, but it was requested with ${this.utils.printReceived(nthCall)}`
)
};
}
};
const toHaveBeenRequestedWithGqlQuery = {
name: "toHaveBeenRequestedWithGqlQuery",
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const gqlQueryAssertion = mockFn();
gqlQueryAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const { query } = info;
gqlQueryAssertion(query);
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.gqlQueryAssertion = gqlQueryAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedGraphQLHandler(received);
const assertion = received.gqlQueryAssertion;
if (!assertion) throw new Error("No GraphQL query assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${assertion?.getMockName()} to${isNot ? " not" : ""} have been requested with GraphQL query ${this.utils.printExpected(expected)}`
)
};
}
};
const toHaveBeenNthRequestedWithGqlVariables = {
name: "toHaveBeenNthRequestedWithGqlVariables",
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedGraphQLHandler(received);
const assertion = received.gqlVariablesAssertion;
if (!assertion) throw new Error("No GraphQL variables assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1];
const isMatch = checkEquality(nthCall?.[0], expected);
const { isNot } = this;
return {
pass: isMatch,
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with GraphQL variables ${this.utils.printExpected(JSON.stringify(expected))}, but it was requested with ${this.utils.printReceived(JSON.stringify(nthCall?.[0]))}`
)
};
}
};
const toHaveBeenRequestedWithGqlVariables = {
name: "toHaveBeenRequestedWithGqlVariables",
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const gqlVariablesAssertion = mockFn();
gqlVariablesAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const { variables } = info;
gqlVariablesAssertion(variables);
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.gqlVariablesAssertion = gqlVariablesAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedGraphQLHandler(received);
const assertion = received.gqlVariablesAssertion;
if (!assertion) throw new Error("No GraphQL variables assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${received.gqlVariablesAssertion?.getMockName()} to${isNot ? " not" : ""} have been requested with GraphQL variables ${this.utils.printExpected(JSON.stringify(expected))}`
)
};
}
};
const toHaveBeenNthRequestedWithHash = {
name: "toHaveBeenNthRequestedWithHash",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.hashAssertion;
if (!assertion) throw new Error("No hash assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with hash ${this.utils.printExpected(expected)}, but it was requested with ${this.utils.printReceived(nthCall)}`
)
};
}
};
const toHaveBeenRequestedWithHash = {
name: "toHaveBeenRequestedWithHash",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const hashAssertion = mockFn();
hashAssertion.mockName(typeof path === "string" ? path : path.source);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
const hash = new URL(clone.url).hash;
hashAssertion(hash);
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.hashAssertion = hashAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const hashAssertion = mockFn();
hashAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
const hash = new URL(clone.url).hash;
hashAssertion(hash);
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.hashAssertion = hashAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.hashAssertion;
if (!assertion) throw new Error("No hash assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested with hash ${this.utils.printExpected(expected)}`
)
};
}
};
const toHaveBeenNthRequestedWithHeaders = {
name: "toHaveBeenNthRequestedWithHeaders",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.headersAssertion;
if (!assertion) throw new Error("No headers assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with headers ${this.utils.printExpected(JSON.stringify(expected))}, but it was requested with ${this.utils.printReceived(JSON.stringify(nthCall))}`
)
};
}
};
const toHaveBeenRequestedWithHeaders = {
name: "toHaveBeenRequestedWithHeaders",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const headersAssertion = mockFn();
headersAssertion.mockName(typeof path === "string" ? path : path.source);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
headersAssertion(Object.fromEntries(clone.headers.entries()));
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.headersAssertion = headersAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const headersAssertion = mockFn();
headersAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
headersAssertion(Object.fromEntries(clone.headers.entries()));
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.headersAssertion = headersAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.headersAssertion;
if (!assertion) throw new Error("No headers assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested with headers ${this.utils.printExpected(JSON.stringify(expected))}`
)
};
}
};
const toHaveBeenNthRequestedWithJsonBody = {
name: "toHaveBeenNthRequestedWithJsonBody",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.jsonBodyAssertion;
if (!assertion) throw new Error("No JSON body assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with JSON body ${this.utils.printExpected(JSON.stringify(expected))}, but it was requested with ${this.utils.printReceived(JSON.stringify(nthCall))}`
)
};
}
};
const toHaveBeenRequestedWithJsonBody = {
name: "toHaveBeenRequestedWithJsonBody",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const jsonBodyAssertion = mockFn();
jsonBodyAssertion.mockName(typeof path === "string" ? path : path.source);
const newResolver = async (info, ...args) => {
const { request } = info;
const clone = request.clone();
try {
const payload = await clone.json();
jsonBodyAssertion(payload);
} catch {
jsonBodyAssertion(void 0);
}
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.jsonBodyAssertion = jsonBodyAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const jsonBodyAssertion = mockFn();
jsonBodyAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = async (info, ...args) => {
const { request } = info;
const clone = request.clone();
try {
const payload = await clone.json();
jsonBodyAssertion(payload);
} catch {
jsonBodyAssertion(void 0);
}
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.jsonBodyAssertion = jsonBodyAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.jsonBodyAssertion;
if (!assertion) throw new Error("No JSON body assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested with JSON body ${this.utils.printExpected(JSON.stringify(expected))}`
)
};
}
};
const toHaveBeenNthRequestedWithPathParameters = {
name: "toHaveBeenNthRequestedWithPathParameters",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.pathParametersAssertion;
if (!assertion) throw new Error("No path parameters assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with path parameters ${this.utils.printExpected(JSON.stringify(expected))}, but it was requested with ${this.utils.printReceived(JSON.stringify(nthCall))}`
)
};
}
};
const toHaveBeenRequestedWithPathParameters = {
name: "toHaveBeenRequestedWithPathParameters",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const pathParametersAssertion = mockFn();
pathParametersAssertion.mockName(
typeof path === "string" ? path : path.source
);
const newResolver = (info, ...args) => {
const { params } = info;
pathParametersAssertion(JSON.parse(JSON.stringify(params)));
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.pathParametersAssertion = pathParametersAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const pathParametersAssertion = mockFn();
pathParametersAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const params = "params" in info ? info.params : {};
pathParametersAssertion(JSON.parse(JSON.stringify(params)));
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.pathParametersAssertion = pathParametersAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.pathParametersAssertion;
if (!assertion) throw new Error("No path parameters assertion found");
const name = assertion.getMockName();
const calls = assertion?.mock.calls || [];
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested with path parameters ${this.utils.printExpected(JSON.stringify(expected))}`
)
};
}
};
const toHaveBeenNthRequestedWithQueryString = {
name: "toHaveBeenNthRequestedWithQueryString",
interceptHttp: (_mockFn, original) => original,
interceptGql: (_mockFn, original) => original,
assert: function(received, time, expected) {
checkMockedHandler(received);
const assertion = received.queryStringAssertion;
if (!assertion) throw new Error("No query string assertion found");
const calls = assertion.mock.calls;
const nthCall = calls[time - 1]?.[0];
const { isNot } = this;
return {
pass: checkEquality(nthCall, expected),
message: () => formatMockCalls(
assertion.getMockName(),
calls,
`Expected ${assertion.getMockName()} to${isNot ? " not" : ""} have been requested the ${ordinalOf(time)} time with query string ${this.utils.printExpected(expected)}, but it was requested with ${this.utils.printReceived(nthCall)}`
)
};
}
};
const toHaveBeenRequestedWithQueryString = {
name: "toHaveBeenRequestedWithQueryString",
interceptHttp: (mockFn, original) => (path, resolver, options, ...rest) => {
const queryStringAssertion = mockFn();
queryStringAssertion.mockName(
typeof path === "string" ? path : path.source
);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
const search = new URL(clone.url).search;
queryStringAssertion(search);
return resolver(info, ...args);
};
const handler = original(path, newResolver, options, ...rest);
handler.queryStringAssertion = queryStringAssertion;
return handler;
},
interceptGql: (mockFn, original) => (operationName, resolver, options, ...rest) => {
const queryStringAssertion = mockFn();
queryStringAssertion.mockName(
typeof operationName === "string" ? operationName : operationName.toString()
);
const newResolver = (info, ...args) => {
const { request } = info;
const clone = request.clone();
const search = new URL(clone.url).search;
queryStringAssertion(search);
return resolver(info, ...args);
};
const handler = original(operationName, newResolver, options, ...rest);
handler.queryStringAssertion = queryStringAssertion;
return handler;
},
assert: function(received, expected) {
checkMockedHandler(received);
const assertion = received.queryStringAssertion;
if (!assertion) throw new Error("No query string assertion found");
const name = assertion.getMockName();
const calls = assertion.mock.calls;
const { isNot } = this;
return {
pass: calls.some((call) => checkEquality(call[0], expected)),
message: () => formatMockCalls(
name,
calls,
`Expected ${name} to${isNot ? " not" : ""} have been requested with query string ${this.utils.printExpected(expected)}`
)
};
}
};
const graphqlOnlyAssertions = [
toHaveBeenRequestedWithGqlVariables,
toHaveBeenNthRequestedWithGqlVariables,
toHaveBeenRequestedWithGqlQuery,
toHaveBeenNthRequestedWithGqlQuery
];
const assertions = [
toHaveBeenRequested,
toHaveBeenRequestedTimes,
toHaveBeenRequestedWith,
toHaveBeenNthRequestedWith,
toHaveBeenRequestedWithBody,
toHaveBeenNthRequestedWithBody,
toHaveBeenRequestedWithHash,
toHaveBeenNthRequestedWithHash,
toHaveBeenRequestedWithHeaders,
toHaveBeenNthRequestedWithHeaders,
toHaveBeenRequestedWithJsonBody,
toHaveBeenNthRequestedWithJsonBody,
toHaveBeenRequestedWithQueryString,
toHaveBeenNthRequestedWithQueryString,
toHaveBeenRequestedWithPathParameters,
toHaveBeenNthRequestedWithPathParameters
];
const httpAssertions = [...assertions];
const graphqlAssertions = [...graphqlOnlyAssertions, ...assertions];
exports.graphqlAssertions = graphqlAssertions;
exports.httpAssertions = httpAssertions;
//# sourceMappingURL=index-OMx971oM.cjs.map
;