|
1 | | -import { provideStore } from '@ngxs/store'; |
| 1 | +import { provideStore, Store } from '@ngxs/store'; |
2 | 2 |
|
3 | 3 | import { MockProvider } from 'ng-mocks'; |
4 | 4 |
|
5 | 5 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
6 | 6 |
|
7 | 7 | import { UserState } from '@core/store/user'; |
| 8 | +import { ProjectModel } from '@osf/shared/models/projects/projects.model'; |
8 | 9 | import { ToastService } from '@osf/shared/services/toast.service'; |
9 | 10 | import { ProjectsState } from '@shared/stores/projects'; |
10 | 11 |
|
11 | 12 | import { provideOSFCore } from '@testing/osf.testing.provider'; |
12 | 13 |
|
13 | 14 | import { ProjectSelectorComponent } from './project-selector.component'; |
14 | 15 |
|
| 16 | +const makeProject = (id: string, isPublic: boolean): ProjectModel => |
| 17 | + ({ id, title: `Project ${id}`, isPublic }) as ProjectModel; |
| 18 | + |
15 | 19 | describe('ProjectSelectorComponent', () => { |
16 | 20 | let component: ProjectSelectorComponent; |
17 | 21 | let fixture: ComponentFixture<ProjectSelectorComponent>; |
| 22 | + let store: Store; |
18 | 23 |
|
19 | 24 | beforeEach(() => { |
20 | 25 | TestBed.configureTestingModule({ |
21 | 26 | imports: [ProjectSelectorComponent], |
22 | 27 | providers: [provideOSFCore(), MockProvider(ToastService), provideStore([ProjectsState, UserState])], |
23 | 28 | }); |
24 | 29 |
|
| 30 | + store = TestBed.inject(Store); |
25 | 31 | fixture = TestBed.createComponent(ProjectSelectorComponent); |
26 | 32 | component = fixture.componentInstance; |
27 | 33 | fixture.detectChanges(); |
@@ -51,4 +57,36 @@ describe('ProjectSelectorComponent', () => { |
51 | 57 |
|
52 | 58 | expect(mockEvent.originalEvent.preventDefault).toHaveBeenCalled(); |
53 | 59 | }); |
| 60 | + |
| 61 | + describe('publicOnly filtering', () => { |
| 62 | + const publicProject = makeProject('1', true); |
| 63 | + const privateProject = makeProject('2', false); |
| 64 | + |
| 65 | + const setProjects = (projects: ProjectModel[]) => { |
| 66 | + store.reset({ |
| 67 | + ...store.snapshot(), |
| 68 | + projects: { projects: { data: projects, isLoading: false, error: null } }, |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + it('should show all projects when publicOnly is false', () => { |
| 73 | + fixture.componentRef.setInput('publicOnly', false); |
| 74 | + setProjects([publicProject, privateProject]); |
| 75 | + fixture.detectChanges(); |
| 76 | + |
| 77 | + const ids = component.projectsOptions().map((o) => o.value.id); |
| 78 | + expect(ids).toContain('1'); |
| 79 | + expect(ids).toContain('2'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should only show public projects when publicOnly is true', () => { |
| 83 | + fixture.componentRef.setInput('publicOnly', true); |
| 84 | + setProjects([publicProject, privateProject]); |
| 85 | + fixture.detectChanges(); |
| 86 | + |
| 87 | + const ids = component.projectsOptions().map((o) => o.value.id); |
| 88 | + expect(ids).toContain('1'); |
| 89 | + expect(ids).not.toContain('2'); |
| 90 | + }); |
| 91 | + }); |
54 | 92 | }); |
0 commit comments