LP#1833080: have eg-bool recognize IDL bool string values
authorGalen Charlton <gmc@equinoxinitiative.org>
Mon, 17 Jun 2019 14:46:14 +0000 (10:46 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 20 Jun 2019 17:47:58 +0000 (13:47 -0400)
This patch updates eg-bool so that it can format both
true Boolean and IDL bool string values (i.e., 't' or 'f'). Prior
to this patch, IDL bool values would always be rendered as 'Yes'.

This patch relaxes the type restriction on the value setter
and getter; unfortunately, there's no way to overload the
setter or making it accept (say) boolean|string.A

This patch also supplies some unit sets.

To test
-------
[1] View an Angular grid that has Boolean fields. The Copy
    Status server admin page is a good one.
[2] Note that the boolean values are all rendered as "Yes".
[3] Apply the patch and repeat step 1. This time, false
    values should be displayed as "No".
[4] Verify that 'npm run test' for the Angular app passes.

Sponsored-by: PaILS

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Bill Erickson <berickxx@gmail.com>

Open-ILS/src/eg2/src/app/share/util/bool.component.spec.ts [new file with mode: 0644]
Open-ILS/src/eg2/src/app/share/util/bool.component.ts

diff --git a/Open-ILS/src/eg2/src/app/share/util/bool.component.spec.ts b/Open-ILS/src/eg2/src/app/share/util/bool.component.spec.ts
new file mode 100644 (file)
index 0000000..aeec506
--- /dev/null
@@ -0,0 +1,59 @@
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { BoolDisplayComponent } from './bool.component';
+import { Component } from '@angular/core';
+import { ViewChild } from '@angular/core';
+
+describe('BoolDisplayComponent', () => {
+    @Component({
+        selector: `eg-host-component`,
+        template: `<eg-bool></eg-bool>`
+    })
+    class TestHostComponent {
+        @ViewChild(BoolDisplayComponent)
+        public boolDisplayComponent: BoolDisplayComponent;
+    }
+
+    let hostComponent: TestHostComponent;
+    let fixture: ComponentFixture<TestHostComponent>;
+
+    beforeEach(async(() => {
+        TestBed.configureTestingModule({
+        declarations: [ BoolDisplayComponent, TestHostComponent ],
+        })
+        .compileComponents();
+    }));
+
+    beforeEach(() => {
+        fixture = TestBed.createComponent(TestHostComponent);
+        hostComponent = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('recognizes Javascript true', async() => {
+        hostComponent.boolDisplayComponent.value = true;
+        fixture.detectChanges();
+        expect(fixture.nativeElement.querySelector('span').innerText).toEqual('Yes');
+    });
+    it('recognizes Javascript false', async() => {
+        hostComponent.boolDisplayComponent.value = false;
+        fixture.detectChanges();
+        expect(fixture.nativeElement.querySelector('span').innerText).toEqual('No');
+    });
+    it('recognizes string "t"', async() => {
+        hostComponent.boolDisplayComponent.value = 't';
+        fixture.detectChanges();
+        expect(fixture.nativeElement.querySelector('span').innerText).toEqual('Yes');
+    });
+    it('recognizes string "f"', async() => {
+        hostComponent.boolDisplayComponent.value = 'f';
+        fixture.detectChanges();
+        expect(fixture.nativeElement.querySelector('span').innerText).toEqual('No');
+    });
+    it('recognizes ternary nul', async() => {
+        hostComponent.boolDisplayComponent.value = null;
+        hostComponent.boolDisplayComponent.ternary = true;
+        fixture.detectChanges();
+        expect(fixture.nativeElement.querySelector('span').innerText).toEqual('Unset');
+    });
+
+});
index 2c7ec97..a7363b3 100644 (file)
@@ -16,10 +16,20 @@ import {Component, Input} from '@angular/core';
 export class BoolDisplayComponent {
 
     _value: boolean;
-    @Input() set value(v: boolean) {
-        this._value = v;
+    @Input() set value(v: any) {
+        if (typeof v === 'string') {
+            if (v === 't') {
+                this._value = true;
+            } else if (v === 'f') {
+                this._value = false;
+            } else {
+                this._value = null;
+            }
+        } else {
+            this._value = v;
+        }
     }
-    get value(): boolean {
+    get value(): any {
         return this._value;
     }