Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e4f0d5e
Extract useRequest and sendRequest from SR into elasticsearch-ui-shar…
cjcenizal Jul 10, 2019
b9765d3
Simplify http service to reduce boilerplate.
cjcenizal Jul 10, 2019
8fea574
Simplify uiMetric service to reduce boilerplate.
cjcenizal Jul 10, 2019
6bf33ec
Refactor useRequest to return isInitialRequest instead of tracking po…
cjcenizal Jul 11, 2019
8acddae
Rename request callback to sendRequest.
cjcenizal Jul 11, 2019
59c2505
Refine useRequest contract.
cjcenizal Jul 11, 2019
1254d38
Add test coverage.
cjcenizal Jul 12, 2019
cd584e1
Remove setRequestInterval from return value.
cjcenizal Jul 13, 2019
4e2672b
Migrate Watcher to use shared request library.
cjcenizal Jul 13, 2019
e326cbc
Simplify request module exports.
cjcenizal Jul 13, 2019
d46df43
Merge branch 'master' into shared/use-request
cjcenizal Jul 15, 2019
3ebd0cb
Revert "Simplify uiMetric service to reduce boilerplate."
cjcenizal Jul 24, 2019
610c3d6
Revert "Simplify http service to reduce boilerplate."
cjcenizal Jul 25, 2019
b2585d0
Address PR feedback.
cjcenizal Jul 25, 2019
0074bcc
Merge branch 'master' into shared/use-request
cjcenizal Jul 25, 2019
98e0c02
Rename plugin to es_ui_shared.
cjcenizal Jul 25, 2019
017dd7d
Fix merge errors.
cjcenizal Jul 25, 2019
15a774b
Fix deserializer unit test.
cjcenizal Jul 25, 2019
02e38e5
Refator folder structure.
cjcenizal Jul 25, 2019
9cf694d
Fix SR regression caused by merge resolution.
cjcenizal Jul 25, 2019
6abb68c
Merge branch 'master' into shared/use-request
cjcenizal Jul 25, 2019
89d06f9
Merge branch 'master' into shared/use-request
cjcenizal Jul 26, 2019
68376ae
Merge branch 'master' into shared/use-request
cjcenizal Jul 29, 2019
9e91d25
Make deserializer type clearer.
cjcenizal Jul 29, 2019
fec0956
Merge branch 'master' into shared/use-request
cjcenizal Jul 29, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/plugins/es_ui_shared/public/request/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {
SendRequestConfig,
SendRequestResponse,
UseRequestConfig,
sendRequest,
useRequest,
} from './request';
247 changes: 247 additions & 0 deletions src/plugins/es_ui_shared/public/request/request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import sinon from 'sinon';
import {
sendRequest as sendRequestUnbound,
useRequest as useRequestUnbound,
} from './request';

import React from 'react';
import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme';

const TestHook = ({ callback }) => {
callback();
return null;
};

let element;

const testHook = (callback) => {
element = mount(<TestHook callback={callback} />);
};

const wait = async wait =>
new Promise(resolve => setTimeout(resolve, wait || 1));

