paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2017 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
Kamil Musin | ac4c188 | 2024-10-25 12:07:32 +0200 | [diff] [blame^] | 6 | import * as sinon from 'sinon'; |
Frank Borden | be9451a | 2022-09-12 15:44:29 +0200 | [diff] [blame] | 7 | import '../../../test/common-test-setup'; |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 8 | import '../../shared/gr-js-api-interface/gr-js-api-interface'; |
| 9 | import {GrPopupInterface} from './gr-popup-interface'; |
| 10 | import {PluginApi} from '../../../api/plugin'; |
| 11 | import {HookApi, PluginElement} from '../../../api/hook'; |
Frank Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 12 | import {queryAndAssert, waitEventLoop} from '../../../test/test-utils'; |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 13 | import {LitElement, html} from 'lit'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 14 | import {customElement} from 'lit/decorators.js'; |
Frank Borden | e1ba821 | 2022-08-29 15:20:01 +0200 | [diff] [blame] | 15 | import {fixture, assert} from '@open-wc/testing'; |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 16 | |
| 17 | @customElement('gr-user-test-popup') |
| 18 | class GrUserTestPopupElement extends LitElement { |
| 19 | override render() { |
| 20 | return html`<div id="barfoo">some test module</div>`; |
| 21 | } |
| 22 | } |
| 23 | declare global { |
| 24 | interface HTMLElementTagNameMap { |
| 25 | 'gr-user-test-popup': GrUserTestPopupElement; |
| 26 | } |
| 27 | } |
| 28 | |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 29 | suite('gr-popup-interface tests', () => { |
| 30 | let container: HTMLElement; |
| 31 | let instance: GrPopupInterface; |
| 32 | let plugin: PluginApi; |
| 33 | |
Frank Borden | dedd671 | 2022-08-22 10:56:11 +0200 | [diff] [blame] | 34 | setup(async () => { |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 35 | window.Gerrit.install( |
| 36 | p => { |
| 37 | plugin = p; |
| 38 | }, |
| 39 | '0.1', |
| 40 | 'http://test.com/plugins/testplugin/static/test.js' |
| 41 | ); |
Frank Borden | dedd671 | 2022-08-22 10:56:11 +0200 | [diff] [blame] | 42 | container = await fixture(html`<div></div>`); |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 43 | sinon.stub(plugin, 'hook').returns({ |
| 44 | getLastAttached() { |
| 45 | return Promise.resolve(container); |
| 46 | }, |
| 47 | } as HookApi<PluginElement>); |
| 48 | }); |
| 49 | |
| 50 | suite('manual', () => { |
| 51 | setup(async () => { |
| 52 | instance = new GrPopupInterface(plugin); |
| 53 | await instance.open(); |
| 54 | }); |
| 55 | |
| 56 | test('open', async () => { |
| 57 | const manual = document.createElement('div'); |
| 58 | manual.id = 'foobar'; |
| 59 | manual.innerHTML = 'manual content'; |
| 60 | const popup = instance._getElement(); |
| 61 | assert.isOk(popup); |
| 62 | popup!.appendChild(manual); |
Frank Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 63 | await waitEventLoop(); |
paladox | a5fc268 | 2022-03-05 00:45:45 +0000 | [diff] [blame] | 64 | assert.equal( |
| 65 | queryAndAssert(container, '#foobar').textContent, |
| 66 | 'manual content' |
| 67 | ); |
| 68 | }); |
| 69 | |
| 70 | test('close', async () => { |
| 71 | assert.isOk(instance._getElement()); |
| 72 | assert.isTrue(instance._getElement()!.opened); |
| 73 | instance.close(); |
| 74 | assert.isOk(instance._getElement()); |
| 75 | assert.isFalse(instance._getElement()!.opened); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | suite('components', () => { |
| 80 | setup(async () => { |
| 81 | instance = new GrPopupInterface(plugin, 'gr-user-test-popup'); |
| 82 | await instance.open(); |
| 83 | }); |
| 84 | |
| 85 | test('open', async () => { |
| 86 | assert.isNotNull(container.querySelector('gr-user-test-popup')); |
| 87 | }); |
| 88 | |
| 89 | test('close', async () => { |
| 90 | assert.isOk(instance._getElement()); |
| 91 | assert.isTrue(instance._getElement()!.opened); |
| 92 | instance.close(); |
| 93 | assert.isOk(instance._getElement()); |
| 94 | assert.isFalse(instance._getElement()!.opened); |
| 95 | }); |
| 96 | }); |
| 97 | }); |