Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions projects/coreui-angular/src/lib/alert/alert.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -152,6 +154,27 @@ describe('AlertComponent with a custom class', () => {
});
});

@Component({
selector: 'c-custom-close-alert',
template: `
<c-alert dismissible>
<ng-template cTemplateId="alertButtonCloseTemplate"><button cButtonClose></button></ng-template>
</c-alert>
`,
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: `<c-alert [(visible)]="visible" dismissible fade>Text</c-alert>`,
imports: [AlertComponent]
Expand Down
Original file line number Diff line number Diff line change
@@ -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: `
<button cButtonClose aria-label="Fechar"></button>
<button cButtonClose [attr.aria-label]="label()"></button>
<button cButtonClose [aria-label]="label()"></button>
`,
imports: [ButtonCloseDirective]
})
class NamedButtonCloseComponent {
readonly label = signal<string | null>('Fechar');
}

@Directive({
selector: '[cComposedButtonClose]',
hostDirectives: [ButtonCloseDirective],
host: { 'aria-label': 'Fechar' }
})
class ComposedButtonCloseDirective {}

describe('ButtonCloseDirective', () => {
let disabled: WritableSignal<boolean>;
let tabindex: WritableSignal<number | undefined>;
Expand Down Expand Up @@ -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<NamedButtonCloseComponent>;

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]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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()',
Expand Down
Loading