blob: e6c710056e403a66508c94f982b1302888847fda [file] [log] [blame]
paladoxa5fc2682022-03-05 00:45:45 +00001/**
2 * @license
3 * Copyright 2017 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
5 */
Kamil Musinac4c1882024-10-25 12:07:32 +02006import * as sinon from 'sinon';
Frank Bordenbe9451a2022-09-12 15:44:29 +02007import '../../../test/common-test-setup';
paladoxa5fc2682022-03-05 00:45:45 +00008import '../../shared/gr-js-api-interface/gr-js-api-interface';
9import {GrPopupInterface} from './gr-popup-interface';
10import {PluginApi} from '../../../api/plugin';
11import {HookApi, PluginElement} from '../../../api/hook';
Frank Borden7b24f732022-09-12 14:29:32 +020012import {queryAndAssert, waitEventLoop} from '../../../test/test-utils';
paladoxa5fc2682022-03-05 00:45:45 +000013import {LitElement, html} from 'lit';
Frank Borden42c1a452022-08-11 16:27:20 +020014import {customElement} from 'lit/decorators.js';
Frank Bordene1ba8212022-08-29 15:20:01 +020015import {fixture, assert} from '@open-wc/testing';
paladoxa5fc2682022-03-05 00:45:45 +000016
17@customElement('gr-user-test-popup')
18class GrUserTestPopupElement extends LitElement {
19 override render() {
20 return html`<div id="barfoo">some test module</div>`;
21 }
22}
23declare global {
24 interface HTMLElementTagNameMap {
25 'gr-user-test-popup': GrUserTestPopupElement;
26 }
27}
28
paladoxa5fc2682022-03-05 00:45:45 +000029suite('gr-popup-interface tests', () => {
30 let container: HTMLElement;
31 let instance: GrPopupInterface;
32 let plugin: PluginApi;
33
Frank Bordendedd6712022-08-22 10:56:11 +020034 setup(async () => {
paladoxa5fc2682022-03-05 00:45:45 +000035 window.Gerrit.install(
36 p => {
37 plugin = p;
38 },
39 '0.1',
40 'http://test.com/plugins/testplugin/static/test.js'
41 );
Frank Bordendedd6712022-08-22 10:56:11 +020042 container = await fixture(html`<div></div>`);
paladoxa5fc2682022-03-05 00:45:45 +000043 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 Borden7b24f732022-09-12 14:29:32 +020063 await waitEventLoop();
paladoxa5fc2682022-03-05 00:45:45 +000064 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});