describe('request lib', () => {
const successRequest = { path: '/success', method: 'post', body: {} };
const errorRequest = { path: '/error', method: 'post', body: {} };
const successResponse = { statusCode: 200, data: { message: 'Success message' } };
const errorResponse = { statusCode: 400, statusText: 'Error message' };

let sendPost;
let sendRequest;
let useRequest;

beforeEach(() => {
sendPost = sinon.stub();
sendPost.withArgs(successRequest.path, successRequest.body).returns(successResponse);
sendPost.withArgs(errorRequest.path, errorRequest.body).throws(errorResponse);

const httpClient = {
post: (...args) => {
return sendPost(...args);
},
};

sendRequest = sendRequestUnbound.bind(null, httpClient);
useRequest = useRequestUnbound.bind(null, httpClient);
});

describe('sendRequest function', () => {
it('uses the provided path, method, and body to send the request', async () => {
const response = await sendRequest({ ...successRequest });
sinon.assert.calledOnce(sendPost);
expect(response).toEqual({ data: successResponse.data });
});

it('surfaces errors', async () => {
try {
await sendRequest({ ...errorRequest });
} catch(e) {
sinon.assert.calledOnce(sendPost);
expect(e).toBe(errorResponse.error);
}
});
});

describe('useRequest hook', () => {
let hook;

function initUseRequest(config) {
act(() => {
testHook(() => {
hook = useRequest(config);
});
});
}

describe('parameters', () => {
describe('path, method, body', () => {
it('is used to send the request', async () => {
initUseRequest({ ...successRequest });
await wait(10);
expect(hook.data).toBe(successResponse.data);
});
});

describe('pollIntervalMs', () => {
it('sends another request after the specified time has elapsed', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 });
await wait(5);
sinon.assert.calledOnce(sendPost);

await wait(40);
sinon.assert.calledTwice(sendPost);

// We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests.
element.unmount();
});
});

describe('initialData', () => {
it('sets the initial data value', () => {
initUseRequest({ ...successRequest, initialData: 'initialData' });
expect(hook.data).toBe('initialData');
});
});

describe('deserializer', () => {
it('is called once the request resolves', async () => {
const deserializer = sinon.stub();
initUseRequest({ ...successRequest, deserializer });
sinon.assert.notCalled(deserializer);

await wait(5);
sinon.assert.calledOnce(deserializer);
sinon.assert.calledWith(deserializer, successResponse.data);
});

it('processes data', async () => {
initUseRequest({ ...successRequest, deserializer: () => 'intercepted' });
await wait(5);
expect(hook.data).toBe('intercepted');
});
});
});

describe('state', () => {
describe('isInitialRequest', () => {
it('is true for the first request and false for subsequent requests', async () => {
initUseRequest({ ...successRequest });
expect(hook.isInitialRequest).toBe(true);

hook.sendRequest();
await wait(5);
expect(hook.isInitialRequest).toBe(false);
});
});

describe('isLoading', () => {
it('represents in-flight request status', async () => {
initUseRequest({ ...successRequest });
expect(hook.isLoading).toBe(true);

await wait(5);
expect(hook.isLoading).toBe(false);
});
});

describe('error', () => {
it('surfaces errors from requests', async () => {
initUseRequest({ ...errorRequest });
await wait(10);
expect(hook.error).toBe(errorResponse);
});

it('persists while a request is in-flight', async () => {
initUseRequest({ ...errorRequest });
await wait(5);
hook.sendRequest();
expect(hook.isLoading).toBe(true);
expect(hook.error).toBe(errorResponse);
});

it('is undefined when the request is successful', async () => {
initUseRequest({ ...successRequest });
await wait(10);
expect(hook.isLoading).toBe(false);
expect(hook.error).toBeUndefined();
});
});

describe('data', () => {
it('surfaces payloads from requests', async () => {
initUseRequest({ ...successRequest });
await wait(10);
expect(hook.data).toBe(successResponse.data);
});

it('persists while a request is in-flight', async () => {
initUseRequest({ ...successRequest });
await wait(5);
hook.sendRequest();
expect(hook.isLoading).toBe(true);
expect(hook.data).toBe(successResponse.data);
});

it('is undefined when the request fails', async () => {
initUseRequest({ ...errorRequest });
await wait(10);
expect(hook.isLoading).toBe(false);
expect(hook.data).toBeUndefined();
});
});
});

describe('callbacks', () => {
describe('sendRequest', () => {
it('sends the request', () => {
initUseRequest({ ...successRequest });
sinon.assert.calledOnce(sendPost);
hook.sendRequest();
sinon.assert.calledTwice(sendPost);
});

it('resets the pollIntervalMs', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 });
await wait(5);
sinon.assert.calledOnce(sendPost);

await wait(20);
hook.sendRequest();

// If the request didn't reset the interval, there would have been three requests sent by now.
await wait(20);
sinon.assert.calledTwice(sendPost);

await wait(20);
sinon.assert.calledThrice(sendPost);

// We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests.
element.unmount();
});
});
});
});
});
Loading