|
| 1 | +/** |
| 2 | + * Copyright 2019 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* istanbul ignore file */ |
| 18 | + |
| 19 | +/** |
| 20 | + * This module provides a set of helper methods used to reduce code duplication in the generated unit tests |
| 21 | + * for the SDKs that depend on this core package. Note that these methods are not used by the tests for this |
| 22 | + * package - they are meant to be exported and made available to dependent libraries. |
| 23 | + * |
| 24 | + * They are included in the `test` directory since they rely on `jest` globals (like `expect`) and this |
| 25 | + * requires special linting rules that should not be configured in the rest of the source code. |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * Takes the request options constructed by the SDK and checks that the `url` and `method` properties |
| 30 | + * were set to their correct values. |
| 31 | + * |
| 32 | + * @param {Object} options - the options object put together by the SDK, retrieved from the createRequest mock |
| 33 | + * @param {String} url - The URL path of the service endpoint, from the paths section of the API definition |
| 34 | + * @param {String} string - The HTTP method for the request, from the API definition |
| 35 | + * @returns {void} |
| 36 | + */ |
| 37 | +module.exports.checkUrlAndMethod = function(options, url, method) { |
| 38 | + expect(options.url).toEqual(url); |
| 39 | + expect(options.method).toEqual(method); |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call, |
| 44 | + * and checks for the expected values for `Accept` and `Content-Type`. This to verify that the SDK sets |
| 45 | + * the correct values in the code. |
| 46 | + * |
| 47 | + * @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class |
| 48 | + * @param {String} accept - the expected value for the `Accept` header |
| 49 | + * @param {String} contentType - the expected value for the `Content-Type` header |
| 50 | + * @returns {void} |
| 51 | + */ |
| 52 | +module.exports.checkMediaHeaders = function(createRequestMock, accept, contentType) { |
| 53 | + const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers; |
| 54 | + expect(headers.Accept).toEqual(accept); |
| 55 | + expect(headers['Content-Type']).toEqual(contentType); |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * Takes the mock object for the `createRequest` method, extracts the headers that were sent with the call, |
| 60 | + * and checks for the expected value for a user-defined header. This is verify that the SDK accepts header |
| 61 | + * parameters and sends them as headers in the request. |
| 62 | + * |
| 63 | + * @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class |
| 64 | + * @param {String} userHeaderName - the name of the header passed by the user, e.g. `Contained-Content-Type` |
| 65 | + * @param {String} userHeaderValue - the expected value for the header passed by the user |
| 66 | + * @returns {void} |
| 67 | + */ |
| 68 | +module.exports.checkUserHeader = function(createRequestMock, userHeaderName, userHeaderValue) { |
| 69 | + const headers = createRequestMock.mock.calls[0][0].defaultOptions.headers; |
| 70 | + expect(headers[userHeaderName]).toEqual(userHeaderValue); |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * This method simply ensures that the method executed without any issues by extracting |
| 75 | + * the argument from the mock object for the `createRequest` method and verifying that it is an object. |
| 76 | + * |
| 77 | + * @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class |
| 78 | + * @returns {void} |
| 79 | + */ |
| 80 | +module.exports.checkForSuccessfulExecution = function(createRequestMock) { |
| 81 | + const sdkParams = createRequestMock.mock.calls[0][0]; |
| 82 | + expect(typeof sdkParams).toEqual('object'); |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * This method extracts the `options` property from the object passed into `createRequest`. This property is |
| 87 | + * an object containing all of the SDK method-specific information (like `path` and `body`) used to build a request. |
| 88 | + * This method is just a convenience method for the unit tests to be able to make assertions on the items in the request. |
| 89 | + * |
| 90 | + * @param {Object} createRequestMock - the jest mock object for the `createRequest` method in the `RequestWrapper` class |
| 91 | + * @returns {void} |
| 92 | + */ |
| 93 | +module.exports.getOptions = function(createRequestMock) { |
| 94 | + return createRequestMock.mock.calls[0][0].options; |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * This method simply ensures that the SDK methods return Promises by checking for |
| 99 | + * the `then` function - common way to assess whether or not an object is a Promise. |
| 100 | + * |
| 101 | + * @param {Promise<any>} sdkPromise - the Promise returned by an SDK method |
| 102 | + * @returns {void} |
| 103 | + */ |
| 104 | +module.exports.expectToBePromise = function(sdkPromise) { |
| 105 | + expect(typeof sdkPromise.then).toBe('function'); |
| 106 | +}; |
0 commit comments