]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/org-family-select/org-family-select.component.spec.ts
LP1830432: Make the org-family-select reusable
[working/Evergreen.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 very simple org structure:
30         // 1 is the root note
31         // 2 is its child
32         orgServiceStub = {
33             root: () => {
34                 return {
35                     a: [],
36                     classname: 'aou',
37                     _isfieldmapper: true,
38                     id: () => 1};
39             },
40             get: (ouId: number) => {
41                 return {
42                     a: [],
43                     classname: 'aou',
44                     _isfieldmapper: true,
45                     children: () => Array(2 - ouId) };
46             }
47         };
48         cookieServiceStub = {};
49         TestBed.configureTestingModule({
50             imports: [
51                 ReactiveFormsModule,
52             ], providers: [
53                 { provide: CookieService, useValue: cookieServiceStub },
54                 { provide: OrgService, useValue: orgServiceStub},
55             ], declarations: [
56                 OrgFamilySelectComponent,
57                 MockOrgSelectComponent,
58         ]});
59         fixture = TestBed.createComponent(OrgFamilySelectComponent);
60         component = fixture.componentInstance;
61         component.domId = 'family-test';
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             component.selectedOrgId = 1;
101             fixture.detectChanges();
102             includeAncestors = fixture.debugElement.query(By.css('#family-test-include-ancestors'));
103             expect(includeAncestors.nativeElement.disabled).toBe(true);
104         });
105     });
106
107 });
108