Skip to content

Commit

Permalink
Add extra test
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 committed Aug 22, 2022
1 parent 7ca79ad commit 6b53155
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { DevWorkspaceBuilder } from '../../../../store/__mocks__/devWorkspaceBui
import { Workspace, WorkspaceAdapter } from '../../../../services/workspace-adapter';
import { RunningWorkspacesExceededError } from '../../../../store/Workspaces/devWorkspaces';
import { DevWorkspace } from '../../../../services/devfileApi/devWorkspace';
import { actionCreators } from '../../../../store/Workspaces';

jest.mock('react-tooltip', () => {
return function DummyTooltip(): React.ReactElement {
Expand All @@ -46,6 +47,14 @@ const mockOnWorkspaceRestart = jest.fn();

const currentStepId = LoadingStep.INITIALIZE;

const mockStopWorkspace = jest.fn();
jest.mock('../../../../store/Workspaces');
(actionCreators.stopWorkspace as jest.Mock).mockImplementation(
(...args) =>
async () =>
mockStopWorkspace(...args),
);

describe('Workspace loader page', () => {
let steps: List<LoaderStep>;

Expand Down Expand Up @@ -175,7 +184,6 @@ describe('Workspace loader page', () => {
origin: 'https://che-host',
});
window.open = jest.fn();
window.location.href = 'asdfsadf';

const alertItem: AlertItem = {
key: 'alert-id',
Expand Down Expand Up @@ -210,6 +218,45 @@ describe('Workspace loader page', () => {
);
});

it('should close running workspace and restart when there is only one running workspace and button clicked', async () => {
createWindowMock({
href: 'https://che-host/dashboard/#/ide/user-che/golang-example',
origin: 'https://che-host',
});
window.open = jest.fn();

const alertItem: AlertItem = {
key: 'alert-id',
title:
'Failed to start the workspace golang-example, reason: You are not allowed to start more workspaces.',
variant: AlertVariant.danger,
error: new RunningWorkspacesExceededError('You are not allowed to start more workspaces.'),
};

const store = new FakeStoreBuilder()
.withDevWorkspaces({
workspaces: [startedWorkspace1, stoppedWorkspace, currentWorkspace],
})
.build();

renderComponent(
{ steps: steps.values, alertItem, workspace: new WorkspaceAdapter(currentWorkspace) },
store,
);

const alert = screen.getByTestId('action-links');
const buttons = within(alert).getAllByRole('button');
expect(buttons.length).toEqual(2);
expect(buttons[1].textContent).toEqual(
'Close running workspace (bash) and restart golang-example',
);

userEvent.click(buttons[1]);

await waitFor(() => expect(mockStopWorkspace).toHaveBeenCalled());
await waitFor(() => expect(mockOnWorkspaceRestart).toHaveBeenCalled());
});

it('should show options if workspace running limit has been reached, and there more than one running workspaces', async () => {
createWindowMock({ origin: 'https://che-host' });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class WorkspaceLoaderPage extends React.PureComponent<Props, State> {
this.handleRestart(false);
})
.catch(err => {
console.log('CATCH!!!:', common.helpers.errors.getMessage(err));
this.appAlerts.showAlert({
key: 'workspace-loader-page-' + getRandomString(4),
title: common.helpers.errors.getMessage(err),
Expand Down

0 comments on commit 6b53155

Please sign in to comment.