Skip to content

Commit

Permalink
Fix failing tests and add new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
jreyesr committed May 22, 2024
1 parent d924969 commit fbd1b82
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
21 changes: 18 additions & 3 deletions __tests__/OutputFieldsChooser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ it('notifies parent when field is added', async () => {

await user.click(getByText("Add"));

expect(onChange).toBeCalledWith([{jsonPath: "", name: ""}]);
expect(onChange).toBeCalledWith([{jsonPath: "", name: "", "context": "body"}]);
});

it('notifies parent when field is deleted', async () => {
Expand Down Expand Up @@ -63,5 +63,20 @@ it('notifies parent when field is updated', async () => {
await user.type(within(field).getByTestId("value"), "$.some.field");
await user.selectOptions(within(field).getByTestId("fieldname"), "a");

expect(onChange).toHaveBeenLastCalledWith([{"jsonPath": "$.some.field", "name": "a"}]);
});
expect(onChange).toHaveBeenLastCalledWith([{"jsonPath": "$.some.field", "name": "a", "context": "body"}]);
});

it("tracks the output's context", async () => {
const onChange = jest.fn();
const user = userEvent.setup();
const {getByText, getByTestId} = render(
<OutputFieldsChooser colNames={["a", "b"]} onChange={onChange} />,
);
await user.click(getByText("Add"));
onChange.mockClear();

const field = getByTestId("singlefield");
await user.selectOptions(within(field).getByTestId("context"), "Status code");

expect(onChange).toHaveBeenLastCalledWith([{"jsonPath": "", "name": "", "context": "statusCode"}]);
})
56 changes: 56 additions & 0 deletions __tests__/RequestSender.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as utils from '../utils';

const mockSendRequest = jest.fn();

const originalReadResponseFromFile = utils.readResponseFromFile
utils.readResponseFromFile = jest.fn()

beforeEach(() => {
utils.readResponseFromFile.mockImplementation(originalReadResponseFromFile)
})

afterEach(() => {
jest.clearAllMocks();
})

const mockContext = {
network: {
sendRequest: mockSendRequest,
},
};

it('parses outputs', async () => {
// Set up a mock response. We need to mock the actual response object,
// and also the code that reads the response's body from disk (on Insomnia,
// the response object only holds the filesystem path to the response body, plus resp. metadata)
mockSendRequest.mockReturnValue({
bytesRead: 42,
contentType: "application/json; charset=utf-8",
headers: [{name: "content-LENGTH", value: "123"}, {name: "X-Header", value: "X-Header value"}],
statusCode: 418, // I'm a teapot!
elapsedTime: 999.999,
})
// this is the actual response body, we're intercepting code that reads data from disk
jest.spyOn(utils, "readResponseFromFile").mockReturnValue('{"foo": "Bar", "baz": "quux"}')

// CSV has two rows, first one will be changed
let csvData = [{row: 1}, {row: 2}];
const setCsvData = (cb) => {csvData = cb(csvData)};

await utils.makeRequest(mockContext, /*request*/null,
/*i*/0, /*row*/csvData[0],
/*delay*/0,
/*outputConfig*/[
{name: "d", context: "body", jsonPath: "$.foo"},
{name: "e", context: "headers", jsonPath: "CONTENT-length"},
{name: "f", context: "statusCode", jsonPath: ""},
{name: "g", context: "reqTime", jsonPath: ""},
],
/*setSent*/() => {}, setCsvData
)

// Expected value: still two rows, second one must be untouched.
// First row has some columns added, one for each output field passed
// Values of output fields must be taken from the (mock) "response"
expect(csvData).toEqual([{row: 1, d: "Bar", e: "123", f: "418", g: "999.999"}, {row: 2}])
});
2 changes: 1 addition & 1 deletion components/OutputField.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function OutputField({options, name, context, jsonPath, onChange,


<select value={context} onChange={onChangeContext}>
<select value={context} onChange={onChangeContext} data-testid="context">
<option value="body">From body</option>
<option value="headers">From header</option>
<option value="statusCode">Status code</option>
Expand Down
6 changes: 4 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export async function makeRequest(context, request, i, row, delay, outputConfig,
}

console.debug("parsing response data")
responseData = JSON.parse(readResponseFromFile(response.bodyPath))
// NOTE: The exports.XYZ is required so mocks can hook this
// See https://medium.com/welldone-software/jest-how-to-mock-a-function-call-inside-a-module-21c05c57a39f
responseData = JSON.parse(exports.readResponseFromFile(response.bodyPath))
}
console.debug(responseData)

Expand All @@ -106,7 +108,7 @@ export async function makeRequest(context, request, i, row, delay, outputConfig,
out = response.elapsedTime.toString()
break
default:
console.error("Unknown outputConfig:", "name", name, "jsonPath", jsonPath, "context", ctx)
console.error("Unknown outputConfig context:", "name", name, "jsonPath", jsonPath, "context", ctx)
continue writerForLoop // Skip to next outputConfig
}
console.debug(name, "+", jsonPath, "@", ctx, "=>", out)
Expand Down

0 comments on commit fbd1b82

Please sign in to comment.