|
| 1 | +import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing'; |
| 2 | +import { By } from '@angular/platform-browser'; |
| 3 | +import { Component } from '@angular/core'; |
| 4 | +import { FOCUS_TRAP_DIRECTIVES } from '../focus-trap/mdc.focus-trap.directive'; |
| 5 | +import { MDC_EVENT_REGISTRY_PROVIDER } from '../../utils/mdc.event.registry'; |
| 6 | +import { DIALOG_DIRECTIVES, MdcDialogDirective, MdcDialogBodyDirective } from './mdc.dialog.directive'; |
| 7 | +import { MdcButtonDirective } from '../button/mdc.button.directive'; |
| 8 | +import { cancelledClick, booleanAttributeStyleTest } from '../../testutils/page.test'; |
| 9 | + |
| 10 | +const templateWithDialog = ` |
| 11 | +<button id="open" mdcButton (click)="dialog.open()">Open Dialog</button> |
| 12 | +<aside id="dialog" #dialog="mdcDialog" mdcDialog mdcFocusTrap> |
| 13 | + <div id="surface" mdcDialogSurface> |
| 14 | + <header mdcDialogHeader> |
| 15 | + <h2 mdcDialogHeaderTitle>Modal Dialog</h2> |
| 16 | + </header> |
| 17 | + <section mdcDialogBody [scrollable]="scrollable"> |
| 18 | + Dialog Body |
| 19 | + </section> |
| 20 | + <footer mdcDialogFooter> |
| 21 | + <button *ngIf="cancelButton" id="cancel" mdcButton mdcDialogCancel>Decline</button> |
| 22 | + <button *ngIf="acceptButton" id="accept" mdcButton mdcDialogAccept>Accept</button> |
| 23 | + </footer> |
| 24 | + </div> |
| 25 | + <div mdcDialogBackdrop></div> |
| 26 | +</aside> |
| 27 | +`; |
| 28 | + |
| 29 | +describe('MdcDialogDirective', () => { |
| 30 | + @Component({ |
| 31 | + template: templateWithDialog |
| 32 | + }) |
| 33 | + class TestComponent { |
| 34 | + scrollable = false; |
| 35 | + cancelButton = true; |
| 36 | + acceptButton = true; |
| 37 | + } |
| 38 | + |
| 39 | + function setup() { |
| 40 | + const fixture = TestBed.configureTestingModule({ |
| 41 | + providers: [MDC_EVENT_REGISTRY_PROVIDER], |
| 42 | + declarations: [...DIALOG_DIRECTIVES, ...FOCUS_TRAP_DIRECTIVES, MdcButtonDirective, TestComponent] |
| 43 | + }).createComponent(TestComponent); |
| 44 | + fixture.detectChanges(); |
| 45 | + return { fixture }; |
| 46 | + } |
| 47 | + |
| 48 | + it('should only display the dialog when opened', (() => { |
| 49 | + const { fixture } = setup(); |
| 50 | + const button = fixture.nativeElement.querySelector('#open'); |
| 51 | + const dialog = fixture.nativeElement.querySelector('#dialog'); |
| 52 | + const cancel = fixture.nativeElement.querySelector('#cancel'); |
| 53 | + const accept = fixture.nativeElement.querySelector('#accept'); |
| 54 | + |
| 55 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(false, 'dialog must be in closed state'); |
| 56 | + button.click(); |
| 57 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(true, 'dialog must be in opened state'); |
| 58 | + cancel.click(); |
| 59 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(false, 'dialog must be in closed state'); |
| 60 | + button.click(); |
| 61 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(true, 'dialog must be in opened state'); |
| 62 | + accept.click(); |
| 63 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(false, 'dialog must be in closed state'); |
| 64 | + })); |
| 65 | + |
| 66 | + it('should trap focus to the dialog when opened', (() => { |
| 67 | + const { fixture } = setup(); |
| 68 | + const button = fixture.nativeElement.querySelector('#open'); |
| 69 | + const dialog = fixture.nativeElement.querySelector('#dialog'); |
| 70 | + const accept = fixture.nativeElement.querySelector('#accept'); |
| 71 | + expect(dialog.classList.contains('mdc-dialog--open')).toBe(false, 'dialog must be in closed state'); |
| 72 | + button.click(); |
| 73 | + // focusTrap is activated on animation 'transitionend', so simulate that event |
| 74 | + // (as tick() and friends won't wait for it): |
| 75 | + fixture.nativeElement.querySelector('#surface').dispatchEvent(new TransitionEvent('transitionend', {})); |
| 76 | + // clicks on the button should now be cancelled: |
| 77 | + expect(cancelledClick(button)).toBe(true); |
| 78 | + // clicks on buttons inside the dialog should not be cancelled: |
| 79 | + expect(cancelledClick(accept)).toBe(false); |
| 80 | + })); |
| 81 | + |
| 82 | + it('should apply dialog button styling to buttons dynamically added', (() => { |
| 83 | + const { fixture } = setup(); |
| 84 | + const button = fixture.nativeElement.querySelector('#open'); |
| 85 | + const dialog = fixture.nativeElement.querySelector('#dialog'); |
| 86 | + const testComponent = fixture.debugElement.injector.get(TestComponent); |
| 87 | + testComponent.cancelButton = false; |
| 88 | + testComponent.acceptButton = false; |
| 89 | + fixture.detectChanges(); |
| 90 | + |
| 91 | + button.click(); |
| 92 | + expect(fixture.nativeElement.querySelector('#cancel')).toBeNull(); |
| 93 | + testComponent.cancelButton = true; |
| 94 | + testComponent.acceptButton = true; |
| 95 | + fixture.detectChanges(); |
| 96 | + const cancel = fixture.nativeElement.querySelector('#cancel'); |
| 97 | + expect(cancel.classList).toContain('mdc-dialog__footer__button'); |
| 98 | + const accept = fixture.nativeElement.querySelector('#accept'); |
| 99 | + expect(accept.classList).toContain('mdc-dialog__footer__button'); |
| 100 | + expect(accept.classList).toContain('mdc-dialog__footer__button--accept'); |
| 101 | + })); |
| 102 | + |
| 103 | + it('should emit the accept event', (() => { |
| 104 | + const { fixture } = setup(); |
| 105 | + const button = fixture.nativeElement.querySelector('#open'); |
| 106 | + const mdcDialog = fixture.debugElement.query(By.directive(MdcDialogDirective)).injector.get(MdcDialogDirective); |
| 107 | + const accept = fixture.nativeElement.querySelector('#accept'); |
| 108 | + button.click(); |
| 109 | + let accepted = false; |
| 110 | + mdcDialog.accept.subscribe(() => { accepted = true; }); |
| 111 | + accept.click(); |
| 112 | + expect(accepted).toBe(true); |
| 113 | + })); |
| 114 | + |
| 115 | + it('should emit the cancel event', (() => { |
| 116 | + const { fixture } = setup(); |
| 117 | + const button = fixture.nativeElement.querySelector('#open'); |
| 118 | + const mdcDialog = fixture.debugElement.query(By.directive(MdcDialogDirective)).injector.get(MdcDialogDirective); |
| 119 | + const cancel = fixture.nativeElement.querySelector('#cancel'); |
| 120 | + button.click(); |
| 121 | + let canceled = false; |
| 122 | + mdcDialog.cancel.subscribe(() => { canceled = true; }); |
| 123 | + cancel.click(); |
| 124 | + expect(canceled).toBe(true); |
| 125 | + })); |
| 126 | + |
| 127 | + it('should style the body according to the scrollable property', (() => { |
| 128 | + const { fixture } = setup(); |
| 129 | + const button = fixture.nativeElement.querySelector('#open'); |
| 130 | + const dialog = fixture.nativeElement.querySelector('#dialog'); |
| 131 | + const testComponent = fixture.debugElement.injector.get(TestComponent); |
| 132 | + const mdcDialogBody = fixture.debugElement.query(By.directive(MdcDialogBodyDirective)).injector.get(MdcDialogBodyDirective); |
| 133 | + |
| 134 | + button.click(); |
| 135 | + booleanAttributeStyleTest( |
| 136 | + fixture, |
| 137 | + testComponent, |
| 138 | + mdcDialogBody, |
| 139 | + 'scrollable', |
| 140 | + 'scrollable', |
| 141 | + 'mdc-dialog__body--scrollable'); |
| 142 | + })); |
| 143 | +}); |
0 commit comments