UNPKG

@ng-apimock/cypress-plugin

Version:
131 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CypressPlugin = void 0; const urljoin = require("url-join"); const uuid = require("uuid"); class CypressPlugin { constructor() { this.isLogsEnabled = true; this.configuration = Object.assign({}, JSON.parse(JSON.stringify({ identifier: Cypress.env('NG_API_MOCK_BASE_IDENTIFIER') || 'apimockid', baseUrl: Cypress.env('NG_API_MOCK_BASE_URL'), basePath: Cypress.env('NG_API_MOCK_BASE_PATH') || '/ngapimock' }))); this.baseUrl = urljoin(this.configuration.baseUrl, this.configuration.basePath); if (Cypress.env('NG_API_MOCK_ENABLE_LOGS') != null) { try { this.isLogsEnabled = JSON.parse(Cypress.env('NG_API_MOCK_ENABLE_LOGS')); } catch (e) { throw new Error('Unexpected value for NG_API_MOCK_ENABLE_LOGS env var, please provide string value: `true` or `false`'); } } } createPreset(name, includeMocks, includeVariables) { return this.getMocks() .then((m) => this.getVariables() .then((v) => { const payload = { name, mocks: includeMocks ? m.state : {}, variables: includeVariables ? v.state : {} }; return this.invoke('presets', 'POST', payload) .then(cy.wrap); })); } delayResponse(name, delay) { return this.invoke('mocks', 'PUT', { name, delay }) .then(cy.wrap); } deleteVariable(key) { return this.invoke(`variables/${key}`, 'DELETE', {}) .then(cy.wrap); } echoRequest(name, echo) { return this.invoke('mocks', 'PUT', { name, echo }) .then(cy.wrap); } getMocks() { return this.invoke('mocks', 'GET', {}) .then((response) => cy.wrap(response.body)); } getPresets() { return this.invoke('presets', 'GET', {}) .then((response) => cy.wrap(response.body)); } getRecordings() { return this.invoke('recordings', 'GET', {}) .then((response) => cy.wrap(response.body)); } getVariables() { return this.invoke('variables', 'GET', {}) .then((response) => cy.wrap(response.body)); } invoke(query, method, body) { const url = urljoin(this.baseUrl, query); const requestObject = { method, url, log: this.isLogsEnabled, headers: { Cookie: `${this.configuration.identifier}=${this.ngApimockId}`, 'Content-Type': 'application/json' } }; if (['GET', 'HEAD'].indexOf(method) === -1) { requestObject.body = body; } return cy.request(requestObject) .then((response) => { if (response.status !== 200) { throw new Error(`An error occured while invoking ${url} that resulted in status code ${response.status}`); } }); } recordRequests(record) { return this.invoke('actions', 'PUT', { action: 'record', record }) .then(cy.wrap); } resetMocksToDefault() { return this.invoke('actions', 'PUT', { action: 'defaults' }) .then(cy.wrap); } selectPreset(name) { return this.invoke('presets', 'PUT', { name }) .then(cy.wrap); } selectScenario(name, scenario) { return this.invoke('mocks', 'PUT', { name, scenario }) .then(cy.wrap); } setMocksToPassThrough() { return this.invoke('actions', 'PUT', { action: 'passThroughs' }) .then(cy.wrap); } setVariable(key, value) { const body = {}; body[key] = value; return this.setVariables(body); } setVariables(variables) { return this.invoke('variables', 'PUT', variables) .then(cy.wrap); } setNgApimockCookie() { return cy.getCookie(this.configuration.identifier, { log: false }) .then((cookie) => { if (cookie === null) { this.ngApimockId = uuid.v4(); cy.request(urljoin(this.baseUrl, 'init')) .then(() => cy.setCookie(this.configuration.identifier, this.ngApimockId)); } else { this.ngApimockId = cookie.value; } }) .then(() => cy.wrap(undefined, { log: false })); } } exports.CypressPlugin = CypressPlugin; //# sourceMappingURL=cypress.plugin.js.map