Unverified Commit 964d0634 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Fix: createRouterAct "reject" config (#81720)

Fixes a problem with the internal `createRouterAct` testing helper where
the `block: "reject"` option would not error correctly, leading to
potential false negatives. I think I accidentally broke this when I
added the ability to provide an array of expected responses.
parent 6f88000f
...@@ -72,6 +72,7 @@ export function createRouterAct( ...@@ -72,6 +72,7 @@ export function createRouterAct(
} }
let expectedResponses: Array<ExpectedResponseConfig> | null let expectedResponses: Array<ExpectedResponseConfig> | null
let forbiddenResponses: Array<ExpectedResponseConfig> | null = null
let shouldBlockAll = false let shouldBlockAll = false
if (config === undefined || config === null) { if (config === undefined || config === null) {
// Default. Expect at least one request, but don't assert on the response. // Default. Expect at least one request, but don't assert on the response.
...@@ -101,6 +102,7 @@ export function createRouterAct( ...@@ -101,6 +102,7 @@ export function createRouterAct(
expectedResponses = [config] expectedResponses = [config]
} else { } else {
expectedResponses = [] expectedResponses = []
forbiddenResponses = [config]
} }
} else { } else {
expectedResponses = [] expectedResponses = []
...@@ -113,6 +115,12 @@ export function createRouterAct( ...@@ -113,6 +115,12 @@ export function createRouterAct(
} }
if (item.block !== 'reject') { if (item.block !== 'reject') {
expectedResponses.push(item) expectedResponses.push(item)
} else {
if (forbiddenResponses === null) {
forbiddenResponses = [item]
} else {
forbiddenResponses.push(item)
}
} }
} }
} }
...@@ -288,14 +296,11 @@ ${fulfilled.body} ...@@ -288,14 +296,11 @@ ${fulfilled.body}
` `
throw error throw error
} }
if (expectedResponses !== null) { if (forbiddenResponses !== null) {
let alreadyMatchedByThisResponse: string | null = null for (const forbiddenResponse of forbiddenResponses) {
for (const expectedResponse of expectedResponses) { const includes = forbiddenResponse.includes
const includes = expectedResponse.includes
const block = expectedResponse.block
if (fulfilled.body.includes(includes)) { if (fulfilled.body.includes(includes)) {
if (block === 'reject') { error.message = `
error.message = `
Received a response containing an unexpected substring: Received a response containing an unexpected substring:
Rejected substring: ${includes} Rejected substring: ${includes}
...@@ -303,9 +308,16 @@ Rejected substring: ${includes} ...@@ -303,9 +308,16 @@ Rejected substring: ${includes}
Response: Response:
${fulfilled.body} ${fulfilled.body}
` `
throw error throw error
} }
}
}
if (expectedResponses !== null) {
let alreadyMatchedByThisResponse: string | null = null
for (const expectedResponse of expectedResponses) {
const includes = expectedResponse.includes
const block = expectedResponse.block
if (fulfilled.body.includes(includes)) {
// Match. Don't check yet whether the responses are received // Match. Don't check yet whether the responses are received
// in the expected order. Instead collect all the matches and // in the expected order. Instead collect all the matches and
// check at the end so we can include a diff in the // check at the end so we can include a diff in the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment