Files
gerrit/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context_test.html
Ben Rohlfs 80d6fdc7cd Add tracking to buttons
Example for when you click the "reply" button:
Reporting: button-click: main gr-change-view div#maincontent section
div#mainchangeinfo div#commitandrelated div div gr-button#replybtn

Change-Id: I0ce4f574e76b8d9c9c1f4fd87db026d86895f97f
2020-01-24 08:43:20 +01:00

159 lines
4.8 KiB
HTML

<!DOCTYPE html>
<!--
@license
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-plugin-action-context</title>
<script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<script src="/bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-js-api-interface.html"/>
<script>void(0);</script>
<test-fixture id="basic">
<template>
<div></div>
</template>
</test-fixture>
<script>
suite('gr-plugin-action-context tests', () => {
let instance;
let sandbox;
let plugin;
setup(() => {
sandbox = sinon.sandbox.create();
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
instance = new GrPluginActionContext(plugin);
});
teardown(() => {
sandbox.restore();
});
test('popup() and hide()', () => {
const popupApiStub = {
close: sandbox.stub(),
};
sandbox.stub(plugin.deprecated, 'popup').returns(popupApiStub);
const el = {};
instance.popup(el);
assert.isTrue(instance.plugin.deprecated.popup.calledWith(el));
instance.hide();
assert.isTrue(popupApiStub.close.called);
});
test('textfield', () => {
assert.equal(instance.textfield().tagName, 'PAPER-INPUT');
});
test('br', () => {
assert.equal(instance.br().tagName, 'BR');
});
test('msg', () => {
const el = instance.msg('foobar');
assert.equal(el.tagName, 'GR-LABEL');
assert.equal(el.textContent, 'foobar');
});
test('div', () => {
const el1 = document.createElement('span');
el1.textContent = 'foo';
const el2 = document.createElement('div');
el2.textContent = 'bar';
const div = instance.div(el1, el2);
assert.equal(div.tagName, 'DIV');
assert.equal(div.textContent, 'foobar');
});
test('button', done => {
const clickStub = sandbox.stub();
const button = instance.button('foo', {onclick: clickStub});
// If you don't attach a Polymer element to the DOM, then the ready()
// callback will not be called and then e.g. this.$ is undefined.
Polymer.dom(document.body).appendChild(button);
MockInteractions.tap(button);
flush(() => {
assert.isTrue(clickStub.called);
assert.equal(button.textContent, 'foo');
done();
});
});
test('checkbox', () => {
const el = instance.checkbox();
assert.equal(el.tagName, 'INPUT');
assert.equal(el.type, 'checkbox');
});
test('label', () => {
const fakeMsg = {};
const fakeCheckbox = {};
sandbox.stub(instance, 'div');
sandbox.stub(instance, 'msg').returns(fakeMsg);
instance.label(fakeCheckbox, 'foo');
assert.isTrue(instance.div.calledWithExactly(fakeCheckbox, fakeMsg));
});
test('call', () => {
instance.action = {
method: 'METHOD',
__key: 'key',
__url: '/changes/1/revisions/2/foo~bar',
};
const sendStub = sandbox.stub().returns(Promise.resolve());
sandbox.stub(plugin, 'restApi').returns({
send: sendStub,
});
const payload = {foo: 'foo'};
const successStub = sandbox.stub();
instance.call(payload, successStub);
assert.isTrue(sendStub.calledWith(
'METHOD', '/changes/1/revisions/2/foo~bar', payload));
});
test('call error', done => {
instance.action = {
method: 'METHOD',
__key: 'key',
__url: '/changes/1/revisions/2/foo~bar',
};
const sendStub = sandbox.stub().returns(Promise.reject(new Error('boom')));
sandbox.stub(plugin, 'restApi').returns({
send: sendStub,
});
const errorStub = sandbox.stub();
document.addEventListener('show-alert', errorStub);
instance.call();
flush(() => {
assert.isTrue(errorStub.calledOnce);
assert.equal(errorStub.args[0][0].detail.message,
'Plugin network error: Error: boom');
done();
});
});
});
</script>