|
| 1 | +import { clearFixture, getFixture } from '../../../tests/helpers/fixture'; |
| 2 | +import Toast from '../Toast'; |
| 3 | +import { |
| 4 | + ATTRIBUTE_ARIA_EXPANDED, |
| 5 | + ATTRIBUTE_DATA_TARGET, |
| 6 | + CLASS_NAME_HIDDEN, |
| 7 | + CLASS_NAME_OPEN, |
| 8 | + CLASS_NAME_TRANSITIONING, |
| 9 | +} from '../constants'; |
| 10 | +import EventHandler from '../dom/EventHandler'; |
| 11 | + |
| 12 | +const testId = 'toast-test'; |
| 13 | + |
| 14 | +const toastHtmlClosed = ` |
| 15 | + <div id="${testId}" class="ToastBar ToastBar--success ToastBar--dismissible is-hidden"> |
| 16 | + <button type="button" data-spirit-target="#${testId}" aria-expanded="false">Close</button> |
| 17 | + </div> |
| 18 | +`; |
| 19 | + |
| 20 | +const toastHtmlOpened = ` |
| 21 | + <div id="${testId}" class="ToastBar ToastBar--success ToastBar--dismissible is-open"> |
| 22 | + <button type="button" data-spirit-target="#${testId}" aria-expanded="true">Close</button> |
| 23 | + </div> |
| 24 | +`; |
| 25 | + |
| 26 | +describe('Toast', () => { |
| 27 | + let fixtureEl: Element; |
| 28 | + |
| 29 | + beforeAll(() => { |
| 30 | + fixtureEl = getFixture(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + clearFixture(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('NAME', () => { |
| 38 | + it('should return plugin name', () => { |
| 39 | + expect(Toast.NAME).toEqual(expect.any(String)); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('DATA_KEY', () => { |
| 44 | + it('should return plugin data key', () => { |
| 45 | + expect(Toast.DATA_KEY).toBe('toast'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('EVENT_KEY', () => { |
| 50 | + it('should return plugin event key', () => { |
| 51 | + expect(Toast.EVENT_KEY).toBe('.toast'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('constructor', () => { |
| 56 | + it('should construct a toast', () => { |
| 57 | + fixtureEl.innerHTML = toastHtmlClosed; |
| 58 | + const element = fixtureEl.querySelector('.ToastBar') as HTMLElement; |
| 59 | + const toast = new Toast(element); |
| 60 | + |
| 61 | + expect(toast.element).toEqual(element); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('show', () => { |
| 66 | + it('should show a toast', async () => { |
| 67 | + fixtureEl.innerHTML = toastHtmlClosed; |
| 68 | + |
| 69 | + const element = fixtureEl.querySelector('.ToastBar') as HTMLElement; |
| 70 | + const toast = new Toast(element); |
| 71 | + const trigger = fixtureEl.querySelector(`[${ATTRIBUTE_DATA_TARGET}="#${testId}"]`) as HTMLButtonElement; |
| 72 | + |
| 73 | + const showSpy = jest.spyOn(Toast.prototype, 'show'); |
| 74 | + |
| 75 | + await toast.show(); |
| 76 | + |
| 77 | + expect(showSpy).toHaveBeenCalled(); |
| 78 | + expect(trigger).toHaveAttribute(ATTRIBUTE_ARIA_EXPANDED, 'true'); |
| 79 | + expect(element).toHaveClass(CLASS_NAME_OPEN); |
| 80 | + expect(element).toHaveClass(CLASS_NAME_TRANSITIONING); |
| 81 | + |
| 82 | + EventHandler.trigger(element, 'transitionend'); |
| 83 | + |
| 84 | + expect(element).toHaveClass(CLASS_NAME_OPEN); |
| 85 | + expect(element).not.toHaveClass(CLASS_NAME_HIDDEN); |
| 86 | + expect(element).not.toHaveClass(CLASS_NAME_TRANSITIONING); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('hide', () => { |
| 91 | + it('should hide a toast', async () => { |
| 92 | + fixtureEl.innerHTML = toastHtmlOpened; |
| 93 | + |
| 94 | + const element = fixtureEl.querySelector('.ToastBar') as HTMLElement; |
| 95 | + const toast = new Toast(element); |
| 96 | + const trigger = fixtureEl.querySelector(`[${ATTRIBUTE_DATA_TARGET}="#${testId}"]`) as HTMLButtonElement; |
| 97 | + |
| 98 | + const hideSpy = jest.spyOn(Toast.prototype, 'hide'); |
| 99 | + |
| 100 | + await toast.hide(); |
| 101 | + |
| 102 | + expect(hideSpy).toHaveBeenCalled(); |
| 103 | + expect(trigger).toHaveAttribute(ATTRIBUTE_ARIA_EXPANDED, 'false'); |
| 104 | + expect(element).toHaveClass(CLASS_NAME_HIDDEN); |
| 105 | + expect(element).toHaveClass(CLASS_NAME_TRANSITIONING); |
| 106 | + |
| 107 | + EventHandler.trigger(element, 'transitionend'); |
| 108 | + |
| 109 | + expect(fixtureEl.querySelector('.ToastBar')).toBeNull(); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments