LP1830432: Make sure that unit tests have an org unit selected
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / share / org-family-select / org-family-select.component.spec.ts
1 import {ComponentFixture, TestBed} from '@angular/core/testing';
2 import {Component, DebugElement, Input} from '@angular/core';
3 import {By} from '@angular/platform-browser';
4 import {OrgFamilySelectComponent} from './org-family-select.component';
5 import {ReactiveFormsModule} from '@angular/forms';
6 import {CookieService} from 'ngx-cookie';
7 import {OrgService} from '@eg/core/org.service';
8
9 @Component({
10     selector: 'eg-org-select',
11     template: ''
12 })
13 class MockOrgSelectComponent {
14     @Input() domId: string;
15     @Input() limitPerms: string;
16     @Input() initialOrgId: number;
17 }
18
19 describe('Component: OrgFamilySelect', () => {
20     let component: OrgFamilySelectComponent;
21     let fixture: ComponentFixture<OrgFamilySelectComponent>;
22     let includeAncestors: DebugElement;
23     let includeDescendants: DebugElement;
24     let orgServiceStub: Partial<OrgService>;
25     let cookieServiceStub: Partial<CookieService>;
26
27     beforeEach(() => {
28         // stub of OrgService for testing
29         // with a super simple org structure:
30         // 1 is the root note, with no children
31         orgServiceStub = {
32             root: () => {
33                 return {
34                     a: [],
35                     classname: 'aou',
36                     _isfieldmapper: true,
37                     id: () => 1};
38             },
39             get: (ouId: number) => {
40                 return {
41                     a: [],
42                     classname: 'aou',
43                     _isfieldmapper: true,
44                     children: () => Array() };
45             }
46         };
47         cookieServiceStub = {};
48         TestBed.configureTestingModule({
49             imports: [
50                 ReactiveFormsModule,
51             ], providers: [
52                 { provide: CookieService, useValue: cookieServiceStub },
53                 { provide: OrgService, useValue: orgServiceStub},
54             ], declarations: [
55                 OrgFamilySelectComponent,
56                 MockOrgSelectComponent,
57         ]});
58         fixture = TestBed.createComponent(OrgFamilySelectComponent);
59         component = fixture.componentInstance;
60         component.domId = 'family-test';
61         component.selectedOrgId = 1;
62         fixture.detectChanges();
63     });
64
65
66     it('provides includeAncestors checkbox by default', () => {
67         fixture.whenStable().then(() => {
68             includeAncestors = fixture.debugElement.query(By.css('#family-test-include-ancestors'));
69             expect(includeAncestors.nativeElement).toBeTruthy();
70         });
71     });
72
73     it('provides includeDescendants checkbox by default', () => {
74         fixture.whenStable().then(() => {
75             includeDescendants = fixture.debugElement.query(By.css('#family-test-include-descendants'));
76             expect(includeDescendants.nativeElement).toBeTruthy();
77         });
78     });
79
80     it('allows user to turn off includeAncestors checkbox', () => {
81         fixture.whenStable().then(() => {
82             component.hideAncestorSelector = true;
83             fixture.detectChanges();
84             includeAncestors = fixture.debugElement.query(By.css('#family-test-include-ancestors'));
85             expect(includeAncestors).toBeNull();
86         });
87     });
88
89     it('allows user to turn off includeDescendants checkbox', () => {
90         fixture.whenStable().then(() => {
91             component.hideDescendantSelector = true;
92             fixture.detectChanges();
93             includeDescendants = fixture.debugElement.query(By.css('#family-test-include-descendants'));
94             expect(includeDescendants).toBeNull();
95         });
96     });
97
98     it('disables includeAncestors checkbox when root OU is chosen', () => {
99         fixture.whenStable().then(() => {
100             fixture.detectChanges();
101             includeAncestors = fixture.debugElement.query(By.css('#family-test-include-ancestors'));
102             expect(includeAncestors.nativeElement.disabled).toBe(true);
103         });
104     });
105
106     it('disables includeAncestors checkbox when OU has no children', () => {
107         fixture.whenStable().then(() => {
108             fixture.detectChanges();
109             includeDescendants = fixture.debugElement.query(By.css('#family-test-include-descendants'));
110             expect(includeDescendants.nativeElement.disabled).toBe(true);
111         });
112     });
113
114 });
115