From 6be7c4e30fa2b2fe257b2c88c6be70e1162ff6ee Mon Sep 17 00:00:00 2001 From: xidedix Date: Wed, 25 Nov 2026 18:30:26 +0000 Subject: [PATCH] fix(ButtonClose): name the button Close by default - static host `aria-label="Close"`, also in `alertButtonCloseTemplate` and in server HTML - `aria-label` of the page (attribute or binding) and of a composing directive wins - Behavior change: `title` or text no longer names the button, nor does a static host `aria-label` of a directive imported before `ButtonCloseDirective`; a test looking for a button named "Close" can match one more --- .../src/lib/alert/alert.component.spec.ts | 23 +++++++ .../lib/button/button-close.directive.spec.ts | 64 ++++++++++++++++++- .../src/lib/button/button-close.directive.ts | 1 + 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/projects/coreui-angular/src/lib/alert/alert.component.spec.ts b/projects/coreui-angular/src/lib/alert/alert.component.spec.ts index 5b8eb0b5..ca90696b 100644 --- a/projects/coreui-angular/src/lib/alert/alert.component.spec.ts +++ b/projects/coreui-angular/src/lib/alert/alert.component.spec.ts @@ -3,6 +3,8 @@ import { By } from '@angular/platform-browser'; import { expect } from 'vitest'; import { AlertComponent } from './alert.component'; +import { ButtonCloseDirective } from '../button'; +import { TemplateIdDirective } from '../shared'; import { AnimationCallbackEvent, ApplicationRef, @@ -152,6 +154,27 @@ describe('AlertComponent with a custom class', () => { }); }); +@Component({ + selector: 'c-custom-close-alert', + template: ` + + + + `, + imports: [AlertComponent, ButtonCloseDirective, TemplateIdDirective] +}) +class CustomCloseAlertComponent {} + +describe('AlertComponent with a custom close button', () => { + it('should name the custom close button', async () => { + const fixture = TestBed.createComponent(CustomCloseAlertComponent); + await fixture.whenStable(); + const buttons = fixture.nativeElement.querySelectorAll('.btn-close'); + expect(buttons).toHaveLength(1); + expect(buttons[0].getAttribute('aria-label')).toBe('Close'); + }); +}); + @Component({ template: `Text`, imports: [AlertComponent] diff --git a/projects/coreui-angular/src/lib/button/button-close.directive.spec.ts b/projects/coreui-angular/src/lib/button/button-close.directive.spec.ts index cf93c510..dcecd5a1 100644 --- a/projects/coreui-angular/src/lib/button/button-close.directive.spec.ts +++ b/projects/coreui-angular/src/lib/button/button-close.directive.spec.ts @@ -1,7 +1,28 @@ -import { inputBinding, signal, WritableSignal } from '@angular/core'; -import { DirectiveFixture, TestBed } from '@angular/core/testing'; +import { Component, Directive, inputBinding, signal, WritableSignal } from '@angular/core'; +import { ComponentFixture, DirectiveFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; import { ButtonCloseDirective } from './button-close.directive'; +@Component({ + selector: 'c-named-button-close', + template: ` + + + + `, + imports: [ButtonCloseDirective] +}) +class NamedButtonCloseComponent { + readonly label = signal('Fechar'); +} + +@Directive({ + selector: '[cComposedButtonClose]', + hostDirectives: [ButtonCloseDirective], + host: { 'aria-label': 'Fechar' } +}) +class ComposedButtonCloseDirective {} + describe('ButtonCloseDirective', () => { let disabled: WritableSignal; let tabindex: WritableSignal; @@ -48,4 +69,43 @@ describe('ButtonCloseDirective', () => { await fixture.whenStable(); expect(fixture.nativeElement.getAttribute('tabindex')).toBe('-1'); }); + + it('should be named Close by default, before the first render', () => { + const unrenderedFixture = TestBed.createDirective(ButtonCloseDirective, { tagName: 'button' }); + expect(unrenderedFixture.nativeElement.getAttribute('aria-label')).toBe('Close'); + }); + + it('should keep the aria-label of a composing directive', async () => { + const composedFixture = TestBed.createDirective(ComposedButtonCloseDirective, { tagName: 'button' }); + await composedFixture.whenStable(); + expect(composedFixture.nativeElement.classList.contains('btn-close')).toBe(true); + expect(composedFixture.nativeElement.getAttribute('aria-label')).toBe('Fechar'); + }); +}); + +describe('ButtonCloseDirective named by the page', () => { + let fixture: ComponentFixture; + + const names = () => + fixture.debugElement + .queryAll(By.directive(ButtonCloseDirective)) + .map(({ nativeElement }) => nativeElement.getAttribute('aria-label')); + + beforeEach(async () => { + fixture = TestBed.createComponent(NamedButtonCloseComponent); + await fixture.whenStable(); + }); + + it('should keep the aria-label of the page', async () => { + expect(names()).toEqual(['Fechar', 'Fechar', 'Fechar']); + fixture.componentInstance.label.set('Cerrar'); + await fixture.whenStable(); + expect(names()).toEqual(['Fechar', 'Cerrar', 'Cerrar']); + }); + + it('should drop the default when the page binds null', async () => { + fixture.componentInstance.label.set(null); + await fixture.whenStable(); + expect(names()).toEqual(['Fechar', null, null]); + }); }); diff --git a/projects/coreui-angular/src/lib/button/button-close.directive.ts b/projects/coreui-angular/src/lib/button/button-close.directive.ts index f7355b27..2dce4964 100644 --- a/projects/coreui-angular/src/lib/button/button-close.directive.ts +++ b/projects/coreui-angular/src/lib/button/button-close.directive.ts @@ -7,6 +7,7 @@ import { ButtonDirective } from './button.directive'; hostDirectives: [{ directive: ThemeDirective, inputs: ['dark'] }], host: { class: 'btn btn-close', + 'aria-label': 'Close', '[class]': 'hostClasses()', '[aria-disabled]': 'ariaDisabled()', '[aria-pressed]': 'isActive()',