2019-10-20 18:02:52 +02:00
|
|
|
import configureMockStore from "redux-mock-store";
|
|
|
|
|
import thunk from "redux-thunk";
|
|
|
|
|
import fetchMock from "fetch-mock";
|
2019-10-19 16:38:07 +02:00
|
|
|
import reducer, {
|
|
|
|
|
FETCH_PLUGINS,
|
|
|
|
|
FETCH_PLUGINS_PENDING,
|
|
|
|
|
FETCH_PLUGINS_SUCCESS,
|
|
|
|
|
FETCH_PLUGINS_FAILURE,
|
|
|
|
|
FETCH_PLUGIN,
|
|
|
|
|
FETCH_PLUGIN_PENDING,
|
|
|
|
|
FETCH_PLUGIN_SUCCESS,
|
|
|
|
|
FETCH_PLUGIN_FAILURE,
|
|
|
|
|
fetchPluginsByLink,
|
|
|
|
|
fetchPluginsSuccess,
|
|
|
|
|
getPluginCollection,
|
|
|
|
|
isFetchPluginsPending,
|
|
|
|
|
getFetchPluginsFailure,
|
|
|
|
|
fetchPluginByLink,
|
|
|
|
|
fetchPluginByName,
|
|
|
|
|
fetchPluginSuccess,
|
|
|
|
|
getPlugin,
|
|
|
|
|
isFetchPluginPending,
|
2019-10-20 18:02:52 +02:00
|
|
|
getFetchPluginFailure
|
|
|
|
|
} from "./plugins";
|
|
|
|
|
import { Plugin, PluginCollection } from "@scm-manager/ui-types";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
const groupManagerPlugin: Plugin = {
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "scm-groupmanager-plugin",
|
|
|
|
|
bundles: ["/scm/groupmanager-plugin.bundle.js"],
|
|
|
|
|
type: "Administration",
|
|
|
|
|
version: "2.0.0-SNAPSHOT",
|
|
|
|
|
author: "Sebastian Sdorra",
|
|
|
|
|
description: "Notify a remote webserver whenever a plugin is pushed to.",
|
2019-10-19 16:38:07 +02:00
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-20 18:02:52 +02:00
|
|
|
href: "http://localhost:8081/api/v2/ui/plugins/scm-groupmanager-plugin"
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const scriptPlugin: Plugin = {
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "scm-script-plugin",
|
|
|
|
|
bundles: ["/scm/script-plugin.bundle.js"],
|
|
|
|
|
type: "Miscellaneous",
|
|
|
|
|
version: "2.0.0-SNAPSHOT",
|
|
|
|
|
author: "Sebastian Sdorra",
|
|
|
|
|
description: "Script support for scm-manager.",
|
2019-10-19 16:38:07 +02:00
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-20 18:02:52 +02:00
|
|
|
href: "http://localhost:8081/api/v2/ui/plugins/scm-script-plugin"
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const branchwpPlugin: Plugin = {
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "scm-branchwp-plugin",
|
|
|
|
|
bundles: ["/scm/branchwp-plugin.bundle.js"],
|
|
|
|
|
type: "Miscellaneous",
|
|
|
|
|
version: "2.0.0-SNAPSHOT",
|
|
|
|
|
author: "Sebastian Sdorra",
|
|
|
|
|
description: "This plugin adds branch write protection for plugins.",
|
2019-10-19 16:38:07 +02:00
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-20 18:02:52 +02:00
|
|
|
href: "http://localhost:8081/api/v2/ui/plugins/scm-branchwp-plugin"
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const pluginCollectionWithNames: PluginCollection = {
|
|
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-20 18:02:52 +02:00
|
|
|
href: "http://localhost:8081/api/v2/ui/plugins"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
_embedded: {
|
2019-10-20 18:02:52 +02:00
|
|
|
plugins: [groupManagerPlugin.name, scriptPlugin.name, branchwpPlugin.name]
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const pluginCollection: PluginCollection = {
|
|
|
|
|
_links: {
|
|
|
|
|
self: {
|
2019-10-20 18:02:52 +02:00
|
|
|
href: "http://localhost:8081/api/v2/ui/plugins"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
_embedded: {
|
2019-10-20 18:02:52 +02:00
|
|
|
plugins: [groupManagerPlugin, scriptPlugin, branchwpPlugin]
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
describe("plugins fetch", () => {
|
|
|
|
|
const URL = "ui/plugins";
|
|
|
|
|
const PLUGINS_URL = "/api/v2/ui/plugins";
|
2019-10-19 16:38:07 +02:00
|
|
|
const mockStore = configureMockStore([thunk]);
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should successfully fetch plugins from link", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchMock.getOnce(PLUGINS_URL, pluginCollection);
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
2019-10-20 18:02:52 +02:00
|
|
|
type: FETCH_PLUGINS_PENDING
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PLUGINS_SUCCESS,
|
2019-10-20 18:02:52 +02:00
|
|
|
payload: pluginCollection
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(fetchPluginsByLink(URL)).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should dispatch FETCH_PLUGINS_FAILURE if request fails", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchMock.getOnce(PLUGINS_URL, {
|
2019-10-20 18:02:52 +02:00
|
|
|
status: 500
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(fetchPluginsByLink(URL)).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(FETCH_PLUGINS_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(FETCH_PLUGINS_FAILURE);
|
|
|
|
|
expect(actions[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should successfully fetch scm-groupmanager-plugin by name", () => {
|
2019-10-21 10:57:56 +02:00
|
|
|
fetchMock.getOnce(PLUGINS_URL + "/scm-groupmanager-plugin", groupManagerPlugin);
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PLUGIN_PENDING,
|
|
|
|
|
payload: {
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "scm-groupmanager-plugin"
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: "scm-groupmanager-plugin"
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PLUGIN_SUCCESS,
|
|
|
|
|
payload: groupManagerPlugin,
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: "scm-groupmanager-plugin"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2019-10-21 10:57:56 +02:00
|
|
|
return store.dispatch(fetchPluginByName(URL, "scm-groupmanager-plugin")).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should dispatch FETCH_PLUGIN_FAILURE, if the request for scm-groupmanager-plugin by name fails", () => {
|
|
|
|
|
fetchMock.getOnce(PLUGINS_URL + "/scm-groupmanager-plugin", {
|
|
|
|
|
status: 500
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2019-10-21 10:57:56 +02:00
|
|
|
return store.dispatch(fetchPluginByName(URL, "scm-groupmanager-plugin")).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(FETCH_PLUGIN_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(FETCH_PLUGIN_FAILURE);
|
|
|
|
|
expect(actions[1].payload.name).toBe("scm-groupmanager-plugin");
|
|
|
|
|
expect(actions[1].payload.error).toBeDefined();
|
|
|
|
|
expect(actions[1].itemId).toBe("scm-groupmanager-plugin");
|
|
|
|
|
});
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should successfully fetch scm-groupmanager-plugin", () => {
|
2019-10-21 10:57:56 +02:00
|
|
|
fetchMock.getOnce("http://localhost:8081/api/v2/ui/plugins/scm-groupmanager-plugin", groupManagerPlugin);
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PLUGIN_PENDING,
|
|
|
|
|
payload: {
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "scm-groupmanager-plugin"
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: "scm-groupmanager-plugin"
|
2019-10-19 16:38:07 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_PLUGIN_SUCCESS,
|
|
|
|
|
payload: groupManagerPlugin,
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: "scm-groupmanager-plugin"
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(fetchPluginByLink(groupManagerPlugin)).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should dispatch FETCH_PLUGIN_FAILURE, it the request for scm-groupmanager-plugin fails", () => {
|
2019-10-21 10:57:56 +02:00
|
|
|
fetchMock.getOnce("http://localhost:8081/api/v2/ui/plugins/scm-groupmanager-plugin", {
|
|
|
|
|
status: 500
|
|
|
|
|
});
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(fetchPluginByLink(groupManagerPlugin)).then(() => {
|
|
|
|
|
const actions = store.getActions();
|
|
|
|
|
expect(actions[0].type).toEqual(FETCH_PLUGIN_PENDING);
|
|
|
|
|
expect(actions[1].type).toEqual(FETCH_PLUGIN_FAILURE);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(actions[1].payload.name).toBe("scm-groupmanager-plugin");
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(actions[1].payload.error).toBeDefined();
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(actions[1].itemId).toBe("scm-groupmanager-plugin");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
describe("plugins reducer", () => {
|
|
|
|
|
it("should return empty object, if state and action is undefined", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(reducer()).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return the same state, if the action is undefined", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
2019-10-20 18:02:52 +02:00
|
|
|
x: true
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
expect(reducer(state)).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return the same state, if the action is unknown to the reducer", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
2019-10-20 18:02:52 +02:00
|
|
|
x: true
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
expect(
|
|
|
|
|
reducer(state, {
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "EL_SPECIALE"
|
|
|
|
|
})
|
2019-10-19 16:38:07 +02:00
|
|
|
).toBe(state);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should store the plugins by it's type and name on FETCH_PLUGINS_SUCCESS", () => {
|
|
|
|
|
const newState = reducer({}, fetchPluginsSuccess(pluginCollection));
|
|
|
|
|
expect(newState.list._embedded.plugins).toEqual([
|
2019-10-20 18:02:52 +02:00
|
|
|
"scm-groupmanager-plugin",
|
|
|
|
|
"scm-script-plugin",
|
|
|
|
|
"scm-branchwp-plugin"
|
2019-10-19 16:38:07 +02:00
|
|
|
]);
|
2019-10-21 10:57:56 +02:00
|
|
|
expect(newState.byNames["scm-groupmanager-plugin"]).toBe(groupManagerPlugin);
|
2019-10-20 18:02:52 +02:00
|
|
|
expect(newState.byNames["scm-script-plugin"]).toBe(scriptPlugin);
|
|
|
|
|
expect(newState.byNames["scm-branchwp-plugin"]).toBe(branchwpPlugin);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should store the plugin at byNames", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const newState = reducer({}, fetchPluginSuccess(groupManagerPlugin));
|
2019-10-21 10:57:56 +02:00
|
|
|
expect(newState.byNames["scm-groupmanager-plugin"]).toBe(groupManagerPlugin);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
describe("plugins selectors", () => {
|
|
|
|
|
const error = new Error("something went wrong");
|
2019-10-19 16:38:07 +02:00
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return the plugins collection", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
plugins: {
|
|
|
|
|
list: pluginCollectionWithNames,
|
|
|
|
|
byNames: {
|
2019-10-20 18:02:52 +02:00
|
|
|
"scm-groupmanager-plugin": groupManagerPlugin,
|
|
|
|
|
"scm-script-plugin": scriptPlugin,
|
|
|
|
|
"scm-branchwp-plugin": branchwpPlugin
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const collection = getPluginCollection(state);
|
|
|
|
|
expect(collection).toEqual(pluginCollection);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return true, when fetch plugins is pending", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
pending: {
|
2019-10-20 18:02:52 +02:00
|
|
|
[FETCH_PLUGINS]: true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
expect(isFetchPluginsPending(state)).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return false, when fetch plugins is not pending", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(isFetchPluginsPending({})).toEqual(false);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return error when fetch plugins did fail", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
failure: {
|
2019-10-20 18:02:52 +02:00
|
|
|
[FETCH_PLUGINS]: error
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
expect(getFetchPluginsFailure(state)).toEqual(error);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return undefined when fetch plugins did not fail", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(getFetchPluginsFailure({})).toBe(undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return the plugin collection", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
plugins: {
|
|
|
|
|
byNames: {
|
2019-10-20 18:02:52 +02:00
|
|
|
"scm-groupmanager-plugin": groupManagerPlugin
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
const plugin = getPlugin(state, "scm-groupmanager-plugin");
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(plugin).toEqual(groupManagerPlugin);
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return true, when fetch plugin is pending", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
pending: {
|
2019-10-20 18:02:52 +02:00
|
|
|
[FETCH_PLUGIN + "/scm-groupmanager-plugin"]: true
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
2019-10-21 10:57:56 +02:00
|
|
|
expect(isFetchPluginPending(state, "scm-groupmanager-plugin")).toEqual(true);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return false, when fetch plugin is not pending", () => {
|
|
|
|
|
expect(isFetchPluginPending({}, "scm-groupmanager-plugin")).toEqual(false);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return error when fetch plugin did fail", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const state = {
|
|
|
|
|
failure: {
|
2019-10-20 18:02:52 +02:00
|
|
|
[FETCH_PLUGIN + "/scm-groupmanager-plugin"]: error
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
2019-10-21 10:57:56 +02:00
|
|
|
expect(getFetchPluginFailure(state, "scm-groupmanager-plugin")).toEqual(error);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
it("should return undefined when fetch plugin did not fail", () => {
|
2019-10-21 10:57:56 +02:00
|
|
|
expect(getFetchPluginFailure({}, "scm-groupmanager-plugin")).toBe(undefined);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|