Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components/modals): use optional injector for SkyModalConfiguration #1615

Merged
merged 1 commit into from
Aug 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { SkyModalHostComponent } from './modal-host.component';
import { SkyModalHostService } from './modal-host.service';
import { SkyModalInstance } from './modal-instance';
import { SkyModalScrollShadowDirective } from './modal-scroll-shadow.directive';
import { SkyModalModule } from './modal.module';
import { SkyModalService } from './modal.service';

describe('Modal component', () => {
Expand Down Expand Up @@ -1268,3 +1269,32 @@ describe('Modal component', () => {
}));
});
});

describe('Modal component (created w/o SkyModalService)', () => {
it('should setup a default modal configuration', () => {
TestBed.configureTestingModule({
declarations: [ModalTestComponent],
imports: [SkyModalModule],
providers: [
{
provide: SkyModalHostService,
useValue: {
onClose: (): void => {
/** */
},
},
},
],
});

const fixture = TestBed.createComponent(ModalTestComponent);

fixture.detectChanges();

// Medium is the default size for modals.
expect(fixture.nativeElement.querySelector('.sky-modal-medium')).toExist();

// Close the modal.
fixture.nativeElement.querySelector('.sky-modal-btn-close').click();
});
});
27 changes: 18 additions & 9 deletions libs/components/modals/src/lib/modules/modal/modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,18 @@ export class SkyModalComponent implements AfterViewInit, OnDestroy, OnInit {
#errorsSvc = inject(SkyModalErrorsService);
#liveAnnouncerSvc = inject(SkyLiveAnnouncerService);

/**
* This provider is optional to account for situations where a modal component
* is implemented without the modal service. For example, when a consumer tests
* a component that uses the modal component but doesn't launch the modal from
* the modal service before executing assertions.
*/
#config =
inject(SkyModalConfiguration, { optional: true }) ??
new SkyModalConfiguration();

constructor(
hostService: SkyModalHostService,
config: SkyModalConfiguration,
elRef: ElementRef,
windowRef: SkyAppWindowRef,
componentAdapter: SkyModalComponentAdapterService,
Expand All @@ -151,16 +160,16 @@ export class SkyModalComponent implements AfterViewInit, OnDestroy, OnInit {
this.#dockService = dockService;
this.#mediaQueryService = mediaQueryService;

this.ariaDescribedBy = config.ariaDescribedBy;
this.ariaLabelledBy = config.ariaLabelledBy;
this.ariaRole = config.ariaRole;
this.helpKey = config.helpKey;
this.tiledBody = config.tiledBody;
this.wrapperClass = config.wrapperClass;
this.ariaDescribedBy = this.#config.ariaDescribedBy;
this.ariaLabelledBy = this.#config.ariaLabelledBy;
this.ariaRole = this.#config.ariaRole;
this.helpKey = this.#config.helpKey;
this.tiledBody = this.#config.tiledBody;
this.wrapperClass = this.#config.wrapperClass;

this.size = config.fullPage
this.size = this.#config.fullPage
? 'full-page'
: config.size?.toLowerCase() || 'medium';
: this.#config.size?.toLowerCase() || 'medium';

this.modalZIndex = this.#hostService.zIndex;
}
Expand Down
Loading