From 477512d0a8860c96b7fc464d8092be16828f060d Mon Sep 17 00:00:00 2001 From: Andy Wilkins Date: Wed, 13 Nov 2024 14:46:28 +0000 Subject: [PATCH] fix problem with eventsToHide being null and add test for this, rework tests not to mock sorting, because that is stupid --- browserslist | 3 +- package.json | 2 +- projects/ccd-case-ui-toolkit/package.json | 2 +- .../event-trigger.component.spec.ts | 55 ++++++++++++------- .../event-trigger/event-trigger.component.ts | 7 ++- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/browserslist b/browserslist index 80848532e4..419f83ddea 100644 --- a/browserslist +++ b/browserslist @@ -9,4 +9,5 @@ last 2 versions Firefox ESR not dead -not IE 9-11 # For IE 9-11 support, remove 'not'. \ No newline at end of file +not op_mini all +not kaios 2.5 diff --git a/package.json b/package.json index c73a3cccde..2a3aabeef2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hmcts/ccd-case-ui-toolkit", - "version": "7.1.15", + "version": "7.1.15-fix-event-hiding", "engines": { "node": ">=18.19.0" }, diff --git a/projects/ccd-case-ui-toolkit/package.json b/projects/ccd-case-ui-toolkit/package.json index 587fb18ea0..5afc666adf 100644 --- a/projects/ccd-case-ui-toolkit/package.json +++ b/projects/ccd-case-ui-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@hmcts/ccd-case-ui-toolkit", - "version": "7.1.15", + "version": "7.1.15-fix-event-hiding", "engines": { "node": ">=18.19.0" }, diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.spec.ts index 65dd52b5bc..310165796f 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.spec.ts @@ -58,16 +58,18 @@ describe('EventTriggerComponent', () => { const $SUBMIT_BUTTON = By.css('form button[type=submit]'); const $EVENT_TRIGGER_FORM = By.css('.event-trigger'); - let orderService: any; + let appConfig: jasmine.SpyObj; let fixture: ComponentFixture; let component: EventTriggerComponent; let de: DebugElement; + let orderService: OrderService; describe('with multiple triggers', () => { beforeEach(waitForAsync(() => { - orderService = createSpyObj('orderService', ['sort']); - orderService.sort.and.returnValue(SORTED_TRIGGERS); + orderService = new OrderService(); + spyOn(orderService, 'sort').and.callThrough(); + appConfig = createSpyObj('appConfig', ['getEventsToHide']); appConfig.getEventsToHide.and.returnValue(['']); @@ -102,7 +104,7 @@ describe('EventTriggerComponent', () => { it('should sort triggers', () => { expect(orderService.sort).toHaveBeenCalledWith(TRIGGERS); - expect(component.triggers).toBe(SORTED_TRIGGERS); + expect(component.triggers).toEqual(SORTED_TRIGGERS); }); it('should hide when there are no triggers', () => { @@ -200,28 +202,28 @@ describe('EventTriggerComponent', () => { it('should return true if ids of both triggers match', () => { const trigger1 = { id: 'EDIT', name: 'Edit', description: 'Edit the current case', order: 1 }; const trigger2 = { id: 'EDIT', name: 'Edit', description: 'Edit the current case', order: 1 }; - + const result = component.compareFn(trigger1, trigger2); - + expect(result).toBe(true); }); - + it('should return false if ids of triggers do not match', () => { const trigger1 = { id: 'EDIT', name: 'Edit', description: 'Edit the current case', order: 1 }; const trigger2 = { id: 'HOLD', name: 'Hold', description: 'Put case on hold', order: 2 }; - + const result = component.compareFn(trigger1, trigger2); - + expect(result).toBe(false); }); - + it('should return false if one or both triggers are null or undefined', () => { const trigger1 = null; const trigger2 = { id: 'HOLD', name: 'Hold', description: 'Put case on hold', order: 2 }; - + const result1 = component.compareFn(trigger1, trigger2); const result2 = component.compareFn(trigger2, trigger1); - + expect(result1).toBe(false); expect(result2).toBe(false); }); @@ -230,10 +232,10 @@ describe('EventTriggerComponent', () => { describe('with a single trigger', () => { beforeEach(waitForAsync(() => { - orderService = createSpyObj('orderService', ['sort']); - orderService.sort.and.returnValue([ TRIGGERS[0] ]); appConfig = createSpyObj('appConfig', ['getEventsToHide']); appConfig.getEventsToHide.and.returnValue(['queryManagementRespondQuery']); + orderService = new OrderService(); + spyOn(orderService, 'sort').and.callThrough(); TestBed .configureTestingModule({ @@ -277,8 +279,9 @@ describe('EventTriggerComponent', () => { describe('Hide events', () => { beforeEach(waitForAsync(() => { - orderService = createSpyObj('orderService', ['sort']); appConfig = createSpyObj('appConfig', ['getEventsToHide']); + orderService = new OrderService(); + spyOn(orderService, 'sort').and.callThrough(); TestBed.configureTestingModule({ imports: [ @@ -305,23 +308,33 @@ describe('EventTriggerComponent', () => { })); it('should hide the respond to query event from the dropdown', () => { - orderService.sort.and.returnValue([TRIGGERS[0], TRIGGERS[1]]); appConfig.getEventsToHide.and.returnValue(['queryManagementRespondQuery']); + component.triggers = TRIGGERS; + expect(component.triggers?.length).toEqual(TRIGGERS.length); component.ngOnChanges(trigersChangeDummy(TRIGGERS)); fixture.detectChanges(); - const triggerIds = component.triggers.map((trigger) => trigger.id); + expect(component.triggers?.length).toEqual(TRIGGERS.length-1); + const triggerIds = component.triggers?.map((trigger) => trigger.id); expect(triggerIds.includes('queryManagementRespondQuery')).toBe(false); - expect(component.triggers.length).toEqual(2); }); it('should show the respond to query event from the dropdown', () => { - orderService.sort.and.returnValue(TRIGGERS); appConfig.getEventsToHide.and.returnValue(['']); component.ngOnChanges(trigersChangeDummy(TRIGGERS)); fixture.detectChanges(); - const triggerIds = component.triggers.map((trigger) => trigger.id); + console.log('triggers: ' + component.triggers?.join(' ')); + const triggerIds = component.triggers?.map((trigger) => trigger.id); + expect(triggerIds.includes('queryManagementRespondQuery')).toBe(true); + expect(component.triggers.length).toEqual(TRIGGERS.length); + }); + + it('should show the respond to query event from the dropdown when eventsToHide is null', () => { + appConfig.getEventsToHide.and.returnValue(null); + component.ngOnChanges(trigersChangeDummy(TRIGGERS)); + fixture.detectChanges(); + const triggerIds = component.triggers?.map((trigger) => trigger.id); expect(triggerIds.includes('queryManagementRespondQuery')).toBe(true); - expect(component.triggers.length).toEqual(4); + expect(component.triggers.length).toEqual(TRIGGERS.length); }); }); }); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.ts index e00edc86e8..1fe0efa1b3 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/event-trigger/event-trigger.component.ts @@ -48,8 +48,9 @@ export class EventTriggerComponent implements OnChanges, OnInit { public ngOnChanges(changes?: SimpleChanges): void { if (changes?.triggers?.currentValue) { const eventsToHide = this.appConfig.getEventsToHide(); - this.triggers = this.triggers?.filter((event) => !eventsToHide.includes(event.id)); - this.triggers = this.orderService.sort(this.triggers); + const filteredTriggers = this.triggers + ?.filter((event) => !eventsToHide || !eventsToHide.includes(event.id)); + this.triggers = this.orderService.sort(filteredTriggers); this.triggerForm = this.fb.group({ trigger: [this.getDefault(), Validators.required] }); @@ -74,6 +75,6 @@ export class EventTriggerComponent implements OnChanges, OnInit { } private getDefault(): any { - return this.triggers.length === 1 ? this.triggers[0] : ''; + return this.triggers?.length === 1 ? this.triggers[0] : ''; } }