Skip to content

Commit

Permalink
✨(front) disable classroom launch button when creating one
Browse files Browse the repository at this point in the history
When the launch button is click, we want to disable the button to
avoid multiple clicks on it.
  • Loading branch information
lunika committed Oct 11, 2023
1 parent 4f4b53e commit 9404a9d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { screen } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import fetchMock from 'fetch-mock';
import { ResponsiveContext } from 'grommet';
import { useCurrentResourceContext } from 'lib-components';
import { render } from 'lib-tests';
import { Deferred, render } from 'lib-tests';
import { Settings } from 'luxon';
import React from 'react';

Expand All @@ -21,6 +22,10 @@ jest.mock('lib-components', () => ({
useCurrentResourceContext: jest.fn(),
}));

jest.mock('components/ClassroomWidgetProvider', () => ({
ClassroomWidgetProvider: () => <p>Classroom widget provider</p>,
}));

const mockedUseCurrentResourceContext =
useCurrentResourceContext as jest.MockedFunction<
typeof useCurrentResourceContext
Expand All @@ -30,17 +35,10 @@ Settings.defaultLocale = 'en';
Settings.defaultZone = 'Europe/Paris';

describe('<DashboardClassroomForm />', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.resetAllMocks();
fetchMock.restore();
});
afterAll(() => {
jest.useRealTimers();
});

it('creates and renders a classroom form', () => {
mockedUseCurrentResourceContext.mockReturnValue([
{
Expand All @@ -60,6 +58,9 @@ describe('<DashboardClassroomForm />', () => {
expect(
screen.getByRole('tab', { name: 'Configuration' }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Launch the classroom now in BBB' }),
).toBeInTheDocument();
});

it('selects the configuration tab by default', () => {
Expand All @@ -81,7 +82,43 @@ describe('<DashboardClassroomForm />', () => {
// The configuration tab is the one that contains the widgets, so we just have to detect the
// Description widget to be sure that we're on the right one.
expect(
screen.getByRole('textbox', { name: 'Description' }),
screen.getByRole('tab', { name: 'Configuration' }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Launch the classroom now in BBB' }),
).toBeInTheDocument();
});

it('creates a classroom when clicking on the launch button', async () => {
mockedUseCurrentResourceContext.mockReturnValue([
{
permissions: {
can_access_dashboard: true,
can_update: true,
},
},
] as any);
const classroom = classroomMockFactory({ id: '1', started: false });
const deferred = new Deferred();
fetchMock.patch('/api/classrooms/1/create/', deferred.promise);
render(
<ResponsiveContext.Provider value="large">
<DashboardClassroomForm classroom={classroom} />
</ResponsiveContext.Provider>,
);

const launchClassroomButton = screen.getByRole('button', {
name: 'Launch the classroom now in BBB',
});

expect(launchClassroomButton).toBeEnabled();

await userEvent.click(launchClassroomButton);

expect(launchClassroomButton).toBeDisabled();

deferred.resolve({ message: 'it works' });

await waitFor(() => expect(launchClassroomButton).toBeEnabled());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, Button, Tab, Tabs, ThemeContext } from 'grommet';
import { normalizeColor } from 'grommet/utils';
import { theme } from 'lib-common';
import { Classroom } from 'lib-components';
import React from 'react';
import React, { useState } from 'react';
import toast from 'react-hot-toast';
import { defineMessages, useIntl } from 'react-intl';
import styled from 'styled-components';
Expand Down Expand Up @@ -42,6 +42,7 @@ interface DashboardClassroomFormProps {

const DashboardClassroomForm = ({ classroom }: DashboardClassroomFormProps) => {
const intl = useIntl();
const [isCreating, setIsCreating] = useState(false);
const extendedTheme = {
tabs: {
header: {
Expand Down Expand Up @@ -72,9 +73,11 @@ const DashboardClassroomForm = ({ classroom }: DashboardClassroomFormProps) => {
const createClassroomMutation = useCreateClassroomAction(classroom.id, {
onSuccess: (data) => {
toast.success(data.message);
setIsCreating(false);
},
onError: () => {
toast.error(intl.formatMessage(messages.createClassroomFail));
setIsCreating(false);
},
});

Expand All @@ -99,10 +102,11 @@ const DashboardClassroomForm = ({ classroom }: DashboardClassroomFormProps) => {
<Button
type="submit"
label={intl.formatMessage(messages.startClassroomLabel)}
disabled={!classroom.title}
disabled={!classroom.title || isCreating}
primary
size="small"
onClick={() => {
setIsCreating(true);
createClassroomMutation.mutate(classroom);
}}
/>
Expand Down

0 comments on commit 9404a9d

Please sign in to comment.