Skip to content

Commit

Permalink
fix problem with eventsToHide being null and add test for this, rewor…
Browse files Browse the repository at this point in the history
…k tests not to mock sorting, because that is stupid
  • Loading branch information
andywilkinshmcts committed Nov 13, 2024
1 parent 1558ef9 commit 477512d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
3 changes: 2 additions & 1 deletion browserslist
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
last 2 versions
Firefox ESR
not dead
not IE 9-11 # For IE 9-11 support, remove 'not'.
not op_mini all
not kaios 2.5
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion projects/ccd-case-ui-toolkit/package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractAppConfig>;
let fixture: ComponentFixture<EventTriggerComponent>;
let component: EventTriggerComponent;
let de: DebugElement;
let orderService: OrderService;

describe('with multiple triggers', () => {
beforeEach(waitForAsync(() => {
orderService = createSpyObj<OrderService>('orderService', ['sort']);
orderService.sort.and.returnValue(SORTED_TRIGGERS);
orderService = new OrderService();
spyOn(orderService, 'sort').and.callThrough();

appConfig = createSpyObj<AbstractAppConfig>('appConfig', ['getEventsToHide']);
appConfig.getEventsToHide.and.returnValue(['']);

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand All @@ -230,10 +232,10 @@ describe('EventTriggerComponent', () => {

describe('with a single trigger', () => {
beforeEach(waitForAsync(() => {
orderService = createSpyObj<OrderService>('orderService', ['sort']);
orderService.sort.and.returnValue([ TRIGGERS[0] ]);
appConfig = createSpyObj<AbstractAppConfig>('appConfig', ['getEventsToHide']);
appConfig.getEventsToHide.and.returnValue(['queryManagementRespondQuery']);
orderService = new OrderService();
spyOn(orderService, 'sort').and.callThrough();

TestBed
.configureTestingModule({
Expand Down Expand Up @@ -277,8 +279,9 @@ describe('EventTriggerComponent', () => {

describe('Hide events', () => {
beforeEach(waitForAsync(() => {
orderService = createSpyObj<OrderService>('orderService', ['sort']);
appConfig = createSpyObj<AbstractAppConfig>('appConfig', ['getEventsToHide']);
orderService = new OrderService();
spyOn(orderService, 'sort').and.callThrough();

TestBed.configureTestingModule({
imports: [
Expand All @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});
Expand All @@ -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] : '';
}
}

0 comments on commit 477512d

Please sign in to comment.