From 3fd88f616696cf697ec4e8ce6c1bd5cc20925442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maren=20S=C3=BCwer?= Date: Fri, 5 Oct 2018 12:11:35 +0200 Subject: [PATCH] add selectors --- scm-ui/src/modules/indexResource.js | 76 +++++++ scm-ui/src/modules/indexResource.test.js | 272 ++++++++++++++++++++++- 2 files changed, 347 insertions(+), 1 deletion(-) diff --git a/scm-ui/src/modules/indexResource.js b/scm-ui/src/modules/indexResource.js index f3f35f1ca8..03713675f2 100644 --- a/scm-ui/src/modules/indexResource.js +++ b/scm-ui/src/modules/indexResource.js @@ -3,6 +3,8 @@ import * as types from "./types"; import { apiClient } from "@scm-manager/ui-components"; import type { Action, IndexResources } from "@scm-manager/ui-types"; +import { isPending } from "./pending"; +import { getFailure } from "./failure"; // Action @@ -73,3 +75,77 @@ export default function reducer( return state; } } + +// selectors + +export function isFetchIndexResourcesPending(state: Object) { + return isPending(state, FETCH_INDEXRESOURCES); +} + +export function getFetchIndexResourcesFailure(state: Object) { + return getFailure(state, FETCH_INDEXRESOURCES); +} + +export function getUiPluginsLink(state: Object) { + return state.indexResources.links["uiPlugins"].href; +} + +export function getMeLink(state: Object) { + if (state.indexResources.links["me"]) + return state.indexResources.links["me"].href; + return undefined; +} + +export function getLogoutLink(state: Object) { + if (state.indexResources.links["logout"]) + return state.indexResources.links["logout"].href; + return undefined; +} + +export function getLoginLink(state: Object) { + if (state.indexResources.links["login"]) + return state.indexResources.links["login"].href; + return undefined; +} + +export function getUsersLink(state: Object) { + if (state.indexResources.links["users"]) + return state.indexResources.links["users"].href; + return undefined; +} + +export function getGroupsLink(state: Object) { + if (state.indexResources.links["groups"]) + return state.indexResources.links["groups"].href; + return undefined; +} + +export function getConfigLink(state: Object) { + if (state.indexResources.links["config"]) + return state.indexResources.links["config"].href; + return undefined; +} + +export function getRepositoriesLink(state: Object) { + if (state.indexResources.links["repositories"]) + return state.indexResources.links["repositories"].href; + return undefined; +} + +export function getHgConfigLink(state: Object) { + if (state.indexResources.links["hgConfig"]) + return state.indexResources.links["hgConfig"].href; + return undefined; +} + +export function getGitConfigLink(state: Object) { + if (state.indexResources.links["gitConfig"]) + return state.indexResources.links["gitConfig"].href; + return undefined; +} + +export function getSvnConfigLink(state: Object) { + if (state.indexResources.links["svnConfig"]) + return state.indexResources.links["svnConfig"].href; + return undefined; +} diff --git a/scm-ui/src/modules/indexResource.test.js b/scm-ui/src/modules/indexResource.test.js index e822c2466c..ad75c5af89 100644 --- a/scm-ui/src/modules/indexResource.test.js +++ b/scm-ui/src/modules/indexResource.test.js @@ -6,7 +6,20 @@ import reducer, { FETCH_INDEXRESOURCES_SUCCESS, FETCH_INDEXRESOURCES_FAILURE, fetchIndexResources, - fetchIndexResourcesSuccess + fetchIndexResourcesSuccess, + FETCH_INDEXRESOURCES, + isFetchIndexResourcesPending, + getFetchIndexResourcesFailure, + getUiPluginsLink, + getMeLink, + getLogoutLink, + getLoginLink, + getUsersLink, + getConfigLink, + getRepositoriesLink, + getHgConfigLink, + getGitConfigLink, + getSvnConfigLink } from "./indexResource"; const indexResourcesUnauthenticated = { @@ -144,3 +157,260 @@ describe("index resources reducer", () => { expect(newState.links).toBe(indexResourcesAuthenticated._links); }); }); + +describe("index resources selectors", () => { + const error = new Error("something goes wrong"); + + it("should return true, when fetch index resources is pending", () => { + const state = { + pending: { + [FETCH_INDEXRESOURCES]: true + } + }; + expect(isFetchIndexResourcesPending(state)).toEqual(true); + }); + + it("should return false, when fetch index resources is not pending", () => { + expect(isFetchIndexResourcesPending({})).toEqual(false); + }); + + it("should return error when fetch index resources did fail", () => { + const state = { + failure: { + [FETCH_INDEXRESOURCES]: error + } + }; + expect(getFetchIndexResourcesFailure(state)).toEqual(error); + }); + + it("should return undefined when fetch index resources did not fail", () => { + expect(getFetchIndexResourcesFailure({})).toBe(undefined); + }); + + // ui plugins link + it("should return ui plugins link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getUiPluginsLink(state)).toBe( + "http://localhost:8081/scm/api/v2/ui/plugins" + ); + }); + + it("should return ui plugins links when unauthenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getUiPluginsLink(state)).toBe( + "http://localhost:8081/scm/api/v2/ui/plugins" + ); + }); + + // me link + it("should return me link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getMeLink(state)).toBe("http://localhost:8081/scm/api/v2/me/"); + }); + + it("should return undefined for me link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getMeLink(state)).toBe(undefined); + }); + + // logout link + it("should return logout link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getLogoutLink(state)).toBe( + "http://localhost:8081/scm/api/v2/auth/access_token" + ); + }); + + it("should return undefined for logout link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getLogoutLink(state)).toBe(undefined); + }); + + // login link + it("should return login link when unauthenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getLoginLink(state)).toBe( + "http://localhost:8081/scm/api/v2/auth/access_token" + ); + }); + + it("should return undefined for login link when authenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getLoginLink(state)).toBe(undefined); + }); + + // users link + it("should return users link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getUsersLink(state)).toBe("http://localhost:8081/scm/api/v2/users/"); + }); + + it("should return undefined for users link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getUsersLink(state)).toBe(undefined); + }); + + // groups link + it("should return groups link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getUsersLink(state)).toBe("http://localhost:8081/scm/api/v2/users/"); + }); + + it("should return undefined for groups link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getUsersLink(state)).toBe(undefined); + }); + + // config link + it("should return config link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getConfigLink(state)).toBe( + "http://localhost:8081/scm/api/v2/config" + ); + }); + + it("should return undefined for config link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getConfigLink(state)).toBe(undefined); + }); + + // repositories link + it("should return repositories link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getRepositoriesLink(state)).toBe( + "http://localhost:8081/scm/api/v2/repositories/" + ); + }); + + it("should return config for repositories link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getRepositoriesLink(state)).toBe(undefined); + }); + + // hgConfig link + it("should return hgConfig link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getHgConfigLink(state)).toBe( + "http://localhost:8081/scm/api/v2/config/hg" + ); + }); + + it("should return config for hgConfig link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getHgConfigLink(state)).toBe(undefined); + }); + + // gitConfig link + it("should return gitConfig link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getGitConfigLink(state)).toBe( + "http://localhost:8081/scm/api/v2/config/git" + ); + }); + + it("should return config for gitConfig link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getGitConfigLink(state)).toBe(undefined); + }); + + // svnConfig link + it("should return svnConfig link when authenticated and has permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesAuthenticated._links + } + }; + expect(getSvnConfigLink(state)).toBe( + "http://localhost:8081/scm/api/v2/config/svn" + ); + }); + + it("should return config for svnConfig link when unauthenticated or has not permission to see it", () => { + const state = { + indexResources: { + links: indexResourcesUnauthenticated._links + } + }; + expect(getSvnConfigLink(state)).toBe(undefined); + }); +});