本文整理匯總了TypeScript中Sinon.fakeServer類的典型用法代碼示例。如果您正苦於以下問題:TypeScript fakeServer類的具體用法?TypeScript fakeServer怎麽用?TypeScript fakeServer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了fakeServer類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: beforeEach
beforeEach(() => {
server = sinon.fakeServer.create()
server.autoRespond = true
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}']);
(global as any).XMLHttpRequest = sinon.useFakeXMLHttpRequest()
})
示例2: beforeEach
beforeEach((): void => {
fakeServer = sinon.fakeServer.create({respondImmediately: true});
registrationController["device"] = device;
deviceName = $("<input>")
.attr("id", "deviceName")
.val("new-device")
.appendTo(document.body);
});
示例3: beforeEach
beforeEach(() => {
server = fakeServer.create();
server.respondWith(
'GET',
OWM_API_STATION(),
fakeResponse(200, MockStations)
);
server.respondWith(
'GET',
OWM_API_FORECAST(lat, lon),
fakeResponse(200, MockForecasts)
);
server.respondImmediately = true;
});
示例4: beforeEach
beforeEach(() => {
server = sinon.fakeServer.create()
server.autoRespond = true
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
let dom = new JSDOM('', {
url: 'https://example.com/foo/bar.html',
contentType: 'text/html',
includeNodeLocations: true
});
(global as any).XMLHttpRequest = sinon.FakeXMLHttpRequest;
(global as any).document = dom.window.document
engine = new Liquid({
root: 'https://example.com/views/',
extname: '.html'
})
})
示例5: init
private init() {
let server = sinon.fakeServer.create({
autoRespond: true,
autoRespondAfterFn: this.autoRespondAfterFn.bind(this)
});
sinon.FakeXMLHttpRequest.useFilters = true;
sinon.FakeXMLHttpRequest.addFilter(function (method, url) {
let state = Leonardo.fetchStatesByUrlAndMethod(url, method);
return !(state && state.active);
});
sinon.FakeXMLHttpRequest.onResponseEnd = function (xhr) {
let res = xhr.response;
try {
res = JSON.parse(xhr.response);
}
catch (e) {
}
Leonardo._logRequest(xhr.method,
xhr.url, xhr.status, xhr.requestHeaders,
xhr.requestBody, xhr.getAllResponseHeaders(), res);
};
server.respondWith(function (request) {
let state = Leonardo.fetchStatesByUrlAndMethod(request.url, request.method),
activeOption = Leonardo.getActiveStateOption(state.name);
if (!!activeOption) {
const resStatus = activeOption.status;
const resData = Utils.isFunction(activeOption.data) ? activeOption.data(request) : activeOption.data;
let resHeaders = {'Content-Type': 'application/json'};
if (activeOption.headers) {
resHeaders = Utils.isFunction(activeOption.headers) ? activeOption.headers(request) : activeOption.headers;
}
request.respond(resStatus, resHeaders, JSON.stringify(resData));
Leonardo._logRequest(request.method, request.url,
resStatus, request.requestHeaders,
request.requestBody, resHeaders, resData);
} else {
console.warn('could not find a state for the following request', request);
}
});
}
示例6: beforeEach
beforeEach(() => {
server = fakeServer.create();
server.respondImmediately = true;
transport = new Transports.XHRTransport({ dsn: testDsn });
});
示例7: beforeEach
beforeEach(() => {
server.restore();
server = fakeServer.create();
});
示例8: describe
describe('API Client', () => {
let server = fakeServer.create();
beforeEach(() => {
server.restore();
server = fakeServer.create();
});
afterEach(() => {
server.restore();
});
describe('static #create', () => {
it('should create instance with baseHref and extraHeaders', () => {
const client = ApiClient.create('context', { 'X-FOO': 'test' });
const sub$ = client
.request({
method: 'GET',
url: '',
})
.subscribe();
const request = server.requests[0];
expect(request.url).toBe('context');
expect(request.requestHeaders['X-FOO']).toBe('test');
sub$.unsubscribe();
});
});
describe('#request', () => {
it('should return stream of remote data', () => {
const client = ApiClient.create('/');
const items1: any[] = [];
const items2: any[] = [];
client.request({ method: 'GET', url: '' }).subscribe(item => items1.push(item));
client.request({ method: 'GET', url: '' }).subscribe(item => items2.push(item));
expect(items1[0]).toEqual(pending);
expect(items2[0]).toEqual(pending);
const [req1, req2] = server.requests;
req1.respond(200, {}, JSON.stringify({}));
req2.respond(404, {}, '');
expect(isSuccess(items1[1])).toBeTruthy();
expect(isFailure(items2[1])).toBeTruthy();
});
});
describe('shorthands: get, post, put, remove', () => {
it('should use $request method with predefined parameters', () => {
const client = ApiClient.create('/');
const spy = jest.spyOn(client, 'request');
client.get('test', { foo: 'bar' });
expect(spy).toHaveBeenCalledWith({
method: 'GET',
url: 'test?foo=bar',
});
spy.mockReset();
client.post('test', {
foo: 'bar',
});
expect(spy).toHaveBeenCalledWith({
method: 'POST',
url: 'test',
body: {
foo: 'bar',
},
});
spy.mockReset();
client.put('test', {
foo: 'bar',
});
expect(spy).toHaveBeenCalledWith({
method: 'PUT',
url: 'test',
body: {
foo: 'bar',
},
});
spy.mockReset();
client.remove('test', {
foo: 'bar',
});
//.........這裏部分代碼省略.........
示例9: Loader
test.beforeEach(t => {
t.context.loader = new Loader();
server = sinon.fakeServer.create();
});