forked from CenterForOpenScience/angular-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprint-withdraw-dialog.component.spec.ts
More file actions
170 lines (129 loc) · 6.14 KB
/
Copy pathpreprint-withdraw-dialog.component.spec.ts
File metadata and controls
170 lines (129 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { MockPipe, MockProvider } from 'ng-mocks';
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
import { TitleCasePipe } from '@angular/common';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { formInputLimits } from '@osf/features/preprints/constants';
import { ProviderReviewsWorkflow, ReviewsState } from '@osf/features/preprints/enums';
import { PreprintModel, PreprintProviderDetails } from '@osf/features/preprints/models';
import { PreprintWithdrawDialogComponent } from './preprint-withdraw-dialog.component';
import { PREPRINT_MOCK } from '@testing/mocks/preprint.mock';
import { PREPRINT_PROVIDER_DETAILS_MOCK } from '@testing/mocks/preprint-provider-details';
import { OSFTestingModule } from '@testing/osf.testing.module';
import { provideMockStore } from '@testing/providers/store-provider.mock';
describe('PreprintWithdrawDialogComponent', () => {
let component: PreprintWithdrawDialogComponent;
let fixture: ComponentFixture<PreprintWithdrawDialogComponent>;
let dialogRefMock: any;
let dialogConfigMock: any;
const mockProvider: PreprintProviderDetails = PREPRINT_PROVIDER_DETAILS_MOCK;
const mockPreprint: PreprintModel = PREPRINT_MOCK;
beforeEach(async () => {
dialogRefMock = {
close: jest.fn(),
};
dialogConfigMock = {
data: { provider: mockProvider, preprint: mockPreprint },
};
await TestBed.configureTestingModule({
imports: [PreprintWithdrawDialogComponent, OSFTestingModule, MockPipe(TitleCasePipe)],
providers: [
MockProvider(DynamicDialogRef, dialogRefMock),
MockProvider(DynamicDialogConfig, dialogConfigMock),
provideMockStore({
signals: [],
}),
],
}).compileComponents();
fixture = TestBed.createComponent(PreprintWithdrawDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should set modal explanation on init', () => {
expect(component.modalExplanation()).toBeDefined();
expect(typeof component.modalExplanation()).toBe('string');
});
it('should handle form validation correctly', () => {
const formControl = component.withdrawalJustificationFormControl;
formControl.setValue('');
expect(formControl.invalid).toBe(true);
const minLength = formInputLimits.withdrawalJustification.minLength;
formControl.setValue('a'.repeat(minLength));
expect(formControl.valid).toBe(true);
});
it('should handle withdraw with valid form', () => {
const validJustification = 'Valid withdrawal justification';
component.withdrawalJustificationFormControl.setValue(validJustification);
expect(() => component.withdraw()).not.toThrow();
});
it('should not proceed with withdraw if form is invalid', () => {
component.withdrawalJustificationFormControl.setValue('');
expect(() => component.withdraw()).not.toThrow();
});
it('should handle withdraw request completion', () => {
const validJustification = 'Valid withdrawal justification';
component.withdrawalJustificationFormControl.setValue(validJustification);
expect(() => component.withdraw()).not.toThrow();
});
it('should handle withdraw request error', () => {
const validJustification = 'Valid withdrawal justification';
component.withdrawalJustificationFormControl.setValue(validJustification);
expect(() => component.withdraw()).not.toThrow();
});
it('should calculate modal explanation for pre-moderation pending', () => {
const providerWithPreMod = { ...mockProvider, reviewsWorkflow: ProviderReviewsWorkflow.PreModeration };
const preprintWithPending = { ...mockPreprint, reviewsState: ReviewsState.Pending };
dialogConfigMock.data = { provider: providerWithPreMod, preprint: preprintWithPending };
expect(() => {
fixture = TestBed.createComponent(PreprintWithdrawDialogComponent);
component = fixture.componentInstance;
component.ngOnInit();
}).not.toThrow();
});
it('should calculate modal explanation for pre-moderation accepted', () => {
const providerWithPreMod = { ...mockProvider, reviewsWorkflow: ProviderReviewsWorkflow.PreModeration };
const preprintWithAccepted = { ...mockPreprint, reviewsState: ReviewsState.Accepted };
dialogConfigMock.data = { provider: providerWithPreMod, preprint: preprintWithAccepted };
expect(() => {
fixture = TestBed.createComponent(PreprintWithdrawDialogComponent);
component = fixture.componentInstance;
component.ngOnInit();
}).not.toThrow();
});
it('should calculate modal explanation for post-moderation', () => {
const providerWithPostMod = { ...mockProvider, reviewsWorkflow: ProviderReviewsWorkflow.PostModeration };
dialogConfigMock.data = { provider: providerWithPostMod, preprint: mockPreprint };
expect(() => {
fixture = TestBed.createComponent(PreprintWithdrawDialogComponent);
component = fixture.componentInstance;
component.ngOnInit();
}).not.toThrow();
});
it('should handle form control state changes', () => {
const formControl = component.withdrawalJustificationFormControl;
formControl.markAsTouched();
expect(formControl.touched).toBe(true);
formControl.setValue('test');
formControl.markAsDirty();
expect(formControl.dirty).toBe(true);
});
it('should handle minimum length validation', () => {
const formControl = component.withdrawalJustificationFormControl;
const minLength = formInputLimits.withdrawalJustification.minLength;
formControl.setValue('a'.repeat(minLength - 1));
expect(formControl.hasError('minlength')).toBe(true);
formControl.setValue('a'.repeat(minLength));
expect(formControl.hasError('minlength')).toBe(false);
});
it('should handle required validation', () => {
const formControl = component.withdrawalJustificationFormControl;
formControl.setValue('');
expect(formControl.hasError('required')).toBe(true);
formControl.setValue(' ');
expect(formControl.hasError('required')).toBe(true);
formControl.setValue('Valid text');
expect(formControl.hasError('required')).toBe(false);
});
});