Skip to the main content


AjaxHelpers

These utility methods are used by Jasmine tests to create a mock server or get reference to mock requests. In either case, the cleanup (restore) is done with an after function.

This pattern is being used instead of the more common beforeEach/afterEach pattern because we were seeing sporadic failures in the afterEach restore call. The cause of the errors were that one test suite was incorrectly being linked as the parent of an unrelated test suite (causing both suites’ afterEach methods to be called). No solution for the root cause has been found, but initializing sinon and cleaning it up on a method-by-method basis seems to work. For more details, see STUD-1264.

Members:

  • XHR_READY_STATES

AjaxHelpers.fakeServer(response)

Get a reference to the mocked server, and respond to all requests with the specified response.

Parameters

response: string, the fake response.

Returns: *, The current request.

AjaxHelpers.fakeRequests()

Keep track of all requests to a fake server, and return a reference to the Array. This allows tests to respond to individual requests.

Returns: Array, An array tracking the fake requests.

AjaxHelpers.withFakeRequests(test)

Wraps a test function so that it is invoked with an additional parameter containing an array of fake HTTP requests.

Parameters

test: function, A function to be invoked with the fake requests.

Returns: function, A wrapped version of the input function.

AjaxHelpers.currentRequest(requests)

Returns the request that has not yet been responded to. If no such request is available then the current test will fail.

Parameters

requests: object, an array of fired sinon requests

Returns: *, The current request.

AjaxHelpers.expectRequest(requests, method, url, body)

Expect that a request was made as expected.

Parameters

requests: object, an array of fired sinon requests

method: string, the expected method of the request

url: string, the expected url of the request

body: string, the expected request body

AjaxHelpers.expectNoRequests(requests)

Verifies that there are no unconsumed requests.

Parameters

requests: object, an array of fired sinon requests

AjaxHelpers.expectJsonRequest(requests, method, url, jsonRequest)

Expect that a request with a JSON payload was made as expected.

Parameters

requests: object, an array of fired sinon requests

method: string, the expected method of the request

url: string, the expected url of the request

jsonRequest: object, the expected request body as an object

AjaxHelpers.expectRequestURL(requests, expectedUrl, expectedParameters)

Expect that a JSON request be made with the given URL and parameters.

Parameters

requests: object, The collected requests

expectedUrl: string, The expected URL excluding the parameters

expectedParameters: object, An object representing the URL parameters

AjaxHelpers.expectPostRequest(requests, url, body)

Intended for use with POST requests using application/x-www-form-urlencoded.

Parameters

requests: object, an array of fired sinon requests

url: string, the expected url of the request

body: string, the expected body of the request

AjaxHelpers.skipResetRequest(requests)

Verify that the HTTP request was marked as reset, and then skip it.

Note: this is typically used when code has explicitly canceled a request after it has been sent. A good example is when a user chooses to cancel a slow running search.

Parameters

requests: object, an array of fired sinon requests

AjaxHelpers.respond(requests, options)

Respond to a server request with a set of options:

  • statusCode: the status code to be returned (defaults to 200)
  • contentType: the content type of the response (defaults to ‘application/json’)
  • body: the body of the response (if JSON, it will be converted to a string)

Parameters

requests: object, an array of fired sinon requests

options: object, the options to provide to the response

AjaxHelpers.respondWithJson(requests, body)

Respond to a request with JSON.

Parameters

requests: object, an array of fired sinon requests

body: object, an object to be serialized to the response

AjaxHelpers.respondWithError(requests, statusCode, body)

Respond to a request with an error status code and a JSON payload.

Parameters

requests: object, an array of fired sinon requests

statusCode: int, the HTTP status code of the response (defaults to 500)

body: object, an object to be serialized to the response

AjaxHelpers.respondWithTextError(requests, statusCode, body)

Respond to a request with an error status code.

Parameters

requests: object, an array of fired sinon requests

statusCode: int, the HTTP status code of the response (defaults to 500)

body: object, the response body as a string

AjaxHelpers.respondWithNoContent(requests)

Respond to a request with an HTTP 204.

Parameters

requests: object, an array of fired sinon requests