Skip to content

Commit 12aeb9d

Browse files
committed
tests for IE9 fixes
1 parent ebc1a10 commit 12aeb9d

File tree

2 files changed

+184
-5
lines changed

2 files changed

+184
-5
lines changed

tasks/test_syntax.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var bundleTestGlob = path.join(constants.pathToJasmineBundleTests, '**/*.js');
1313

1414
// main
1515
assertJasmineSuites();
16-
assertHeaders();
16+
assertSrcContents();
1717
assertFileNames();
1818
assertCircularDeps();
1919

@@ -44,8 +44,12 @@ function assertJasmineSuites() {
4444
});
4545
}
4646

47-
// check for header in src and lib files
48-
function assertHeaders() {
47+
/*
48+
* tests about the contents of source (and lib) files:
49+
* - check for header comment
50+
* - check that we don't have .classList
51+
*/
52+
function assertSrcContents() {
4953
var licenseSrc = constants.licenseSrc;
5054
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
5155
var logs = [];
@@ -56,7 +60,15 @@ function assertHeaders() {
5660

5761
// parse through code string while keeping track of comments
5862
var comments = [];
59-
falafel(code, {onComment: comments, locations: true}, function() {});
63+
falafel(code, {onComment: comments, locations: true}, function(node) {
64+
// look for .classList
65+
if(node.type === 'MemberExpression') {
66+
var parts = node.source().split('.');
67+
if(parts[parts.length - 1] === 'classList') {
68+
logs.push(file + ' : contains .classList (IE failure)');
69+
}
70+
}
71+
});
6072

6173
var header = comments[0];
6274

@@ -70,7 +82,7 @@ function assertHeaders() {
7082
}
7183
});
7284

73-
log('correct headers in lib/ and src/', logs);
85+
log('correct headers and contents in lib/ and src/', logs);
7486
});
7587
}
7688

test/jasmine/tests/lib_test.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Lib = require('@src/lib');
22
var setCursor = require('@src/lib/setcursor');
33
var overrideCursor = require('@src/lib/override_cursor');
4+
var config = require('@src/plot_api/plot_config');
45

56
var d3 = require('d3');
67
var Plotly = require('@lib');
@@ -1599,6 +1600,172 @@ describe('Test lib.js:', function() {
15991600
expect(Lib.isPlotDiv({})).toBe(false);
16001601
});
16011602
});
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+
});
16021769
});
16031770

16041771
describe('Queue', function() {

0 commit comments

Comments
 (0)