From eef6f0fa0f2dedb5aa08836558643ac4d09ed088 Mon Sep 17 00:00:00 2001
From: Galen Charlton <gmc@equinoxinitiative.org>
Date: Mon, 17 Jun 2019 10:46:14 -0400
Subject: [PATCH] LP#1833080: have eg-bool recognize IDL bool string values

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>
---
 .../eg2/src/app/share/util/bool.component.spec.ts  | 59 ++++++++++++++++++++++
 .../src/eg2/src/app/share/util/bool.component.ts   | 16 ++++--
 2 files changed, 72 insertions(+), 3 deletions(-)
 create mode 100644 Open-ILS/src/eg2/src/app/share/util/bool.component.spec.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
index 0000000000..aeec506333
--- /dev/null
+++ b/Open-ILS/src/eg2/src/app/share/util/bool.component.spec.ts
@@ -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');
+    });
+
+});
diff --git a/Open-ILS/src/eg2/src/app/share/util/bool.component.ts b/Open-ILS/src/eg2/src/app/share/util/bool.component.ts
index 2c7ec971f0..a7363b330d 100644
--- a/Open-ILS/src/eg2/src/app/share/util/bool.component.ts
+++ b/Open-ILS/src/eg2/src/app/share/util/bool.component.ts
@@ -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;
     }
 
-- 
2.11.0