Skip to content

Commit 9772e7a

Browse files
committed
ASAP-1613 Add disableDateMadePublic to Available Actions
1 parent 0f9bfab commit 9772e7a

11 files changed

Lines changed: 249 additions & 135 deletions

File tree

apps/storybook/src/ResearchOutputForm.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const researchOutputFormProps: ComponentProps<typeof ResearchOutputForm> = {
1919
flowId: 'team-create-manual',
2020
availableActions: {
2121
disableImpactAndCategory: false,
22+
disableDateMadePublic: false,
2223
canSaveDraft: true,
2324
showImpactAndCategory: true,
2425
showChangelogAndVersionHistory: false,

packages/model/src/research-output.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ const isAddVersionFlow = (flow: ResearchOutputFlowDescriptor): boolean =>
620620
const isEditFlow = (flow: ResearchOutputFlowDescriptor): boolean =>
621621
flow.action === 'edit';
622622

623+
const isImportedFromManuscript = (
624+
flow: ResearchOutputFlowDescriptor,
625+
): boolean => flow.origin === 'manuscript';
626+
623627
const supportsDrafts = (flow: ResearchOutputFlowDescriptor): boolean =>
624628
flow.origin !== 'manuscript' &&
625629
flow.action !== 'add-version' &&
@@ -642,6 +646,7 @@ const publishesOnSave = (flow: ResearchOutputFlowDescriptor): boolean =>
642646
export type ResearchOutputFlowBehavior = {
643647
isAddVersionFlow: boolean;
644648
isEditFlow: boolean;
649+
isImportedFromManuscript: boolean;
645650
supportsDrafts: boolean;
646651
requiresAddVersionConfirm: boolean;
647652
requiresPublishConfirm: boolean;
@@ -656,6 +661,7 @@ export const getResearchOutputFlowBehavior = (
656661
return {
657662
isAddVersionFlow: isAddVersionFlow(flow),
658663
isEditFlow: isEditFlow(flow),
664+
isImportedFromManuscript: isImportedFromManuscript(flow),
659665
supportsDrafts: supportsDrafts(flow),
660666
requiresAddVersionConfirm: requiresAddVersionConfirm(flow),
661667
requiresPublishConfirm: requiresPublishConfirm(flow),

packages/model/test/research-output.test.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,26 @@ describe('convertDecisionToBoolean', () => {
100100

101101
describe('getResearchOutputFlowBehavior', () => {
102102
test.each`
103-
flowId | isAddVersionFlow | isEditFlow | supportsDrafts | requiresAddVersionConfirm | requiresPublishConfirm | requiresSameDescriptionConfirm | publishesOnSave
104-
${'team-create-manual'} | ${false} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
105-
${'team-create-imported-from-manuscript'} | ${false} | ${false} | ${false} | ${false} | ${true} | ${false} | ${true}
106-
${'team-edit-draft'} | ${false} | ${true} | ${true} | ${false} | ${true} | ${false} | ${true}
107-
${'team-edit-published'} | ${false} | ${true} | ${false} | ${false} | ${false} | ${false} | ${false}
108-
${'team-add-version'} | ${true} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true}
109-
${'team-add-version-from-manuscript'} | ${true} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true}
110-
${'team-duplicate'} | ${false} | ${false} | ${true} | ${false} | ${true} | ${true} | ${true}
111-
${'working-group-create'} | ${false} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
112-
${'working-group-edit-draft'} | ${false} | ${true} | ${true} | ${false} | ${true} | ${false} | ${true}
113-
${'working-group-edit-published'} | ${false} | ${true} | ${false} | ${false} | ${false} | ${false} | ${false}
114-
${'working-group-add-version'} | ${true} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true}
115-
${'working-group-duplicate'} | ${false} | ${false} | ${true} | ${false} | ${true} | ${true} | ${true}
103+
flowId | isAddVersionFlow | isEditFlow | isImportedFromManuscript | supportsDrafts | requiresAddVersionConfirm | requiresPublishConfirm | requiresSameDescriptionConfirm | publishesOnSave
104+
${'team-create-manual'} | ${false} | ${false} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
105+
${'team-create-imported-from-manuscript'} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true} | ${false} | ${true}
106+
${'team-edit-draft'} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
107+
${'team-edit-published'} | ${false} | ${true} | ${false} | ${false} | ${false} | ${false} | ${false} | ${false}
108+
${'team-add-version'} | ${true} | ${false} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true}
109+
${'team-add-version-from-manuscript'} | ${true} | ${false} | ${true} | ${false} | ${true} | ${false} | ${false} | ${true}
110+
${'team-duplicate'} | ${false} | ${false} | ${false} | ${true} | ${false} | ${true} | ${true} | ${true}
111+
${'working-group-create'} | ${false} | ${false} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
112+
${'working-group-edit-draft'} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true} | ${false} | ${true}
113+
${'working-group-edit-published'} | ${false} | ${true} | ${false} | ${false} | ${false} | ${false} | ${false} | ${false}
114+
${'working-group-add-version'} | ${true} | ${false} | ${false} | ${false} | ${true} | ${false} | ${false} | ${true}
115+
${'working-group-duplicate'} | ${false} | ${false} | ${false} | ${true} | ${false} | ${true} | ${true} | ${true}
116116
`(
117-
'$flowId is add version flow: $isAddVersionFlow, is edit flow: $isEditFlow, supports drafts: $supportsDrafts, requires add version confirm: $requiresAddVersionConfirm, requires publish confirm: $requiresPublishConfirm, requires same description confirm: $requiresSameDescriptionConfirm, publishes on save: $publishesOnSave',
117+
'$flowId is add version flow: $isAddVersionFlow, is edit flow: $isEditFlow, is imported from manuscript: $isImportedFromManuscript, supports drafts: $supportsDrafts, requires add version confirm: $requiresAddVersionConfirm, requires publish confirm: $requiresPublishConfirm, requires same description confirm: $requiresSameDescriptionConfirm, publishes on save: $publishesOnSave',
118118
({
119119
flowId,
120120
isAddVersionFlow,
121121
isEditFlow,
122+
isImportedFromManuscript,
122123
supportsDrafts,
123124
requiresAddVersionConfirm,
124125
requiresPublishConfirm,
@@ -128,6 +129,7 @@ describe('getResearchOutputFlowBehavior', () => {
128129
expect(getResearchOutputFlowBehavior(flowId)).toEqual({
129130
isAddVersionFlow,
130131
isEditFlow,
132+
isImportedFromManuscript,
131133
supportsDrafts,
132134
requiresAddVersionConfirm,
133135
requiresPublishConfirm,

packages/react-components/src/organisms/ResearchOutputPublishingCard.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '../molecules';
1212
import { noop } from '../utils';
1313

14-
type ResearchOutputFormSharingCardProps = Pick<
14+
type ResearchOutputPublishingCardProps = Pick<
1515
ResearchOutputPostRequest,
1616
'sharingStatus'
1717
> & {
@@ -26,6 +26,7 @@ type ResearchOutputFormSharingCardProps = Pick<
2626
publishDate?: Date;
2727
isCreatingOutputRoute?: boolean;
2828
isImportedFromManuscript?: boolean;
29+
disableDateMadePublic?: boolean;
2930
};
3031

3132
export const getPublishDateValidationMessage = (e: ValidityState): string => {
@@ -38,8 +39,8 @@ export const getPublishDateValidationMessage = (e: ValidityState): string => {
3839
return 'Publish date cannot be greater than today';
3940
};
4041

41-
const ResearchOutputFormSharingCard: React.FC<
42-
ResearchOutputFormSharingCardProps
42+
const ResearchOutputPublishingCard: React.FC<
43+
ResearchOutputPublishingCardProps
4344
> = ({
4445
researchOutputData,
4546
documentType,
@@ -49,6 +50,7 @@ const ResearchOutputFormSharingCard: React.FC<
4950
sharingStatus,
5051
publishDate,
5152
isImportedFromManuscript,
53+
disableDateMadePublic,
5254
onChangeAsapFunded = noop,
5355
onChangeUsedInPublication = noop,
5456
onChangeSharingStatus = noop,
@@ -117,12 +119,7 @@ const ResearchOutputFormSharingCard: React.FC<
117119
subtitle={'(required)'}
118120
description={'The date this output first became publicly available.'}
119121
required
120-
enabled={
121-
!(
122-
researchOutputData?.publishDate &&
123-
(isImportedFromManuscript || researchOutputData?.id)
124-
)
125-
}
122+
enabled={!disableDateMadePublic}
126123
onChange={onChangePublishDate}
127124
value={publishDate}
128125
max={new Date()}
@@ -132,4 +129,4 @@ const ResearchOutputFormSharingCard: React.FC<
132129
</FormCard>
133130
);
134131

135-
export default ResearchOutputFormSharingCard;
132+
export default ResearchOutputPublishingCard;

packages/react-components/src/organisms/__tests__/ResearchOutputPublishingCard.test.tsx

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createResearchOutputResponse } from '@asap-hub/fixtures';
21
import { render, screen, within } from '@testing-library/react';
32
import userEvent from '@testing-library/user-event';
43
import { startOfTomorrow } from 'date-fns';
@@ -46,52 +45,26 @@ it('conditionally shows date published field', async () => {
4645
expect(screen.queryByLabelText(/date made public/i)).toBeVisible();
4746
});
4847

49-
it('enables the date made public field when creating an output', () => {
50-
render(<ResearchOutputPublishingCard {...props} sharingStatus={'Public'} />);
51-
expect(screen.getByLabelText(/date made public/i)).toBeEnabled();
52-
});
53-
54-
it('disables the date made public field when editing an output that already has a date', () => {
55-
render(
56-
<ResearchOutputPublishingCard
57-
{...props}
58-
sharingStatus={'Public'}
59-
researchOutputData={{
60-
...createResearchOutputResponse(),
61-
publishDate: '2022-03-24',
62-
}}
63-
/>,
64-
);
65-
expect(screen.getByLabelText(/date made public/i)).toBeDisabled();
66-
});
67-
68-
it('enables the date made public field when editing an output without a date', () => {
48+
it('enables the date made public field when disableDateMadePublic is false', () => {
6949
render(
7050
<ResearchOutputPublishingCard
7151
{...props}
7252
sharingStatus={'Public'}
73-
researchOutputData={{
74-
...createResearchOutputResponse(),
75-
publishDate: undefined,
76-
}}
53+
disableDateMadePublic={false}
7754
/>,
7855
);
7956
expect(screen.getByLabelText(/date made public/i)).toBeEnabled();
8057
});
8158

82-
it('enables the date made public field when duplicating an output that has a date', () => {
59+
it('disables the date made public field when disableDateMadePublic is true', () => {
8360
render(
8461
<ResearchOutputPublishingCard
8562
{...props}
8663
sharingStatus={'Public'}
87-
researchOutputData={{
88-
...createResearchOutputResponse(),
89-
id: '',
90-
publishDate: '2022-03-24',
91-
}}
64+
disableDateMadePublic={true}
9265
/>,
9366
);
94-
expect(screen.getByLabelText(/date made public/i)).toBeEnabled();
67+
expect(screen.getByLabelText(/date made public/i)).toBeDisabled();
9568
});
9669

9770
it('triggers an on change for date published', async () => {
@@ -167,33 +140,6 @@ describe('getPublishDateValidationMessage returns', () => {
167140
});
168141
});
169142

170-
it('disables the date field when imported from a manuscript with a date', () => {
171-
render(
172-
<ResearchOutputPublishingCard
173-
{...props}
174-
sharingStatus={'Public'}
175-
isImportedFromManuscript
176-
researchOutputData={createResearchOutputResponse()}
177-
/>,
178-
);
179-
expect(screen.getByLabelText(/date made public/i)).toBeDisabled();
180-
});
181-
182-
it('enables the date field when imported from a manuscript without a date', () => {
183-
render(
184-
<ResearchOutputPublishingCard
185-
{...props}
186-
sharingStatus={'Public'}
187-
isImportedFromManuscript
188-
researchOutputData={{
189-
...createResearchOutputResponse(),
190-
publishDate: undefined,
191-
}}
192-
/>,
193-
);
194-
expect(screen.getByLabelText(/date made public/i)).toBeEnabled();
195-
});
196-
197143
it('prompts for the date when editing a public output that has no date', async () => {
198144
// Suppress act() warnings from TextField's internal async validation state updates
199145
const consoleMock = mockActErrorsInConsole();

packages/react-components/src/templates/ResearchOutputForm.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ const ResearchOutputForm: React.FC<ResearchOutputFormProps> = ({
583583
setPublishDate(date ? new Date(date) : undefined)
584584
}
585585
isImportedFromManuscript={isImportedFromManuscript}
586+
disableDateMadePublic={availableActions.disableDateMadePublic}
586587
/>
587588
<ResearchOutputExtraInformationCard
588589
documentType={documentType}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { ResearchOutputResponse } from '@asap-hub/model';
2+
import { fireEvent, render, screen, within } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import { MemoryRouter } from 'react-router';
5+
6+
import ResearchOutputForm from '../../ResearchOutputForm';
7+
import {
8+
getDefaultProps,
9+
initialResearchOutputData,
10+
renderPrefilledForm,
11+
submitForm,
12+
} from '../../test-utils/research-output-form';
13+
import { mockActErrorsInConsole } from '../../../test-utils';
14+
15+
describe('date made public field', () => {
16+
let researchOutputFormProps: ReturnType<typeof getDefaultProps>;
17+
18+
beforeEach(() => {
19+
jest.spyOn(console, 'error').mockImplementation();
20+
researchOutputFormProps = getDefaultProps();
21+
});
22+
23+
afterEach(() => {
24+
jest.resetAllMocks();
25+
});
26+
27+
it('should disable the date made public field when available action disableDateMadePublic is true', () => {
28+
const { rerender } = render(
29+
<MemoryRouter>
30+
<ResearchOutputForm
31+
{...researchOutputFormProps}
32+
researchOutputData={initialResearchOutputData}
33+
availableActions={{
34+
...researchOutputFormProps.availableActions,
35+
disableDateMadePublic: true,
36+
}}
37+
/>
38+
</MemoryRouter>,
39+
);
40+
41+
expect(screen.getByLabelText(/date made public/i)).toBeDisabled();
42+
43+
rerender(
44+
<MemoryRouter>
45+
<ResearchOutputForm
46+
{...researchOutputFormProps}
47+
researchOutputData={initialResearchOutputData}
48+
availableActions={{
49+
...researchOutputFormProps.availableActions,
50+
disableDateMadePublic: false,
51+
}}
52+
/>
53+
</MemoryRouter>,
54+
);
55+
56+
expect(screen.getByLabelText(/date made public/i)).toBeEnabled();
57+
});
58+
});
59+
60+
describe('submitting the date made public', () => {
61+
const saveFn = jest.fn();
62+
let consoleMock: ReturnType<typeof mockActErrorsInConsole>;
63+
64+
beforeEach(() => {
65+
saveFn.mockResolvedValue({ id: '42' } as ResearchOutputResponse);
66+
consoleMock = mockActErrorsInConsole();
67+
});
68+
69+
afterEach(() => {
70+
consoleMock.mockRestore();
71+
jest.resetAllMocks();
72+
});
73+
74+
it('can submit published date', async () => {
75+
renderPrefilledForm({ onSave: saveFn });
76+
77+
const sharingStatus = screen.getByRole('group', {
78+
name: /sharing status/i,
79+
});
80+
await userEvent.click(
81+
within(sharingStatus).getByRole('radio', { name: 'Public' }),
82+
);
83+
fireEvent.change(screen.getByLabelText(/date made public/i), {
84+
target: { value: '2022-03-24' },
85+
});
86+
87+
await submitForm();
88+
89+
expect(saveFn).toHaveBeenLastCalledWith(
90+
expect.objectContaining({
91+
sharingStatus: 'Public',
92+
publishDate: new Date('2022-03-24').toISOString(),
93+
}),
94+
);
95+
});
96+
});

0 commit comments

Comments
 (0)