Skip to content

Commit 5480900

Browse files
authored
fix(contributors-table): fixed failed tests (#977)
## Summary of Changes 1. Fixed failed tests for contributors table
1 parent a933d9d commit 5480900

2 files changed

Lines changed: 93 additions & 45 deletions

File tree

src/app/shared/components/contributors/contributors-table/contributors-table.component.spec.ts

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MockProvider } from 'ng-mocks';
1+
import { MockComponents, MockProvider } from 'ng-mocks';
22

33
import { ComponentFixture, TestBed } from '@angular/core/testing';
44

@@ -13,84 +13,95 @@ import { CustomDialogServiceMockBuilder } from '@testing/providers/custom-dialog
1313

1414
import { EducationHistoryDialogComponent } from '../../education-history-dialog/education-history-dialog.component';
1515
import { EmploymentHistoryDialogComponent } from '../../employment-history-dialog/employment-history-dialog.component';
16+
import { IconComponent } from '../../icon/icon.component';
17+
import { InfoIconComponent } from '../../info-icon/info-icon.component';
18+
import { SelectComponent } from '../../select/select.component';
1619

1720
import { ContributorsTableComponent } from './contributors-table.component';
1821

22+
const makeTableParams = (overrides: Partial<TableParameters> = {}): TableParameters => ({
23+
rows: 10,
24+
paginator: true,
25+
scrollable: false,
26+
rowsPerPageOptions: [10, 25, 50],
27+
totalRecords: 4,
28+
firstRowIndex: 10,
29+
defaultSortOrder: null,
30+
defaultSortColumn: null,
31+
...overrides,
32+
});
33+
1934
describe('ContributorsTableComponent', () => {
2035
let component: ContributorsTableComponent;
2136
let fixture: ComponentFixture<ContributorsTableComponent>;
2237
let mockCustomDialogService: ReturnType<CustomDialogServiceMockBuilder['build']>;
2338

24-
const tableParams: TableParameters = {
25-
rows: 10,
26-
paginator: true,
27-
scrollable: false,
28-
rowsPerPageOptions: [10, 25, 50],
29-
totalRecords: 4,
30-
firstRowIndex: 10,
31-
defaultSortOrder: null,
32-
defaultSortColumn: null,
33-
};
34-
3539
beforeEach(() => {
3640
mockCustomDialogService = CustomDialogServiceMockBuilder.create().build();
3741

3842
TestBed.configureTestingModule({
39-
imports: [ContributorsTableComponent],
43+
imports: [ContributorsTableComponent, ...MockComponents(SelectComponent, IconComponent, InfoIconComponent)],
4044
providers: [provideOSFCore(), MockProvider(CustomDialogService, mockCustomDialogService)],
4145
});
4246

4347
fixture = TestBed.createComponent(ContributorsTableComponent);
4448
component = fixture.componentInstance;
45-
fixture.componentRef.setInput('tableParams', tableParams);
49+
fixture.componentRef.setInput('tableParams', makeTableParams());
4650
fixture.detectChanges();
4751
});
4852

4953
it('should create', () => {
5054
expect(component).toBeTruthy();
5155
});
5256

53-
it('should compute isProject based on resourceType', () => {
57+
it('should return true from isProject when resourceType is Project', () => {
5458
fixture.componentRef.setInput('resourceType', ResourceType.Project);
5559
fixture.detectChanges();
5660
expect(component.isProject()).toBe(true);
61+
});
5762

63+
it('should return false from isProject when resourceType is Registration', () => {
5864
fixture.componentRef.setInput('resourceType', ResourceType.Registration);
5965
fixture.detectChanges();
6066
expect(component.isProject()).toBe(false);
6167
});
6268

63-
it('should compute deactivatedContributors when list contains deactivated contributor', () => {
64-
const contributors: ContributorModel[] = [
69+
it('should return true from deactivatedContributors when at least one contributor is deactivated', () => {
70+
fixture.componentRef.setInput('contributors', [
6571
{ ...MOCK_CONTRIBUTOR, id: '1', deactivated: false },
6672
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '2', deactivated: true },
67-
];
68-
69-
component.contributors.set(contributors);
70-
73+
]);
74+
fixture.detectChanges();
7175
expect(component.deactivatedContributors()).toBe(true);
7276
});
7377

74-
it('should compute showLoadMore when loaded contributors are below total records', () => {
75-
component.contributors.set([{ ...MOCK_CONTRIBUTOR, id: '1' }]);
76-
77-
expect(component.showLoadMore()).toBe(true);
78+
it('should return false from deactivatedContributors when all contributors are active', () => {
79+
fixture.componentRef.setInput('contributors', [
80+
{ ...MOCK_CONTRIBUTOR, id: '1', deactivated: false },
81+
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '2', deactivated: false },
82+
]);
83+
fixture.detectChanges();
84+
expect(component.deactivatedContributors()).toBe(false);
7885
});
7986

80-
it('should compute showLoadMore as false when contributors length matches total records', () => {
81-
const contributors: ContributorModel[] = [
82-
{ ...MOCK_CONTRIBUTOR, id: '1' },
83-
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '2' },
84-
{ ...MOCK_CONTRIBUTOR, id: '3' },
85-
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '4' },
86-
];
87-
component.contributors.set(contributors);
87+
it('should return false from deactivatedContributors when contributor list is empty', () => {
88+
fixture.componentRef.setInput('contributors', []);
89+
fixture.detectChanges();
90+
expect(component.deactivatedContributors()).toBe(false);
91+
});
8892

93+
it('should default showLoadMore to false', () => {
8994
expect(component.showLoadMore()).toBe(false);
9095
});
9196

92-
it('should emit remove event when removeContributor is called', () => {
93-
const contributor = { ...MOCK_CONTRIBUTOR, id: 'remove-id' };
97+
it('should reflect showLoadMore as true when set by parent', () => {
98+
fixture.componentRef.setInput('showLoadMore', true);
99+
fixture.detectChanges();
100+
expect(component.showLoadMore()).toBe(true);
101+
});
102+
103+
it('should emit remove event with the given contributor when removeContributor is called', () => {
104+
const contributor: ContributorModel = { ...MOCK_CONTRIBUTOR, id: 'remove-id' };
94105
vi.spyOn(component.remove, 'emit');
95106

96107
component.removeContributor(contributor);
@@ -106,7 +117,7 @@ describe('ContributorsTableComponent', () => {
106117
expect(component.loadMore.emit).toHaveBeenCalled();
107118
});
108119

109-
it('should open education history dialog with contributor education data', () => {
120+
it('should open EducationHistoryDialogComponent with contributor education data', () => {
110121
const contributor: ContributorModel = {
111122
...MOCK_CONTRIBUTOR,
112123
id: 'education-id',
@@ -133,7 +144,19 @@ describe('ContributorsTableComponent', () => {
133144
});
134145
});
135146

136-
it('should open employment history dialog with contributor employment data', () => {
147+
it('should open EducationHistoryDialogComponent with an empty education array', () => {
148+
const contributor: ContributorModel = { ...MOCK_CONTRIBUTOR, id: 'no-education-id', education: [] };
149+
150+
component.openEducationHistory(contributor);
151+
152+
expect(mockCustomDialogService.open).toHaveBeenCalledWith(EducationHistoryDialogComponent, {
153+
header: 'project.contributors.table.headers.education',
154+
width: '552px',
155+
data: [],
156+
});
157+
});
158+
159+
it('should open EmploymentHistoryDialogComponent with contributor employment data', () => {
137160
const contributor: ContributorModel = {
138161
...MOCK_CONTRIBUTOR,
139162
id: 'employment-id',
@@ -160,16 +183,42 @@ describe('ContributorsTableComponent', () => {
160183
});
161184
});
162185

163-
it('should reorder contributors indices using table firstRowIndex', () => {
164-
const contributors: ContributorModel[] = [
186+
it('should open EmploymentHistoryDialogComponent with an empty employment array', () => {
187+
const contributor: ContributorModel = { ...MOCK_CONTRIBUTOR, id: 'no-employment-id', employment: [] };
188+
189+
component.openEmploymentHistory(contributor);
190+
191+
expect(mockCustomDialogService.open).toHaveBeenCalledWith(EmploymentHistoryDialogComponent, {
192+
header: 'project.contributors.table.headers.employment',
193+
width: '552px',
194+
data: [],
195+
});
196+
});
197+
198+
it('should reindex contributors starting from tableParams.firstRowIndex on row reorder', () => {
199+
fixture.componentRef.setInput('tableParams', makeTableParams({ firstRowIndex: 10 }));
200+
fixture.componentRef.setInput('contributors', [
165201
{ ...MOCK_CONTRIBUTOR, id: '1', index: 0 },
166202
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '2', index: 1 },
167203
{ ...MOCK_CONTRIBUTOR, id: '3', index: 2 },
168-
];
169-
component.contributors.set(contributors);
204+
]);
205+
fixture.detectChanges();
206+
207+
component.onRowReorder();
208+
209+
expect(component.contributors().map((c) => c.index)).toEqual([10, 11, 12]);
210+
});
211+
212+
it('should reindex contributors from 0 when firstRowIndex is 0 on row reorder', () => {
213+
fixture.componentRef.setInput('tableParams', makeTableParams({ firstRowIndex: 0 }));
214+
fixture.componentRef.setInput('contributors', [
215+
{ ...MOCK_CONTRIBUTOR, id: '1', index: 5 },
216+
{ ...MOCK_CONTRIBUTOR_WITHOUT_HISTORY, id: '2', index: 6 },
217+
]);
218+
fixture.detectChanges();
170219

171220
component.onRowReorder();
172221

173-
expect(component.contributors().map((item) => item.index)).toEqual([10, 11, 12]);
222+
expect(component.contributors().map((c) => c.index)).toEqual([0, 1]);
174223
});
175224
});

src/app/shared/components/contributors/contributors-table/contributors-table.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { ContributorPermission } from '@osf/shared/enums/contributors/contributo
1515
import { ResourceType } from '@osf/shared/enums/resource-type.enum';
1616
import { CustomDialogService } from '@osf/shared/services/custom-dialog.service';
1717
import { ContributorModel } from '@shared/models/contributors/contributor.model';
18-
import { SelectOption } from '@shared/models/select-option.model';
1918
import { TableParameters } from '@shared/models/table-parameters.model';
2019

2120
import { EducationHistoryDialogComponent } from '../../education-history-dialog/education-history-dialog.component';
@@ -61,10 +60,10 @@ export class ContributorsTableComponent {
6160

6261
customDialogService = inject(CustomDialogService);
6362

64-
readonly permissionsOptions: SelectOption[] = PERMISSION_OPTIONS;
63+
readonly permissionsOptions = PERMISSION_OPTIONS;
6564
readonly ContributorPermission = ContributorPermission;
6665

67-
skeletonData: ContributorModel[] = Array.from({ length: 3 }, () => ({}) as ContributorModel);
66+
skeletonData = Array.from({ length: 3 }, () => ({}) as ContributorModel);
6867

6968
isProject = computed(() => this.resourceType() === ResourceType.Project);
7069

0 commit comments

Comments
 (0)