Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: MultiEnv switcher visible in view mode in cloud #32333

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
306 changes: 306 additions & 0 deletions app/client/src/ce/hooks/useShowEnvSwitcher.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import React from "react";
import { render } from "@testing-library/react";
import { Provider } from "react-redux";
import configureMockStore from "redux-mock-store";
// import type { AppState } from "@appsmith/reducers";
import useShowEnvSwitcher from "./useShowEnvSwitcher";

const spier = {
brayn003 marked this conversation as resolved.
Show resolved Hide resolved
useShowEnvSwitcher: useShowEnvSwitcher,
};

const useShowEnvSwitcherSpy = jest.spyOn(spier, "useShowEnvSwitcher");

const MockEl = ({ viewMode = false }: { viewMode?: boolean } = {}) => {
spier.useShowEnvSwitcher({ viewMode });
return <div />;
};

const renderMockElement = ({
store,
viewMode,
}: {
store: any;
viewMode: boolean;
}) => {
return render(
<Provider store={store}>
<MockEl viewMode={viewMode} />
</Provider>,
);
};

describe("BottomBar environment switcher", () => {
it("should render when admin in edit mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: true,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(true);
});
it("should render when dev in edit mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(true);
});
it("should render when viewer in edit mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: [],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});

it("should render when admin in preview mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: true,
},
},
editor: {
isPreviewMode: true,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});
it("should render when dev in preview mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: true,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});
it("should render when viewer in preview mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: [],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: true,
},
},
});
renderMockElement({ store, viewMode: false });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});

it("should render when admin in view mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: true,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: true });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});
it("should render when dev in view mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: ["manage:applications"],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: true });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});
it("should render when viewer in view mode", () => {
const mockStore = configureMockStore();
const store = mockStore({
ui: {
selectedWorkspace: {
workspace: {},
},
applications: {
currentApplication: {
userPermissions: [],
},
},
users: {
featureFlag: {
data: {
release_datasource_environments_enabled: false,
},
},
currentUser: {
isSuperUser: false,
},
},
editor: {
isPreviewMode: false,
},
},
});
renderMockElement({ store, viewMode: true });
expect(useShowEnvSwitcherSpy).lastReturnedWith(false);
});
});
10 changes: 8 additions & 2 deletions app/client/src/ce/hooks/useShowEnvSwitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ const useShowEnvSwitcher = ({ viewMode }: { viewMode: boolean }) => {
const isLoaded = areEnvironmentsFetched(state, workspace?.id);
const list = getEnvironmentsWithPermission(state);
const isDefault = list?.[0]?.isDefault;
return isLoaded && (list.length === 0 || (list.length === 1 && isDefault));
if (!isFeatureEnabled) {
return true;
} else {
return (
isLoaded && (list.length === 0 || (list.length === 1 && isDefault))
);
}
});

const isRampAllowed = useSelector((state) =>
showProductRamps(RAMP_NAME.MULTIPLE_ENV, true)(state),
);

if (!isFeatureEnabled && !isRampAllowed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this check here then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is another case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But !isFeatureEnabled check will already pass on top so same check won't be needed here right? Also, if it makes sense, we can actually move the entire condition to the top newly added line.

Copy link
Contributor Author

@brayn003 brayn003 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, isFeatureEnabled in line number 23 is actually getting called inside the isMultiEnvNotPresent selector function. Both of them have different scopes. It is used to make isMultiEnvNotPresent false even if multiple environments are present. Eg - cloud, where the feature flag is false but there are multiple envs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh alright. Didn't open the collapsed lines above. Thanks.

return false;
}

if (viewMode && isMultiEnvNotPresent) {
return false;
}
Expand Down
Loading