Files
polymer/test/unit/logging.html
2018-04-19 18:19:39 -07:00

106 lines
3.4 KiB
HTML

<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="utf-8">
<script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
<script src="wct-browser-config.js"></script>
<script src="../../node_modules/wct-browser-legacy/browser.js"></script>
<script type="module" src="../../polymer-legacy.js"></script>
</head>
<body>
<dom-module id="x-logging">
<script type="module">
import { Polymer } from '../../polymer-legacy.js';
Polymer({
is: 'x-logging',
properties: {
stream: {
value: () => [],
type: Array
}
},
_logger(level, args) {
this.stream.push({level, args});
}
});
</script>
</dom-module>
<script type="module">
import { LegacyElementMixin } from '../../lib/legacy/legacy-element-mixin.js';
class XMixin extends LegacyElementMixin(HTMLElement) {
static get is() { return 'x-mixin'; }
static get properties() {
return {
stream: {
value: () => [],
type: Array
}
};
}
_logger(level, args) {
this.stream.push({level, args});
}
}
customElements.define(XMixin.is, XMixin);
</script>
<script type="module">
import { Base } from '../../polymer-legacy.js';
suite('Logging', () => {
let fnEl, classEl;
suiteSetup(() => {
fnEl = document.createElement('x-logging');
document.body.appendChild(fnEl);
classEl = document.createElement('x-mixin');
document.body.appendChild(classEl);
});
test('API', () => {
['_log', '_warn', '_error', '_logf', '_logger'].forEach((n) => {
assert.isFunction(fnEl[n], `${n} not defined on Polymer() element`);
assert.isFunction(classEl[n], `${n} not defined on Polymer.Element element`);
});
});
test('_log, _warn_, and _error go through _logger', () => {
fnEl._log('fn');
classEl._log('class');
fnEl._warn('fn');
classEl._warn('class');
fnEl._error('fn');
classEl._error('class');
assert.deepEqual(fnEl.stream, [{level: 'log', args: ['fn']}, {level: 'warn', args: ['fn']}, {level: 'error', args: ['fn']}]);
assert.deepEqual(classEl.stream, [{level: 'log', args: ['class']}, {level: 'warn', args: ['class']}, {level: 'error', args: ['class']}]);
});
test('_logf', () => {
let args = ['hi', 'there'];
let output = fnEl._logf(...args);
assert.oneOf(fnEl.is, output, `${fnEl.is} should be in ${output}`);
assert.includeMembers(output, args, `${args} should be in ${output}`);
});
});
suite('_logger', () => {
test('_log with single parameter', () => {
const msg = 'log test';
const orgConsole = console;
const spy = sinon.spy();
window.console = {
log: spy
};
Base._log(msg);
window.console = orgConsole;
assert.equal(spy.callCount, 1, 'console.log called more than one time');
assert.equal(spy.args[0].length, 1, 'console.log called with more than one argument');
assert.equal(spy.args[0][0], msg, 'message not pass properly to log');
});
});
</script>
</body>
</html>