2018-09-10 17:00:53 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import configureMockStore from "redux-mock-store";
|
|
|
|
|
import thunk from "redux-thunk";
|
|
|
|
|
import fetchMock from "fetch-mock";
|
|
|
|
|
import {
|
2018-09-14 16:15:13 +02:00
|
|
|
FETCH_CHANGESETS,
|
|
|
|
|
FETCH_CHANGESETS_FAILURE,
|
2018-09-10 17:00:53 +02:00
|
|
|
FETCH_CHANGESETS_PENDING,
|
|
|
|
|
FETCH_CHANGESETS_SUCCESS,
|
2018-09-19 13:49:04 +02:00
|
|
|
fetchChangesets,
|
|
|
|
|
fetchChangesetsByBranchAndPage,
|
2018-09-14 16:15:13 +02:00
|
|
|
fetchChangesetsByNamespaceNameAndBranch,
|
2018-09-19 13:49:04 +02:00
|
|
|
fetchChangesetsByPage,
|
2018-09-14 16:15:13 +02:00
|
|
|
fetchChangesetsSuccess,
|
|
|
|
|
getChangesets,
|
|
|
|
|
getFetchChangesetsFailure,
|
|
|
|
|
isFetchChangesetsPending
|
2018-09-10 17:00:53 +02:00
|
|
|
} from "./changesets";
|
|
|
|
|
import reducer from "./changesets";
|
|
|
|
|
|
2018-09-18 16:30:37 +02:00
|
|
|
const changesets = {};
|
2018-09-10 17:00:53 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
describe("changesets", () => {
|
|
|
|
|
describe("fetching of changesets", () => {
|
|
|
|
|
const DEFAULT_BRANCH_URL = "/api/rest/v2/repositories/foo/bar/changesets";
|
2018-09-19 13:49:04 +02:00
|
|
|
const SPECIFIC_BRANCH_URL =
|
|
|
|
|
"/api/rest/v2/repositories/foo/bar/branches/specific/changesets";
|
2018-09-14 16:15:13 +02:00
|
|
|
const mockStore = configureMockStore([thunk]);
|
2018-09-10 17:00:53 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.reset();
|
|
|
|
|
fetchMock.restore();
|
|
|
|
|
});
|
2018-09-10 17:00:53 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should fetch changesets for default branch", () => {
|
|
|
|
|
fetchMock.getOnce(DEFAULT_BRANCH_URL, "{}");
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
2018-09-19 13:49:04 +02:00
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: "foo/bar",
|
2018-09-14 16:15:13 +02:00
|
|
|
itemId: "foo/bar"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_SUCCESS,
|
2018-09-18 16:30:37 +02:00
|
|
|
payload: changesets,
|
2018-09-14 16:15:13 +02:00
|
|
|
itemId: "foo/bar"
|
|
|
|
|
}
|
|
|
|
|
];
|
2018-09-10 17:00:53 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
const store = mockStore({});
|
2018-09-19 13:49:04 +02:00
|
|
|
return store.dispatch(fetchChangesets("foo", "bar")).then(() => {
|
2018-09-14 16:15:13 +02:00
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
2018-09-10 17:00:53 +02:00
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should fetch changesets for specific branch", () => {
|
2018-09-18 16:30:37 +02:00
|
|
|
const itemId = "foo/bar/specific";
|
2018-09-14 16:15:13 +02:00
|
|
|
fetchMock.getOnce(SPECIFIC_BRANCH_URL, "{}");
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
2018-09-19 13:49:04 +02:00
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: itemId,
|
2018-09-18 16:30:37 +02:00
|
|
|
itemId
|
2018-09-14 16:15:13 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_SUCCESS,
|
2018-09-18 16:30:37 +02:00
|
|
|
payload: changesets,
|
|
|
|
|
itemId
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
];
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
const store = mockStore({});
|
2018-09-19 13:49:04 +02:00
|
|
|
return store
|
|
|
|
|
.dispatch(
|
|
|
|
|
fetchChangesetsByNamespaceNameAndBranch("foo", "bar", "specific")
|
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
});
|
2018-09-10 17:00:53 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should fail fetching changesets on error", () => {
|
2018-09-18 16:30:37 +02:00
|
|
|
const itemId = "foo/bar";
|
2018-09-14 16:15:13 +02:00
|
|
|
fetchMock.getOnce(DEFAULT_BRANCH_URL, 500);
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
2018-09-19 13:49:04 +02:00
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: itemId,
|
2018-09-18 16:30:37 +02:00
|
|
|
itemId
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2018-09-19 13:49:04 +02:00
|
|
|
return store.dispatch(fetchChangesets("foo", "bar")).then(() => {
|
2018-09-14 16:15:13 +02:00
|
|
|
expect(store.getActions()[0]).toEqual(expectedActions[0]);
|
|
|
|
|
expect(store.getActions()[1].type).toEqual(FETCH_CHANGESETS_FAILURE);
|
|
|
|
|
expect(store.getActions()[1].payload).toBeDefined();
|
|
|
|
|
});
|
2018-09-19 13:49:04 +02:00
|
|
|
});
|
2018-09-14 16:15:13 +02:00
|
|
|
|
|
|
|
|
it("should fail fetching changesets for specific branch on error", () => {
|
2018-09-18 16:30:37 +02:00
|
|
|
const itemId = "foo/bar/specific";
|
2018-09-14 16:15:13 +02:00
|
|
|
fetchMock.getOnce(SPECIFIC_BRANCH_URL, 500);
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
2018-09-19 13:49:04 +02:00
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: itemId,
|
2018-09-18 16:30:37 +02:00
|
|
|
itemId
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
2018-09-19 13:49:04 +02:00
|
|
|
return store
|
|
|
|
|
.dispatch(
|
|
|
|
|
fetchChangesetsByNamespaceNameAndBranch("foo", "bar", "specific")
|
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
expect(store.getActions()[0]).toEqual(expectedActions[0]);
|
|
|
|
|
expect(store.getActions()[1].type).toEqual(FETCH_CHANGESETS_FAILURE);
|
|
|
|
|
expect(store.getActions()[1].payload).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should fetch changesets by page", () => {
|
|
|
|
|
fetchMock.getOnce(DEFAULT_BRANCH_URL + "?page=5", "{}");
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: "foo/bar",
|
|
|
|
|
itemId: "foo/bar"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_SUCCESS,
|
|
|
|
|
payload: changesets,
|
|
|
|
|
itemId: "foo/bar"
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store.dispatch(fetchChangesetsByPage("foo", "bar", 5)).then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
2018-09-14 16:15:13 +02:00
|
|
|
});
|
2018-09-19 13:49:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should fetch changesets by branch and page", () => {
|
|
|
|
|
fetchMock.getOnce(SPECIFIC_BRANCH_URL + "?page=5", "{}");
|
|
|
|
|
|
|
|
|
|
const expectedActions = [
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_PENDING,
|
|
|
|
|
payload: "foo/bar/specific",
|
|
|
|
|
itemId: "foo/bar/specific"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: FETCH_CHANGESETS_SUCCESS,
|
|
|
|
|
payload: changesets,
|
|
|
|
|
itemId: "foo/bar/specific"
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const store = mockStore({});
|
|
|
|
|
return store
|
|
|
|
|
.dispatch(fetchChangesetsByBranchAndPage("foo", "bar", "specific", 5))
|
|
|
|
|
.then(() => {
|
|
|
|
|
expect(store.getActions()).toEqual(expectedActions);
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-09-11 17:20:30 +02:00
|
|
|
});
|
|
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
describe("changesets reducer", () => {
|
2018-09-11 17:20:30 +02:00
|
|
|
const responseBody = {
|
2018-09-19 13:49:04 +02:00
|
|
|
page: 1,
|
|
|
|
|
pageTotal: 10,
|
|
|
|
|
_links: {},
|
2018-09-11 17:20:30 +02:00
|
|
|
_embedded: {
|
|
|
|
|
changesets: [
|
2018-09-19 13:49:04 +02:00
|
|
|
{ id: "changeset1", author: { mail: "z@phod.com", name: "zaphod" } },
|
|
|
|
|
{ id: "changeset2", description: "foo" },
|
|
|
|
|
{ id: "changeset3", description: "bar" }
|
2018-09-11 17:20:30 +02:00
|
|
|
],
|
|
|
|
|
_embedded: {
|
|
|
|
|
tags: [],
|
|
|
|
|
branches: [],
|
|
|
|
|
parents: []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should set state to received changesets", () => {
|
2018-09-19 13:49:04 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{},
|
|
|
|
|
fetchChangesetsSuccess(responseBody, "foo", "bar")
|
|
|
|
|
);
|
2018-09-14 16:15:13 +02:00
|
|
|
expect(newState).toBeDefined();
|
2018-09-19 13:49:04 +02:00
|
|
|
expect(newState.byKey["foo/bar"].byId["changeset1"].author.mail).toEqual(
|
|
|
|
|
"z@phod.com"
|
|
|
|
|
);
|
|
|
|
|
expect(newState.byKey["foo/bar"].byId["changeset2"].description).toEqual(
|
|
|
|
|
"foo"
|
|
|
|
|
);
|
|
|
|
|
expect(newState.byKey["foo/bar"].byId["changeset3"].description).toEqual(
|
|
|
|
|
"bar"
|
|
|
|
|
);
|
|
|
|
|
expect(newState.list).toEqual({
|
|
|
|
|
entry: {
|
|
|
|
|
page: 1,
|
|
|
|
|
pageTotal: 10,
|
|
|
|
|
_links: {}
|
|
|
|
|
},
|
|
|
|
|
entries: ["changeset1", "changeset2", "changeset3"]
|
|
|
|
|
});
|
2018-09-14 16:15:13 +02:00
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should not delete existing changesets from state", () => {
|
|
|
|
|
const responseBody = {
|
|
|
|
|
_embedded: {
|
|
|
|
|
changesets: [
|
2018-09-19 13:49:04 +02:00
|
|
|
{ id: "changeset1", author: { mail: "z@phod.com", name: "zaphod" } }
|
2018-09-14 16:15:13 +02:00
|
|
|
],
|
|
|
|
|
_embedded: {
|
|
|
|
|
tags: [],
|
|
|
|
|
branches: [],
|
|
|
|
|
parents: []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-09-19 13:49:04 +02:00
|
|
|
const newState = reducer(
|
|
|
|
|
{
|
|
|
|
|
byKey: {
|
|
|
|
|
"foo/bar": {
|
|
|
|
|
byId: {
|
|
|
|
|
["changeset2"]: {
|
|
|
|
|
id: "changeset2",
|
|
|
|
|
author: { mail: "mail@author.com", name: "author" }
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
2018-09-11 17:20:30 +02:00
|
|
|
}
|
2018-09-19 13:49:04 +02:00
|
|
|
},
|
|
|
|
|
fetchChangesetsSuccess(responseBody, "foo", "bar")
|
|
|
|
|
);
|
|
|
|
|
expect(newState.byKey["foo/bar"].byId["changeset2"]).toBeDefined();
|
|
|
|
|
expect(newState.byKey["foo/bar"].byId["changeset1"]).toBeDefined();
|
|
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
});
|
|
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
describe("changeset selectors", () => {
|
|
|
|
|
const error = new Error("Something went wrong");
|
|
|
|
|
|
|
|
|
|
it("should get all changesets for a given namespace and name", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
changesets: {
|
2018-09-19 13:49:04 +02:00
|
|
|
byKey: {
|
|
|
|
|
"foo/bar": {
|
|
|
|
|
byId: {
|
|
|
|
|
id1: { id: "id1" },
|
|
|
|
|
id2: { id: "id2" }
|
|
|
|
|
}
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-09-19 13:49:04 +02:00
|
|
|
const result = getChangesets(state, "foo", "bar");
|
|
|
|
|
expect(result).toContainEqual({ id: "id1" });
|
2018-09-14 16:15:13 +02:00
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should return true, when fetching changesets is pending", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
pending: {
|
|
|
|
|
[FETCH_CHANGESETS + "/foo/bar"]: true
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
expect(isFetchChangesetsPending(state, "foo", "bar")).toBeTruthy();
|
|
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should return false, when fetching changesets is not pending", () => {
|
|
|
|
|
expect(isFetchChangesetsPending({}, "foo", "bar")).toEqual(false);
|
|
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should return error if fetching changesets failed", () => {
|
|
|
|
|
const state = {
|
|
|
|
|
failure: {
|
|
|
|
|
[FETCH_CHANGESETS + "/foo/bar"]: error
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
expect(getFetchChangesetsFailure(state, "foo", "bar")).toEqual(error);
|
|
|
|
|
});
|
2018-09-12 17:15:58 +02:00
|
|
|
|
2018-09-14 16:15:13 +02:00
|
|
|
it("should return false if fetching changesets did not fail", () => {
|
|
|
|
|
expect(getFetchChangesetsFailure({}, "foo", "bar")).toBeUndefined();
|
2018-09-19 13:49:04 +02:00
|
|
|
});
|
2018-09-14 16:15:13 +02:00
|
|
|
});
|
2018-09-10 17:00:53 +02:00
|
|
|
});
|