pulsar-edit / package-backend

Pulsar Server Backend for Packages
https://api.pulsar-edit.dev
MIT License
11 stars 11 forks source link

Create, and utilize HTTP Mock Object Builder Pattern for easier chaining of mocked API returns #146

Closed confused-Techie closed 1 year ago

confused-Techie commented 1 year ago

Requirements

Description of the Change

One factor holding back the, very much needed, total testing refactor, is that while all backend API calls have been moved into a singular function, chaining any type of complex requests can get complex, and become unwieldy to work with.

Since many of the integration functions that need to be properly tested can end up themselves making multiples of tests, and we need to have proper control over every return that may occur within that API return chain, there is no pretty way of creating all of these Mock API responses.

So to at least make them much more wieldy, and avoid duplicating code, this PR introduces a new testing module httpMock.helper.jest.js which exports a few functions and classes to assist with mocking API returns.

The most important the HTTP class, uses an object builder pattern to craft an object, that once parsed, mimics the API return that would be provided from an API call.

This in conjunction with the reused webRequestMock of the vcs test module, lets us provide an array of the parsed objects returned by HTTP and use them based on path as responses to API calls, to properly Mock any API return within a given chain.

Lastly, a global hashMap is used to retain every generated mock, so that it can be reused within a single files tests, to reduce duplication.