UNPKG

@opra/testing

Version:
76 lines (75 loc) 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiExpectObject = void 0; const tslib_1 = require("tslib"); const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is")); const objects_1 = require("@jsopen/objects"); const common_1 = require("@opra/common"); const expect_1 = require("expect"); const api_expect_base_js_1 = require("./api-expect-base.js"); class ApiExpectObject extends api_expect_base_js_1.ApiExpectBase { get not() { return new ApiExpectObject(this.response, !this.isNot); } /** * Tests if Response payload matches given object * @param expected */ toMatch(expected) { try { expected = (0, objects_1.omitNullish)(expected); const data = type_is_1.default.is(this.response.contentType, [ common_1.MimeTypes.opra_response_json, ]) ? this.response.body.payload : this.response.body; this._expect(data).toEqual(expect_1.expect.objectContaining(expected)); } catch (e) { Error.captureStackTrace(e, this.toMatch); throw e; } return this; } /** * Tests if Response payload has all of provided fields. * @param fields */ toContainFields(fields) { try { fields = Array.isArray(fields) ? fields : [fields]; const data = type_is_1.default.is(this.response.contentType, [ common_1.MimeTypes.opra_response_json, ]) ? this.response.body.payload : this.response.body; this._expect(Object.keys(data)).toEqual(expect_1.expect.arrayContaining(fields)); } catch (e) { Error.captureStackTrace(e, this.toContainFields); throw e; } return this; } /** * Tests if Response payload only contains all of provided fields. * @param fields */ toContainAllFields(fields) { try { fields = Array.isArray(fields) ? fields : [fields]; const data = type_is_1.default.is(this.response.contentType, [ common_1.MimeTypes.opra_response_json, ]) ? this.response.body.payload : this.response.body; this._expect(Object.keys(data)).toEqual(fields); } catch (e) { Error.captureStackTrace(e, this.toContainAllFields); throw e; } return this; } } exports.ApiExpectObject = ApiExpectObject;