forked from CenterForOpenScience/angular-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-selector.component.spec.ts
More file actions
92 lines (70 loc) · 2.96 KB
/
Copy pathproject-selector.component.spec.ts
File metadata and controls
92 lines (70 loc) · 2.96 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
import { provideStore, Store } from '@ngxs/store';
import { MockProvider } from 'ng-mocks';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { UserState } from '@core/store/user';
import { ProjectModel } from '@osf/shared/models/projects/projects.model';
import { ToastService } from '@osf/shared/services/toast.service';
import { ProjectsState } from '@shared/stores/projects';
import { provideOSFCore } from '@testing/osf.testing.provider';
import { ProjectSelectorComponent } from './project-selector.component';
const makeProject = (id: string, isPublic: boolean): ProjectModel =>
({ id, title: `Project ${id}`, isPublic }) as ProjectModel;
describe('ProjectSelectorComponent', () => {
let component: ProjectSelectorComponent;
let fixture: ComponentFixture<ProjectSelectorComponent>;
let store: Store;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ProjectSelectorComponent],
providers: [provideOSFCore(), MockProvider(ToastService), provideStore([ProjectsState, UserState])],
});
store = TestBed.inject(Store);
fixture = TestBed.createComponent(ProjectSelectorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should handle project selection', () => {
vi.spyOn(component.projectChange, 'emit');
const mockProject = { id: '1', title: 'Test Project' } as any;
const mockEvent = { value: mockProject };
component.handleProjectChange(mockEvent as any);
expect(component.projectChange.emit).toHaveBeenCalledWith(mockProject);
});
it('should handle filter search', () => {
const mockEvent = {
originalEvent: { preventDefault: vi.fn() },
filter: 'test filter',
};
component.handleFilterSearch(mockEvent as any);
expect(mockEvent.originalEvent.preventDefault).toHaveBeenCalled();
});
describe('publicOnly filtering', () => {
const publicProject = makeProject('1', true);
const privateProject = makeProject('2', false);
const setProjects = (projects: ProjectModel[]) => {
store.reset({
...store.snapshot(),
projects: { projects: { data: projects, isLoading: false, error: null } },
});
};
it('should show all projects when publicOnly is false', () => {
fixture.componentRef.setInput('publicOnly', false);
setProjects([publicProject, privateProject]);
fixture.detectChanges();
const ids = component.projectsOptions().map((o) => o.value.id);
expect(ids).toContain('1');
expect(ids).toContain('2');
});
it('should only show public projects when publicOnly is true', () => {
fixture.componentRef.setInput('publicOnly', true);
setProjects([publicProject, privateProject]);
fixture.detectChanges();
const ids = component.projectsOptions().map((o) => o.value.id);
expect(ids).toContain('1');
expect(ids).not.toContain('2');
});
});
});