|
1 | 1 | var Lib = require('@src/lib');
|
2 | 2 | var setCursor = require('@src/lib/setcursor');
|
3 | 3 | var overrideCursor = require('@src/lib/override_cursor');
|
| 4 | +var config = require('@src/plot_api/plot_config'); |
4 | 5 |
|
5 | 6 | var d3 = require('d3');
|
6 | 7 | var Plotly = require('@lib');
|
@@ -1599,6 +1600,172 @@ describe('Test lib.js:', function() {
|
1599 | 1600 | expect(Lib.isPlotDiv({})).toBe(false);
|
1600 | 1601 | });
|
1601 | 1602 | });
|
| 1603 | + |
| 1604 | + describe('isD3Selection', function() { |
| 1605 | + var gd; |
| 1606 | + |
| 1607 | + beforeEach(function() { |
| 1608 | + gd = createGraphDiv(); |
| 1609 | + }); |
| 1610 | + |
| 1611 | + afterEach(function() { |
| 1612 | + destroyGraphDiv(); |
| 1613 | + Plotly.setPlotConfig({ queueLength: 0 }); |
| 1614 | + }); |
| 1615 | + |
| 1616 | + it('recognizes real and duck typed selections', function() { |
| 1617 | + var yesSelections = [ |
| 1618 | + d3.select(gd), |
| 1619 | + // this is what got us into trouble actually - d3 selections can |
| 1620 | + // contain non-nodes - say for example d3 selections! then they |
| 1621 | + // don't work correctly. But it makes a convenient test! |
| 1622 | + d3.select(1), |
| 1623 | + // just showing what we actually do in this function: duck type |
| 1624 | + // using the `classed` method. |
| 1625 | + {classed: function(v) { return !!v; }} |
| 1626 | + ]; |
| 1627 | + |
| 1628 | + yesSelections.forEach(function(v) { |
| 1629 | + expect(Lib.isD3Selection(v)).toBe(true, v); |
| 1630 | + }); |
| 1631 | + }); |
| 1632 | + |
| 1633 | + it('rejects non-selections', function() { |
| 1634 | + var notSelections = [ |
| 1635 | + 1, |
| 1636 | + 'path', |
| 1637 | + [1, 2], |
| 1638 | + [[1, 2]], |
| 1639 | + {classed: 1}, |
| 1640 | + gd |
| 1641 | + ]; |
| 1642 | + |
| 1643 | + notSelections.forEach(function(v) { |
| 1644 | + expect(Lib.isD3Selection(v)).toBe(false, v); |
| 1645 | + }); |
| 1646 | + }); |
| 1647 | + }); |
| 1648 | + |
| 1649 | + describe('loggers', function() { |
| 1650 | + var stashConsole, |
| 1651 | + stashLogLevel; |
| 1652 | + |
| 1653 | + function consoleFn(name, hasApply, messages) { |
| 1654 | + var out = function() { |
| 1655 | + var args = []; |
| 1656 | + for(var i = 0; i < arguments.length; i++) args.push(arguments[i]); |
| 1657 | + messages.push([name, args]); |
| 1658 | + }; |
| 1659 | + |
| 1660 | + if(!hasApply) out.apply = undefined; |
| 1661 | + |
| 1662 | + return out; |
| 1663 | + } |
| 1664 | + |
| 1665 | + function mockConsole(hasApply, hasTrace) { |
| 1666 | + var out = { |
| 1667 | + MESSAGES: [] |
| 1668 | + }; |
| 1669 | + out.log = consoleFn('log', hasApply, out.MESSAGES); |
| 1670 | + out.error = consoleFn('error', hasApply, out.MESSAGES); |
| 1671 | + |
| 1672 | + if(hasTrace) out.trace = consoleFn('trace', hasApply, out.MESSAGES); |
| 1673 | + |
| 1674 | + return out; |
| 1675 | + } |
| 1676 | + |
| 1677 | + beforeEach(function() { |
| 1678 | + stashConsole = window.console; |
| 1679 | + stashLogLevel = config.logging; |
| 1680 | + }); |
| 1681 | + |
| 1682 | + afterEach(function() { |
| 1683 | + window.console = stashConsole; |
| 1684 | + config.logging = stashLogLevel; |
| 1685 | + }); |
| 1686 | + |
| 1687 | + it('emits one console message if apply is available', function() { |
| 1688 | + var c = window.console = mockConsole(true, true); |
| 1689 | + config.logging = 2; |
| 1690 | + |
| 1691 | + Lib.log('tick', 'tock', 'tick', 'tock', 1); |
| 1692 | + Lib.warn('I\'m', 'a', 'little', 'cuckoo', 'clock', [1, 2]); |
| 1693 | + Lib.error('cuckoo!', 'cuckoo!!!', {a: 1, b: 2}); |
| 1694 | + |
| 1695 | + expect(c.MESSAGES).toEqual([ |
| 1696 | + ['trace', ['LOG:', 'tick', 'tock', 'tick', 'tock', 1]], |
| 1697 | + ['trace', ['WARN:', 'I\'m', 'a', 'little', 'cuckoo', 'clock', [1, 2]]], |
| 1698 | + ['error', ['ERROR:', 'cuckoo!', 'cuckoo!!!', {a: 1, b: 2}]] |
| 1699 | + ]); |
| 1700 | + }); |
| 1701 | + |
| 1702 | + it('falls back on console.log if no trace', function() { |
| 1703 | + var c = window.console = mockConsole(true, false); |
| 1704 | + config.logging = 2; |
| 1705 | + |
| 1706 | + Lib.log('Hi'); |
| 1707 | + Lib.warn(42); |
| 1708 | + |
| 1709 | + expect(c.MESSAGES).toEqual([ |
| 1710 | + ['log', ['LOG:', 'Hi']], |
| 1711 | + ['log', ['WARN:', 42]] |
| 1712 | + ]); |
| 1713 | + }); |
| 1714 | + |
| 1715 | + it('falls back on separate calls if no apply', function() { |
| 1716 | + var c = window.console = mockConsole(false, false); |
| 1717 | + config.logging = 2; |
| 1718 | + |
| 1719 | + Lib.log('tick', 'tock', 'tick', 'tock', 1); |
| 1720 | + Lib.warn('I\'m', 'a', 'little', 'cuckoo', 'clock', [1, 2]); |
| 1721 | + Lib.error('cuckoo!', 'cuckoo!!!', {a: 1, b: 2}); |
| 1722 | + |
| 1723 | + expect(c.MESSAGES).toEqual([ |
| 1724 | + ['log', ['LOG:']], |
| 1725 | + ['log', ['tick']], |
| 1726 | + ['log', ['tock']], |
| 1727 | + ['log', ['tick']], |
| 1728 | + ['log', ['tock']], |
| 1729 | + ['log', [1]], |
| 1730 | + ['log', ['WARN:']], |
| 1731 | + ['log', ['I\'m']], |
| 1732 | + ['log', ['a']], |
| 1733 | + ['log', ['little']], |
| 1734 | + ['log', ['cuckoo']], |
| 1735 | + ['log', ['clock']], |
| 1736 | + ['log', [[1, 2]]], |
| 1737 | + ['error', ['ERROR:']], |
| 1738 | + ['error', ['cuckoo!']], |
| 1739 | + ['error', ['cuckoo!!!']], |
| 1740 | + ['error', [{a: 1, b: 2}]] |
| 1741 | + ]); |
| 1742 | + }); |
| 1743 | + |
| 1744 | + it('omits .log at log level 1', function() { |
| 1745 | + var c = window.console = mockConsole(true, true); |
| 1746 | + config.logging = 1; |
| 1747 | + |
| 1748 | + Lib.log(1); |
| 1749 | + Lib.warn(2); |
| 1750 | + Lib.error(3); |
| 1751 | + |
| 1752 | + expect(c.MESSAGES).toEqual([ |
| 1753 | + ['trace', ['WARN:', 2]], |
| 1754 | + ['error', ['ERROR:', 3]] |
| 1755 | + ]); |
| 1756 | + }); |
| 1757 | + |
| 1758 | + it('logs nothing at log level 0', function() { |
| 1759 | + var c = window.console = mockConsole(true, true); |
| 1760 | + config.logging = 0; |
| 1761 | + |
| 1762 | + Lib.log(1); |
| 1763 | + Lib.warn(2); |
| 1764 | + Lib.error(3); |
| 1765 | + |
| 1766 | + expect(c.MESSAGES).toEqual([]); |
| 1767 | + }); |
| 1768 | + }); |
1602 | 1769 | });
|
1603 | 1770 |
|
1604 | 1771 | describe('Queue', function() {
|
|
0 commit comments