|
| 1 | +import type { InternalConfirmDialog } from '../InternalConfirmDialog/InternalConfirmDialog'; |
| 2 | +import type { NotificationElement } from '@vaadin/vaadin-notification'; |
| 3 | +import type { ButtonElement } from '@vaadin/vaadin-button'; |
| 4 | +import type { FetchEvent } from '../../public/NucleonElement/FetchEvent'; |
| 5 | + |
| 6 | +import './index'; |
| 7 | + |
| 8 | +import { InternalPostActionControl as Control } from './InternalPostActionControl'; |
| 9 | +import { html, expect, fixture, waitUntil } from '@open-wc/testing'; |
| 10 | +import { DialogHideEvent } from '../../private/Dialog/DialogHideEvent'; |
| 11 | +import { stub } from 'sinon'; |
| 12 | + |
| 13 | +describe('InternalPostActionControl', () => { |
| 14 | + it('imports dependencies', () => { |
| 15 | + expect(customElements.get('vaadin-notification')).to.exist; |
| 16 | + expect(customElements.get('vaadin-button')).to.exist; |
| 17 | + expect(customElements.get('foxy-internal-confirm-dialog')).to.exist; |
| 18 | + expect(customElements.get('foxy-internal-control')).to.exist; |
| 19 | + expect(customElements.get('foxy-i18n')).to.exist; |
| 20 | + }); |
| 21 | + |
| 22 | + it('defines itself as foxy-internal-post-action-control', () => { |
| 23 | + expect(customElements.get('foxy-internal-post-action-control')).to.equal(Control); |
| 24 | + }); |
| 25 | + |
| 26 | + it('defines reactive properties', () => { |
| 27 | + expect(new Control()).to.have.property('theme', null); |
| 28 | + expect(Control.properties).to.have.property('theme'); |
| 29 | + |
| 30 | + expect(new Control()).to.have.property('href', null); |
| 31 | + expect(Control.properties).to.have.property('href'); |
| 32 | + |
| 33 | + expect(new Control()).to.have.deep.property('messageOptions', {}); |
| 34 | + expect(Control.properties).to.have.deep.property('messageOptions', { |
| 35 | + attribute: 'message-options', |
| 36 | + type: Object, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('extends foxy-internal-control', () => { |
| 41 | + expect(new Control()).to.be.instanceOf(customElements.get('foxy-internal-control')); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders internal confirm dialog', async () => { |
| 45 | + const control = await fixture<Control>( |
| 46 | + html` |
| 47 | + <foxy-internal-post-action-control .messageOptions=${{ foo: 'bar' }}> |
| 48 | + </foxy-internal-post-action-control> |
| 49 | + ` |
| 50 | + ); |
| 51 | + |
| 52 | + const dialog = control.renderRoot.querySelector('foxy-internal-confirm-dialog'); |
| 53 | + |
| 54 | + expect(dialog).to.exist; |
| 55 | + expect(dialog).to.have.attribute('header', 'header'); |
| 56 | + expect(dialog).to.have.attribute('infer', 'confirm-dialog'); |
| 57 | + expect(dialog).to.have.deep.property('messageOptions', { foo: 'bar' }); |
| 58 | + }); |
| 59 | + |
| 60 | + ['success', 'error'].forEach(theme => { |
| 61 | + it(`renders ${theme} notification`, async () => { |
| 62 | + const control = await fixture<Control>( |
| 63 | + html` |
| 64 | + <foxy-internal-post-action-control lang="es" ns="post-action"> |
| 65 | + </foxy-internal-post-action-control> |
| 66 | + ` |
| 67 | + ); |
| 68 | + |
| 69 | + const notification = control.renderRoot.querySelector<NotificationElement>( |
| 70 | + `#${theme}-notification` |
| 71 | + ); |
| 72 | + |
| 73 | + expect(notification).to.exist; |
| 74 | + expect(notification).to.have.attribute('position', 'bottom-end'); |
| 75 | + expect(notification).to.have.attribute('duration', '3000'); |
| 76 | + expect(notification).to.have.attribute('theme', theme); |
| 77 | + |
| 78 | + const root = document.createElement('div'); |
| 79 | + notification?.renderer?.(root); |
| 80 | + |
| 81 | + const message = root.querySelector('foxy-i18n'); |
| 82 | + expect(message).to.exist; |
| 83 | + expect(message).to.have.attribute('key', theme); |
| 84 | + expect(message).to.have.attribute('lang', 'es'); |
| 85 | + expect(message).to.have.attribute('ns', 'post-action notification'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('renders button', async () => { |
| 90 | + const control = await fixture<Control>(html` |
| 91 | + <foxy-internal-post-action-control theme="primary"></foxy-internal-post-action-control> |
| 92 | + `); |
| 93 | + |
| 94 | + const button = control.renderRoot.querySelector('vaadin-button'); |
| 95 | + expect(button).to.exist; |
| 96 | + expect(button).to.have.attribute('theme', 'primary'); |
| 97 | + |
| 98 | + const caption = button?.querySelector('foxy-i18n'); |
| 99 | + expect(caption).to.exist; |
| 100 | + expect(caption).to.have.attribute('key', 'idle'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('opens confirm dialog on button click', async () => { |
| 104 | + const control = await fixture<Control>(html` |
| 105 | + <foxy-internal-post-action-control></foxy-internal-post-action-control> |
| 106 | + `); |
| 107 | + |
| 108 | + const button = control.renderRoot.querySelector('vaadin-button') as ButtonElement; |
| 109 | + const dialog = control.renderRoot.querySelector( |
| 110 | + 'foxy-internal-confirm-dialog' |
| 111 | + ) as InternalConfirmDialog; |
| 112 | + |
| 113 | + const showMethod = stub(dialog, 'show'); |
| 114 | + button.click(); |
| 115 | + |
| 116 | + expect(showMethod).to.have.been.calledOnce; |
| 117 | + expect(showMethod).to.have.been.calledWith(button); |
| 118 | + }); |
| 119 | + |
| 120 | + it('sends POST request on confirm dialog submit (successful response)', async () => { |
| 121 | + let lastSuccessEvent = null as CustomEvent | null; |
| 122 | + let lastFetchEvent = null as FetchEvent | null; |
| 123 | + |
| 124 | + const control = await fixture<Control>(html` |
| 125 | + <foxy-internal-post-action-control |
| 126 | + href="https://demo.api/virtual/empty" |
| 127 | + @success=${(evt: CustomEvent) => (lastSuccessEvent = evt)} |
| 128 | + @fetch=${(evt: FetchEvent) => { |
| 129 | + lastFetchEvent = evt; |
| 130 | + evt.respondWith(Promise.resolve(new Response())); |
| 131 | + }} |
| 132 | + > |
| 133 | + </foxy-internal-post-action-control> |
| 134 | + `); |
| 135 | + |
| 136 | + const dialog = control.renderRoot.querySelector( |
| 137 | + 'foxy-internal-confirm-dialog' |
| 138 | + ) as InternalConfirmDialog; |
| 139 | + |
| 140 | + dialog.dispatchEvent(new DialogHideEvent(false)); |
| 141 | + await control.requestUpdate(); |
| 142 | + const button = control.renderRoot.querySelector('vaadin-button') as ButtonElement; |
| 143 | + const caption = button.querySelector('foxy-i18n'); |
| 144 | + expect(button).to.have.attribute('disabled'); |
| 145 | + expect(caption).to.have.attribute('key', 'busy'); |
| 146 | + |
| 147 | + await waitUntil(() => lastFetchEvent !== null); |
| 148 | + expect(lastFetchEvent?.request.url).to.equal('https://demo.api/virtual/empty'); |
| 149 | + expect(lastFetchEvent?.request.method).to.equal('POST'); |
| 150 | + |
| 151 | + await waitUntil(() => lastSuccessEvent !== null); |
| 152 | + await control.requestUpdate(); |
| 153 | + expect(button).to.not.have.attribute('disabled'); |
| 154 | + expect(caption).to.have.attribute('key', 'idle'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('sends POST request on confirm dialog submit (failed response)', async () => { |
| 158 | + let lastErrorEvent = null as CustomEvent | null; |
| 159 | + let lastFetchEvent = null as FetchEvent | null; |
| 160 | + |
| 161 | + const control = await fixture<Control>(html` |
| 162 | + <foxy-internal-post-action-control |
| 163 | + href="https://demo.api/virtual/empty" |
| 164 | + @error=${(evt: CustomEvent) => (lastErrorEvent = evt)} |
| 165 | + @fetch=${(evt: FetchEvent) => { |
| 166 | + lastFetchEvent = evt; |
| 167 | + evt.respondWith(Promise.resolve(new Response(null, { status: 400 }))); |
| 168 | + }} |
| 169 | + > |
| 170 | + </foxy-internal-post-action-control> |
| 171 | + `); |
| 172 | + |
| 173 | + const dialog = control.renderRoot.querySelector( |
| 174 | + 'foxy-internal-confirm-dialog' |
| 175 | + ) as InternalConfirmDialog; |
| 176 | + |
| 177 | + dialog.dispatchEvent(new DialogHideEvent(false)); |
| 178 | + await control.requestUpdate(); |
| 179 | + const button = control.renderRoot.querySelector('vaadin-button') as ButtonElement; |
| 180 | + const caption = button.querySelector('foxy-i18n'); |
| 181 | + expect(button).to.have.attribute('disabled'); |
| 182 | + expect(caption).to.have.attribute('key', 'busy'); |
| 183 | + |
| 184 | + await waitUntil(() => lastFetchEvent !== null); |
| 185 | + expect(lastFetchEvent?.request.url).to.equal('https://demo.api/virtual/empty'); |
| 186 | + expect(lastFetchEvent?.request.method).to.equal('POST'); |
| 187 | + |
| 188 | + await waitUntil(() => lastErrorEvent !== null); |
| 189 | + await control.requestUpdate(); |
| 190 | + expect(button).to.not.have.attribute('disabled'); |
| 191 | + expect(caption).to.have.attribute('key', 'idle'); |
| 192 | + }); |
| 193 | +}); |
0 commit comments