1
0
mirror of https://github.com/SomboChea/ui synced 2024-12-01 02:14:27 +07:00
verdaccio-ui/test/e2e/request.ts
Juan Picado @jotadeveloper d1b3e6e3b5
build: e2e integration with puppeteer (#192)
* build: add e2e testing scripts

* build: add e2e testing scripts

* chore: fix script

* chore: fix script

* chore: ignore e2e normal test

* chore: fix node_latest_browser

* chore: move lint to prepare

* chore: fix lint

* chore: add local theme

* fix: e2e tests
2019-10-13 10:27:01 +02:00

138 lines
3.2 KiB
TypeScript

import assert from 'assert';
import _ from 'lodash';
import request from 'request';
const requestData = Symbol('smart_request_data');
export interface RequestPromise {
status(reason: any): any;
body_ok(reason: any): any;
body_error(reason: any): any;
request(reason: any): any;
response(reason: any): any;
send(reason: any): any;
}
function injectResponse(smartObject: any, promise: Promise<any>): Promise<any> {
// $FlowFixMe
promise[requestData] = smartObject[requestData];
return promise;
}
export class PromiseAssert extends Promise<any> implements RequestPromise {
public constructor(options: any) {
super(options);
}
public status(expected: number) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function(body) {
try {
assert.equal(selfData.response.statusCode, expected);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public body_ok(expected: any) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function(body) {
try {
if (_.isRegExp(expected)) {
assert(body.ok.match(expected), "'" + body.ok + "' doesn't match " + expected);
} else {
assert.equal(body.ok, expected);
}
assert.equal(body.error, null);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public body_error(expected: any) {
// $FlowFixMe
const selfData = this[requestData];
return injectResponse(
this,
this.then(function(body) {
try {
if (_.isRegExp(expected)) {
assert(body.error.match(expected), body.error + " doesn't match " + expected);
} else {
assert.equal(body.error, expected);
}
assert.equal(body.ok, null);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public request(callback: any) {
callback(this[requestData].request);
return this;
}
public response(cb: any) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function(body) {
cb(selfData.response);
return body;
})
);
}
public send(data: any) {
this[requestData].request.end(data);
return this;
}
}
function smartRequest(options: any): Promise<any> {
const smartObject: any = {};
smartObject[requestData] = {};
smartObject[requestData].error = Error();
Error.captureStackTrace(smartObject[requestData].error, smartRequest);
const promiseResult: Promise<any> = new PromiseAssert(function(resolve, reject) {
// store request reference on symbol
smartObject[requestData].request = request(options, function(err, res, body) {
if (err) {
return reject(err);
}
// store the response on symbol
smartObject[requestData].response = res;
resolve(body);
});
});
return injectResponse(smartObject, promiseResult);
}
export default smartRequest;