Skip to content

Commit 266a2da

Browse files
authored
[ENG-10364] Part 2: Add unit tests for preprints (#898)
- Ticket: [ENG-10364] - Feature flag: n/a ## Summary of Changes 1. Update unit tests for preprints stepper components.
1 parent 2ae4c46 commit 266a2da

File tree

49 files changed

+2151
-1342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2151
-1342
lines changed

setup-jest.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ jest.mock('@newrelic/browser-agent/loaders/browser-agent', () => ({
5252
stop: jest.fn(),
5353
})),
5454
}));
55+
56+
if (!globalThis.structuredClone) {
57+
Object.defineProperty(globalThis, 'structuredClone', {
58+
value: <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T,
59+
writable: true,
60+
configurable: true,
61+
});
62+
}

src/app/features/contributors/contributors.component.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ describe('Component: Contributors', () => {
3333

3434
const mockContributors: ContributorModel[] = [MOCK_CONTRIBUTOR, MOCK_CONTRIBUTOR_WITHOUT_HISTORY];
3535

36-
beforeAll(() => {
37-
if (typeof (globalThis as any).structuredClone !== 'function') {
38-
Object.defineProperty(globalThis as any, 'structuredClone', {
39-
configurable: true,
40-
writable: true,
41-
value: (o: unknown) => JSON.parse(JSON.stringify(o)),
42-
});
43-
}
44-
});
45-
4636
beforeEach(async () => {
4737
jest.useFakeTimers();
4838

src/app/features/metadata/dialogs/contributors-dialog/contributors-dialog.component.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ describe('ContributorsDialogComponent', () => {
2828

2929
const mockContributors: ContributorModel[] = [MOCK_CONTRIBUTOR];
3030

31-
beforeAll(() => {
32-
if (typeof (globalThis as any).structuredClone !== 'function') {
33-
Object.defineProperty(globalThis as any, 'structuredClone', {
34-
configurable: true,
35-
writable: true,
36-
value: (o: unknown) => JSON.parse(JSON.stringify(o)),
37-
});
38-
}
39-
});
40-
4131
beforeEach(async () => {
4232
mockCustomDialogService = CustomDialogServiceMockBuilder.create().build();
4333

src/app/features/moderation/components/moderators-list/moderators-list.component.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ describe('ModeratorsListComponent', () => {
4141

4242
const mockModerators: ModeratorModel[] = MOCK_MODERATORS;
4343

44-
beforeAll(() => {
45-
if (typeof (globalThis as any).structuredClone !== 'function') {
46-
Object.defineProperty(globalThis as any, 'structuredClone', {
47-
configurable: true,
48-
writable: true,
49-
value: (o: unknown) => JSON.parse(JSON.stringify(o)),
50-
});
51-
}
52-
});
53-
5444
beforeEach(async () => {
5545
mockActivatedRoute = ActivatedRouteMockBuilder.create()
5646
.withParams({ providerId: mockProviderId })

src/app/features/preprints/components/stepper/author-assertion-step/array-input/array-input.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
</div>
2020

2121
<section class="mt-4 flex flex-row justify-content-end">
22-
<p-button [label]="'common.buttons.addOneMore' | translate" severity="secondary" (click)="add()" />
22+
<p-button [label]="'common.buttons.addOneMore' | translate" severity="secondary" (onClick)="add()" />
2323
</section>
2424
</section>

src/app/features/preprints/components/stepper/author-assertion-step/array-input/array-input.component.spec.ts

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,55 @@ import { TextInputComponent } from '@osf/shared/components/text-input/text-input
77

88
import { ArrayInputComponent } from './array-input.component';
99

10-
import { OSFTestingModule } from '@testing/osf.testing.module';
10+
import { provideOSFCore } from '@testing/osf.testing.provider';
1111

1212
describe('ArrayInputComponent', () => {
1313
let component: ArrayInputComponent;
1414
let fixture: ComponentFixture<ArrayInputComponent>;
15-
let formArray: FormArray<FormControl>;
15+
let formArray: FormArray<FormControl<string>>;
1616

17-
beforeEach(async () => {
18-
await TestBed.configureTestingModule({
19-
imports: [ArrayInputComponent, MockComponent(TextInputComponent), OSFTestingModule],
20-
}).compileComponents();
17+
function setup(overrides?: { withValidators?: boolean; formArray?: FormArray<FormControl<string>> }) {
18+
TestBed.configureTestingModule({
19+
imports: [ArrayInputComponent, MockComponent(TextInputComponent)],
20+
providers: [provideOSFCore()],
21+
});
2122

2223
fixture = TestBed.createComponent(ArrayInputComponent);
2324
component = fixture.componentInstance;
2425

25-
formArray = new FormArray<FormControl>([new FormControl('test')]);
26-
fixture.componentRef.setInput('formArray', formArray);
26+
formArray =
27+
overrides?.formArray ?? new FormArray<FormControl<string>>([new FormControl('test', { nonNullable: true })]);
28+
fixture.componentRef.setInput('formArray', formArray as FormArray<FormControl>);
2729
fixture.componentRef.setInput('inputPlaceholder', 'Enter value');
28-
fixture.componentRef.setInput('validators', [Validators.required]);
30+
if (overrides?.withValidators ?? true) {
31+
fixture.componentRef.setInput('validators', [Validators.required]);
32+
}
2933

3034
fixture.detectChanges();
31-
});
32-
33-
it('should create', () => {
34-
expect(component).toBeTruthy();
35-
});
36-
37-
it('should have correct input values', () => {
38-
expect(component.formArray()).toBe(formArray);
39-
expect(component.inputPlaceholder()).toBe('Enter value');
40-
expect(component.validators()).toEqual([Validators.required]);
41-
});
35+
}
4236

4337
it('should add new control to form array', () => {
38+
setup();
4439
const initialLength = formArray.length;
4540

4641
component.add();
4742

4843
expect(formArray.length).toBe(initialLength + 1);
49-
expect(formArray.at(formArray.length - 1)).toBeInstanceOf(FormControl);
44+
const newControl = formArray.at(formArray.length - 1);
45+
expect(newControl.value).toBe('');
46+
expect(newControl.hasError('required')).toBe(true);
5047
});
5148

52-
it('should add control with correct validators', () => {
49+
it('should add control without validators when validators input is not set', () => {
50+
setup({ withValidators: false });
5351
component.add();
5452

5553
const newControl = formArray.at(formArray.length - 1);
56-
expect(newControl.hasError('required')).toBe(true);
54+
expect(newControl.errors).toBeNull();
5755
});
5856

5957
it('should remove control at specified index', () => {
60-
component.add();
58+
setup();
6159
component.add();
6260
const initialLength = formArray.length;
6361

@@ -67,37 +65,13 @@ describe('ArrayInputComponent', () => {
6765
});
6866

6967
it('should not remove control if only one control exists', () => {
70-
const singleControlArray = new FormArray<FormControl>([new FormControl('only')]);
71-
fixture.componentRef.setInput('formArray', singleControlArray);
72-
fixture.detectChanges();
68+
const singleControlArray = new FormArray<FormControl<string>>([new FormControl('only', { nonNullable: true })]);
69+
setup({ formArray: singleControlArray });
7370

7471
const initialLength = singleControlArray.length;
7572

7673
component.remove(0);
7774

7875
expect(singleControlArray.length).toBe(initialLength);
7976
});
80-
81-
it('should handle multiple add and remove operations', () => {
82-
const initialLength = formArray.length;
83-
84-
component.add();
85-
component.add();
86-
component.add();
87-
88-
expect(formArray.length).toBe(initialLength + 3);
89-
90-
component.remove(1);
91-
component.remove(2);
92-
93-
expect(formArray.length).toBe(initialLength + 1);
94-
});
95-
96-
it('should create controls with nonNullable true', () => {
97-
component.add();
98-
99-
const newControl = formArray.at(formArray.length - 1);
100-
expect(newControl.value).toBe('');
101-
expect(newControl.hasError('required')).toBe(true);
102-
});
10377
});

src/app/features/preprints/components/stepper/author-assertion-step/array-input/array-input.component.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import { TextInputComponent } from '@osf/shared/components/text-input/text-input
1515
changeDetection: ChangeDetectionStrategy.OnPush,
1616
})
1717
export class ArrayInputComponent {
18-
formArray = input.required<FormArray<FormControl>>();
19-
inputPlaceholder = input.required<string>();
20-
validators = input.required<ValidatorFn[]>();
18+
readonly formArray = input.required<FormArray<FormControl>>();
19+
readonly inputPlaceholder = input.required<string>();
20+
readonly validators = input<ValidatorFn[]>([]);
2121

22-
add() {
22+
add(): void {
2323
this.formArray().push(
2424
new FormControl('', {
2525
nonNullable: true,
@@ -28,9 +28,11 @@ export class ArrayInputComponent {
2828
);
2929
}
3030

31-
remove(index: number) {
32-
if (this.formArray().length > 1) {
33-
this.formArray().removeAt(index);
31+
remove(index: number): void {
32+
const formArray = this.formArray();
33+
34+
if (formArray.length > 1) {
35+
formArray.removeAt(index);
3436
}
3537
}
3638
}

src/app/features/preprints/components/stepper/author-assertion-step/author-assertions-step.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ <h2>{{ 'preprints.preprintStepper.authorAssertions.publicPreregistration.title'
224224
styleClass="w-full"
225225
[label]="'common.buttons.back' | translate"
226226
severity="info"
227-
(click)="backButtonClicked()"
227+
(onClick)="backButtonClicked()"
228228
/>
229229
<p-button
230230
class="w-6 md:w-9rem"
@@ -236,6 +236,6 @@ <h2>{{ 'preprints.preprintStepper.authorAssertions.publicPreregistration.title'
236236
tooltipPosition="top"
237237
[disabled]="authorAssertionsForm.invalid"
238238
[loading]="isUpdatingPreprint()"
239-
(click)="nextButtonClicked()"
239+
(onClick)="nextButtonClicked()"
240240
/>
241241
</section>

0 commit comments

Comments
 (0)