Tests: Start mocking API for action tests

stable/1.0.x
Alex Gleason 2020-06-09 16:47:41 -05:00
rodzic 85a56d86f3
commit 6810f8d0b6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
8 zmienionych plików z 70 dodań i 6 usunięć

Wyświetl plik

@ -196,6 +196,7 @@ module.exports = {
'webpack/**',
'app/soapbox/test_setup.js',
'app/**/__tests__/**',
'app/**/__mocks__/**',
],
},
],

Wyświetl plik

@ -0,0 +1,10 @@
import MockAdapter from 'axios-mock-adapter';
const api = jest.requireActual('../api').default;
export default (...params) => {
const axiosInstance = api(...params);
const mock = new MockAdapter(axiosInstance);
mock.onGet('/instance/about/index.html').reply(200, '<h1>Hello world</h1>');
return axiosInstance;
};

Wyświetl plik

@ -0,0 +1,25 @@
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {
FETCH_ABOUT_PAGE_REQUEST,
FETCH_ABOUT_PAGE_SUCCESS,
fetchAboutPage,
} from '../about';
import { Map as ImmutableMap } from 'immutable';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('About actions', () => {
it('creates FETCH_ABOUT_PAGE_SUCCESS when fetching about page has been done', () => {
const expectedActions = [
{ type: FETCH_ABOUT_PAGE_REQUEST, slug: 'index' },
{ type: FETCH_ABOUT_PAGE_SUCCESS, slug: 'index' },
];
const store = mockStore(ImmutableMap());
return store.dispatch(fetchAboutPage()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});

Wyświetl plik

@ -1,12 +1,15 @@
import api from '../api';
export const FETCH_ABOUT_PAGE_REQUEST = 'FETCH_ABOUT_PAGE_REQUEST';
export const FETCH_ABOUT_PAGE_SUCCESS = 'FETCH_ABOUT_PAGE_SUCCESS';
export const FETCH_ABOUT_PAGE_FAIL = 'FETCH_ABOUT_PAGE_FAIL';
export function fetchAboutPage(slug = 'index') {
return (dispatch, getState) => {
dispatch({ type: FETCH_ABOUT_PAGE_REQUEST, slug });
return api(getState).get(`/instance/about/${slug}.html`).catch(error => {
return api(getState).get(`/instance/about/${slug}.html`).then(() => {
dispatch({ type: FETCH_ABOUT_PAGE_SUCCESS, slug });
}).catch(error => {
dispatch({ type: FETCH_ABOUT_PAGE_FAIL, slug });
});
};

Wyświetl plik

@ -5,11 +5,7 @@ import LinkHeader from 'http-link-header';
export const getLinks = response => {
const value = response.headers.link;
if (!value) {
return { refs: [] };
}
if (!value) return { refs: [] };
return LinkHeader.parse(value);
};

Wyświetl plik

@ -5,3 +5,5 @@ import Adapter from 'enzyme-adapter-react-16';
const adapter = new Adapter();
configure({ adapter });
jest.mock('soapbox/api');

Wyświetl plik

@ -146,6 +146,7 @@
"websocket.js": "^0.1.12"
},
"devDependencies": {
"axios-mock-adapter": "^1.18.1",
"babel-eslint": "^10.1.0",
"babel-jest": "^24.8.0",
"enzyme": "^3.8.0",
@ -159,6 +160,7 @@
"raf": "^3.4.1",
"react-intl-translations-manager": "^5.0.3",
"react-test-renderer": "^16.7.0",
"redux-mock-store": "^1.5.4",
"sass-lint": "^1.13.1",
"webpack-dev-server": "^3.5.1",
"yargs": "^15.0.0"

Wyświetl plik

@ -2313,6 +2313,14 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==
axios-mock-adapter@^1.18.1:
version "1.18.1"
resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.18.1.tgz#a2ba2638ef513d954793f96bde3e26bd4a1b7940"
integrity sha512-kFBZsG1Ma5yxjRGHq5KuuL55mPb7WzFULhypquEhzPg8SH5CXICb+qwC2CCA5u+GQVpiqGPwKSRkd3mBCs6gdw==
dependencies:
fast-deep-equal "^3.1.1"
is-buffer "^2.0.3"
axios@^0.19.0:
version "0.19.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"
@ -6168,6 +6176,11 @@ is-buffer@^2.0.2:
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725"
integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==
is-buffer@^2.0.3:
version "2.0.4"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.4.tgz#3e572f23c8411a5cfd9557c849e3665e0b290623"
integrity sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==
is-builtin-module@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe"
@ -7371,6 +7384,11 @@ lodash.isobject@^3.0.2:
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
integrity sha1-PI+41bW/S/kK4G4U8qUwpO2TXh0=
lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=
lodash.kebabcase@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36"
@ -9847,6 +9865,13 @@ redux-immutable@^4.0.0:
resolved "https://registry.yarnpkg.com/redux-immutable/-/redux-immutable-4.0.0.tgz#3a1a32df66366462b63691f0e1dc35e472bbc9f3"
integrity sha1-Ohoy32Y2ZGK2NpHw4dw15HK7yfM=
redux-mock-store@^1.5.4:
version "1.5.4"
resolved "https://registry.yarnpkg.com/redux-mock-store/-/redux-mock-store-1.5.4.tgz#90d02495fd918ddbaa96b83aef626287c9ab5872"
integrity sha512-xmcA0O/tjCLXhh9Fuiq6pMrJCwFRaouA8436zcikdIpYWWCjU76CRk+i2bHx8EeiSiMGnB85/lZdU3wIJVXHTA==
dependencies:
lodash.isplainobject "^4.0.6"
redux-thunk@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"