Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #873 from ZupIT/issue/idm-workspaces
Browse files Browse the repository at this point in the history
Fix workspace list in external IDM flow
  • Loading branch information
monicaribeirozup committed Feb 3, 2021
2 parents f93874a + c6b4444 commit 49ae321
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import selectEvent from 'react-select-event';
import { FetchMock } from 'jest-fetch-mock';
import CreateRelease from '../index';

beforeEach(() => {
(fetch as FetchMock).resetMocks();
});

const mockGetModules = JSON.stringify({
content: [
{
Expand Down Expand Up @@ -68,8 +72,7 @@ test('form should be valid', async () => {
await act(() => userEvent.type(versionInput, 'image-1.0.0'));

await waitFor(() =>
expect(screen.getByTestId('button-default-submit')).not.toBeDisabled(),
{ timeout: 500 }
expect(screen.getByTestId('button-default-submit')).not.toBeDisabled()
);
});

Expand Down
52 changes: 34 additions & 18 deletions ui/src/modules/Workspaces/__tests__/Workspace.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,6 @@ test('render Workspace modal', async () => {
await waitFor(() => expect(screen.queryByTestId('modal-default')).not.toBeInTheDocument());
});

test('render Workspace and search', async () => {
const workspaceRequest = jest.fn();

jest.spyOn(authUtils, 'isRoot').mockImplementation(() => true);
const useWorkspaceSpy = jest.spyOn(WorkspaceHooks, 'useWorkspace')
.mockImplementation(() => [workspaceRequest, jest.fn(), false]);

render(<Workspace selectedWorkspace={jest.fn()} />);

const search = screen.getByTestId('input-text-search');

await act(() => userEvent.type(search , 'workspace'));

await waitFor(() => expect(workspaceRequest).toHaveBeenCalledTimes(2));

useWorkspaceSpy.mockRestore();
});

test('render Workspace and see a placeholder', async () => {
jest.spyOn(authUtils, 'isRoot').mockImplementation(() => true);

Expand All @@ -107,3 +89,37 @@ test('render Workspace modal and add new workspace', async () => {

expect(screen.queryByTestId('modal-default')).toBeInTheDocument();
});

test('render Workspace with isIDMAuthFlow disabled and search', async () => {
const workspaceRequest = jest.fn();

jest.spyOn(authUtils, 'isIDMAuthFlow').mockImplementation(() => false);
jest.spyOn(authUtils, 'isRoot').mockImplementation(() => true);
const useWorkspaceSpy = jest.spyOn(WorkspaceHooks, 'useWorkspace')
.mockImplementation(() => [workspaceRequest, jest.fn(), false]);

useWorkspaceSpy.mockRestore();
render(<Workspace selectedWorkspace={jest.fn()} />);

const search = screen.getByTestId('input-text-search');

await act(() => userEvent.type(search , 'workspace'));

await waitFor(() => expect(workspaceRequest).toHaveBeenCalledTimes(0));
});

test('render Workspace with isIDMAuthFlow enabled and search', async () => {
const workspaceRequest = jest.fn();

jest.spyOn(authUtils, 'isIDMAuthFlow').mockImplementation(() => true);
jest.spyOn(authUtils, 'isRoot').mockImplementation(() => true);
jest.spyOn(WorkspaceHooks, 'useWorkspace').mockImplementation(() => [workspaceRequest, jest.fn(), false]);

render(<Workspace selectedWorkspace={jest.fn()} />);

const search = screen.getByTestId('input-text-search');

await act(() => userEvent.type(search , 'workspace'));

await waitFor(() => expect(workspaceRequest).toHaveBeenCalledTimes(1));
});
18 changes: 14 additions & 4 deletions ui/src/modules/Workspaces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
* limitations under the License.
*/

import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import { getProfileByKey } from 'core/utils/profile';
import Page from 'core/components/Page';
import { useGlobalState } from 'core/state/hooks';
import Placeholder from 'core/components/Placeholder';
import { getAccessTokenDecoded, logout } from 'core/utils/auth';
import { getAccessTokenDecoded, isIDMAuthFlow, logout } from 'core/utils/auth';
import { useWorkspace } from './hooks';
import Menu from './Menu';
import { isRoot } from 'core/utils/auth';
import { findWorkspacesByUserId } from 'core/providers/users';

interface Props {
selectedWorkspace: (name: string) => void;
Expand All @@ -31,15 +32,24 @@ interface Props {
const Workspaces = ({ selectedWorkspace }: Props) => {
const { name: profileName, email } = getAccessTokenDecoded();
const workspaces = getProfileByKey('workspaces');
const userId = getProfileByKey('id');
const [filterWorkspace, , loading] = useWorkspace();
const [name, setName] = useState('');
const { list } = useGlobalState(({ workspaces }) => workspaces);

useEffect(() => {
const onIDMFlow = useCallback(() => {
if (isRoot()) {
filterWorkspace(name);
} else {
findWorkspacesByUserId(userId);
}
}, [filterWorkspace, name, userId]);

useEffect(() => {
if (isIDMAuthFlow()) {
onIDMFlow();
}
}, [name, filterWorkspace]);
}, [onIDMFlow]);

useEffect(() => {
if (!email) logout();
Expand Down

0 comments on commit 49ae321

Please sign in to comment.