Axis.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. topSuite("Ext.chart.axis.Axis",
  2. ['Ext.chart.*', 'Ext.data.ArrayStore', 'Ext.app.ViewController',
  3. 'Ext.Container', 'Ext.layout.Fit'],
  4. function() {
  5. beforeEach(function() {
  6. // Tons of warnings regarding Sencha download server
  7. spyOn(Ext.log, 'warn');
  8. });
  9. describe('getRange', function () {
  10. it("linked axes should always return the range of the master axis", function () {
  11. var chartConfig = {
  12. renderTo: Ext.getBody(),
  13. width: 400,
  14. height: 200,
  15. store: {
  16. fields: ['name', 'value'],
  17. data: [{
  18. name: 'one',
  19. value: 10
  20. }, {
  21. name: 'two',
  22. value: 7
  23. }, {
  24. name: 'three',
  25. value: 5
  26. }, {
  27. name: 'four',
  28. value: 2
  29. }, {
  30. name: 'five',
  31. value: 27
  32. }]
  33. },
  34. series: {
  35. type: 'bar',
  36. xField: 'name',
  37. yField: 'value'
  38. }
  39. },
  40. verticalNumeric = Ext.Object.merge({}, chartConfig, {
  41. axes: [
  42. {
  43. id: 'left',
  44. type: 'numeric',
  45. position: 'left'
  46. },
  47. {
  48. id: 'bottom',
  49. type: 'category',
  50. position: 'bottom'
  51. },
  52. {
  53. position: 'right',
  54. linkedTo: 'left'
  55. },
  56. {
  57. position: 'top',
  58. linkedTo: 'bottom'
  59. }
  60. ]
  61. }),
  62. horizontalNumeric = Ext.Object.merge({}, chartConfig, {
  63. flipXY: true,
  64. axes: [
  65. {
  66. id: 'left',
  67. type: 'category',
  68. position: 'left'
  69. },
  70. {
  71. id: 'bottom',
  72. type: 'numeric',
  73. position: 'bottom'
  74. },
  75. {
  76. position: 'top',
  77. linkedTo: 'bottom'
  78. },
  79. {
  80. position: 'right',
  81. linkedTo: 'left'
  82. }
  83. ]
  84. });
  85. var axisProto = Ext.chart.axis.Axis.prototype,
  86. originalGetRange = axisProto.getRange;
  87. function getRange() {
  88. var range = originalGetRange.apply(this, arguments),
  89. masterAxis = this.masterAxis;
  90. if (range && masterAxis) {
  91. expect(range[0]).toEqual(masterAxis.range[0]);
  92. expect(range[1]).toEqual(masterAxis.range[1]);
  93. }
  94. return range;
  95. }
  96. axisProto.getRange = getRange;
  97. var verticalNumericChart = new Ext.chart.CartesianChart(verticalNumeric);
  98. verticalNumericChart.performLayout();
  99. verticalNumericChart.destroy();
  100. var horizontalNumericChart = new Ext.chart.CartesianChart(horizontalNumeric);
  101. horizontalNumericChart.performLayout();
  102. horizontalNumericChart.destroy();
  103. axisProto.getRange = originalGetRange;
  104. });
  105. });
  106. describe('adjustByMajorUnit', function () {
  107. var chart;
  108. afterEach(function () {
  109. Ext.destroy(chart);
  110. });
  111. it('should round the axis range to nice values', function () {
  112. var layoutDone;
  113. chart = new Ext.chart.CartesianChart({
  114. renderTo: Ext.getBody(),
  115. width: 400,
  116. height: 400,
  117. store: {
  118. data: [
  119. { year: 1890, men: 1002, women: 988},
  120. { year: 1900, men: 1007, women: 999},
  121. { year: 1910, men: 1056, women: 1043},
  122. { year: 1920, men: 1077, women: 1044},
  123. { year: 1930, men: 1099, women: 1082},
  124. { year: 1940, men: 1125, women: 1098},
  125. { year: 1950, men: 885, women: 1076}
  126. ]
  127. },
  128. axes: [
  129. {
  130. id: 'left',
  131. type: 'numeric',
  132. position: 'left',
  133. grid: true
  134. },
  135. {
  136. type: 'category',
  137. position: 'bottom'
  138. }
  139. ],
  140. series: [
  141. {
  142. stacked: false,
  143. type: 'bar',
  144. xField: 'year',
  145. yField: ['men','women']
  146. }
  147. ],
  148. listeners: {
  149. layout: function () {
  150. layoutDone = true;
  151. }
  152. }
  153. });
  154. waitsFor(function () {
  155. return layoutDone;
  156. });
  157. runs(function () {
  158. var leftAxis = chart.getAxis('left'),
  159. attr = leftAxis.getSprites()[0].attr;
  160. expect(attr.max).toBe(1200);
  161. expect(attr.dataMax).toBe(1200); // TODO: One would expect this to be 1125.
  162. });
  163. });
  164. });
  165. describe('resolveListenerScope', function () {
  166. var testScope;
  167. function setTestScope() {
  168. testScope = this;
  169. }
  170. var scopeObject = {
  171. setTestScope: setTestScope
  172. };
  173. var store = Ext.create('Ext.data.Store', {
  174. fields: ['x', 'y'],
  175. data: [
  176. {x: 0, y: 0},
  177. {x: 1, y: 1}
  178. ]
  179. });
  180. var axisConfig = {
  181. type: 'numeric',
  182. position: 'bottom'
  183. };
  184. function createContainer(options) {
  185. var config = {
  186. width: 400,
  187. height: 400,
  188. layout: 'fit'
  189. };
  190. Ext.apply(config, options);
  191. var container = Ext.create('Ext.container.Container', config);
  192. container.setTestScope = setTestScope;
  193. return container;
  194. }
  195. function createController() {
  196. return Ext.create('Ext.app.ViewController', {
  197. setTestScope: setTestScope
  198. });
  199. }
  200. function createChart(options) {
  201. var config = {
  202. store: store,
  203. axes: axisConfig
  204. };
  205. Ext.apply(config, options);
  206. var chart = Ext.create('Ext.chart.CartesianChart', config);
  207. chart.setTestScope = setTestScope;
  208. return chart;
  209. }
  210. function createAxisClass(listenerScope) {
  211. return Ext.define(null, {
  212. extend: 'Ext.chart.axis.Numeric',
  213. setTestScope: setTestScope,
  214. listeners: {
  215. test: {
  216. fn: 'setTestScope',
  217. scope: listenerScope
  218. }
  219. }
  220. });
  221. }
  222. describe('axis instance listener', function () {
  223. describe('no chart controller, chart container controller', function () {
  224. var chart, axis,
  225. container, containerController;
  226. beforeEach(function () {
  227. testScope = undefined;
  228. containerController = createController();
  229. chart = createChart();
  230. container = createContainer({
  231. controller: containerController
  232. });
  233. container.add(chart);
  234. axis = chart.getAxes()[0];
  235. axis.setTestScope = setTestScope;
  236. });
  237. afterEach(function () {
  238. chart.destroy();
  239. container.destroy();
  240. });
  241. it("listener scoped to 'this' should refer to the axis", function () {
  242. axis.on({
  243. test: 'setTestScope',
  244. scope: 'this'
  245. });
  246. axis.fireEvent('test', axis);
  247. expect(testScope).toBe(axis);
  248. });
  249. it("listener scoped to an arbitrary object should refer to that object", function () {
  250. axis.on({
  251. test: 'setTestScope',
  252. scope: scopeObject
  253. });
  254. axis.fireEvent('test', axis);
  255. expect(testScope).toBe(scopeObject);
  256. });
  257. it("listener scoped to 'controller' should refer to chart container controller", function () {
  258. axis.on({
  259. test: 'setTestScope',
  260. scope: 'controller'
  261. });
  262. axis.fireEvent('test', axis);
  263. expect(testScope).toBe(containerController);
  264. });
  265. it("listener with no explicit scope should be scoped to chart container controller", function () {
  266. axis.on('test', 'setTestScope');
  267. axis.fireEvent('test', axis);
  268. expect(testScope).toBe(containerController);
  269. });
  270. });
  271. describe('chart controller, no chart container controller', function () {
  272. var chart, axis,
  273. container, chartController;
  274. beforeEach(function () {
  275. testScope = undefined;
  276. chartController = createController();
  277. chart = createChart({
  278. controller: chartController
  279. });
  280. container = createContainer();
  281. container.add(chart);
  282. axis = chart.getAxes()[0];
  283. axis.setTestScope = setTestScope;
  284. });
  285. afterEach(function () {
  286. chart.destroy();
  287. container.destroy();
  288. });
  289. it("listener scoped to 'this' should refer to the axis", function () {
  290. axis.on({
  291. test: 'setTestScope',
  292. scope: 'this'
  293. });
  294. axis.fireEvent('test', axis);
  295. expect(testScope).toBe(axis);
  296. });
  297. it("listener scoped to an arbitrary object should refer to that object", function () {
  298. axis.on({
  299. test: 'setTestScope',
  300. scope: scopeObject
  301. });
  302. axis.fireEvent('test', axis);
  303. expect(testScope).toBe(scopeObject);
  304. });
  305. it("listener scoped to 'controller' should refer to chart controller", function () {
  306. axis.on({
  307. test: 'setTestScope',
  308. scope: 'controller'
  309. });
  310. axis.fireEvent('test', axis);
  311. expect(testScope).toBe(chartController);
  312. });
  313. it("listener with no explicit scope should be scoped to chart controller", function () {
  314. axis.on('test', 'setTestScope');
  315. axis.fireEvent('test', axis);
  316. expect(testScope).toBe(chartController);
  317. });
  318. });
  319. describe('chart controller, chart container controller', function () {
  320. var chart, container, axis,
  321. chartController,
  322. containerController;
  323. beforeEach(function () {
  324. testScope = undefined;
  325. chartController = createController();
  326. containerController = createController();
  327. chart = createChart({
  328. controller: chartController
  329. });
  330. container = createContainer({
  331. controller: containerController
  332. });
  333. container.add(chart);
  334. axis = chart.getAxes()[0];
  335. axis.setTestScope = setTestScope;
  336. });
  337. afterEach(function () {
  338. chart.destroy();
  339. container.destroy();
  340. });
  341. it("listener scoped to 'this' should refer to the axis", function () {
  342. axis.on({
  343. test: 'setTestScope',
  344. scope: 'this'
  345. });
  346. axis.fireEvent('test', axis);
  347. expect(testScope).toBe(axis);
  348. });
  349. it("listener scoped to an arbitrary object should refer to that object", function () {
  350. axis.on({
  351. test: 'setTestScope',
  352. scope: scopeObject
  353. });
  354. axis.fireEvent('test', axis);
  355. expect(testScope).toBe(scopeObject);
  356. });
  357. it("listener scoped to 'controller' should refer to chart controller", function () {
  358. axis.on({
  359. test: 'setTestScope',
  360. scope: 'controller'
  361. });
  362. axis.fireEvent('test', axis);
  363. expect(testScope).toBe(chartController);
  364. });
  365. it("listener with no explicit scope should be scoped to chart controller", function () {
  366. axis.on('test', 'setTestScope');
  367. axis.fireEvent('test', axis);
  368. expect(testScope).toBe(chartController);
  369. });
  370. });
  371. describe('no chart controller, no chart container controller', function () {
  372. var chart, axis, container;
  373. beforeEach(function () {
  374. testScope = undefined;
  375. chart = createChart();
  376. container = createContainer();
  377. container.add(chart);
  378. axis = chart.getAxes()[0];
  379. axis.setTestScope = setTestScope;
  380. });
  381. afterEach(function () {
  382. chart.destroy();
  383. container.destroy();
  384. });
  385. it("listener scoped to 'this' should refer to the axis", function () {
  386. axis.on({
  387. test: 'setTestScope',
  388. scope: 'this'
  389. });
  390. axis.fireEvent('test', axis);
  391. expect(testScope).toBe(axis);
  392. });
  393. it("listener scoped to an arbitrary object should refer to that object", function () {
  394. axis.on({
  395. test: 'setTestScope',
  396. scope: scopeObject
  397. });
  398. axis.fireEvent('test', axis);
  399. expect(testScope).toBe(scopeObject);
  400. });
  401. it("listener scoped to 'controller' should fail", function () {
  402. axis.on({
  403. test: 'setTestScope',
  404. scope: 'controller'
  405. });
  406. expect(function () {
  407. axis.fireEvent('test', axis);
  408. }).toThrow();
  409. });
  410. it("listener with no explicit scope should be scoped to the chart", function () {
  411. axis.on('test', 'setTestScope');
  412. axis.fireEvent('test', axis);
  413. expect(testScope).toBe(chart);
  414. });
  415. });
  416. describe('chart inside container with defaultListenerScope: true (no controllers)', function () {
  417. var chart, axis, container;
  418. beforeEach(function () {
  419. testScope = undefined;
  420. chart = createChart();
  421. container = createContainer({
  422. defaultListenerScope: true
  423. });
  424. container.add(chart);
  425. axis = chart.getAxes()[0];
  426. axis.setTestScope = setTestScope;
  427. });
  428. afterEach(function () {
  429. chart.destroy();
  430. container.destroy();
  431. });
  432. it("listener scoped to 'this' should refer to the axis", function () {
  433. axis.on({
  434. test: 'setTestScope',
  435. scope: 'this'
  436. });
  437. axis.fireEvent('test', axis);
  438. expect(testScope).toBe(axis);
  439. });
  440. it("listener scoped to an arbitrary object should refer to that object", function () {
  441. axis.on({
  442. test: 'setTestScope',
  443. scope: scopeObject
  444. });
  445. axis.fireEvent('test', axis);
  446. expect(testScope).toBe(scopeObject);
  447. });
  448. it("listener scoped to 'controller' should fail", function () {
  449. axis.on({
  450. test: 'setTestScope',
  451. scope: 'controller'
  452. });
  453. expect(function () {
  454. axis.fireEvent('test', axis);
  455. }).toThrow();
  456. });
  457. it("listener with no explicit scope should be scoped to the container", function () {
  458. axis.on('test', 'setTestScope');
  459. axis.fireEvent('test', axis);
  460. expect(testScope).toBe(container);
  461. });
  462. });
  463. describe('chart with a controller and defaultListenerScope: true', function () {
  464. var chart, axis, chartController;
  465. beforeEach(function () {
  466. testScope = undefined;
  467. chartController = createController();
  468. chart = createChart({
  469. controller: chartController,
  470. defaultListenerScope: true
  471. });
  472. axis = chart.getAxes()[0];
  473. axis.setTestScope = setTestScope;
  474. });
  475. afterEach(function () {
  476. chart.destroy();
  477. });
  478. it("listener scoped to 'this' should refer to the axis", function () {
  479. axis.on({
  480. test: 'setTestScope',
  481. scope: 'this'
  482. });
  483. axis.fireEvent('test', axis);
  484. expect(testScope).toBe(axis);
  485. });
  486. it("listener scoped to an arbitrary object should refer to that object", function () {
  487. axis.on({
  488. test: 'setTestScope',
  489. scope: scopeObject
  490. });
  491. axis.fireEvent('test', axis);
  492. expect(testScope).toBe(scopeObject);
  493. });
  494. it("listener scoped to 'controller' should refer to the chart controller", function () {
  495. axis.on({
  496. test: 'setTestScope',
  497. scope: 'controller'
  498. });
  499. axis.fireEvent('test', axis);
  500. expect(testScope).toBe(chartController);
  501. });
  502. it("listener with no explicit scope should be scoped to the chart", function () {
  503. axis.on('test', 'setTestScope');
  504. axis.fireEvent('test', axis);
  505. expect(testScope).toBe(chart);
  506. });
  507. });
  508. describe('chart with a controller', function () {
  509. var chart, axis, chartController;
  510. beforeEach(function () {
  511. testScope = undefined;
  512. chartController = createController();
  513. chart = createChart({
  514. controller: chartController
  515. });
  516. axis = chart.getAxes()[0];
  517. axis.setTestScope = setTestScope;
  518. });
  519. afterEach(function () {
  520. chart.destroy();
  521. });
  522. it("listener scoped to 'this' should refer to the axis", function () {
  523. axis.on({
  524. test: 'setTestScope',
  525. scope: 'this'
  526. });
  527. axis.fireEvent('test', axis);
  528. expect(testScope).toBe(axis);
  529. });
  530. it("listener scoped to an arbitrary object should refer to that object", function () {
  531. axis.on({
  532. test: 'setTestScope',
  533. scope: scopeObject
  534. });
  535. axis.fireEvent('test', axis);
  536. expect(testScope).toBe(scopeObject);
  537. });
  538. it("listener scoped to 'controller' should refer to the chart controller", function () {
  539. axis.on({
  540. test: 'setTestScope',
  541. scope: 'controller'
  542. });
  543. axis.fireEvent('test', axis);
  544. expect(testScope).toBe(chartController);
  545. });
  546. it("listener with no explicit scope should be scoped to the chart controller", function () {
  547. axis.on('test', 'setTestScope');
  548. axis.fireEvent('test', axis);
  549. expect(testScope).toBe(chartController);
  550. });
  551. });
  552. describe('chart with defaultListenerScope: true (container, no controllers)', function () {
  553. var chart, container, axis, chartController;
  554. beforeEach(function () {
  555. testScope = undefined;
  556. chartController = createController();
  557. chart = createChart({
  558. controller: chartController,
  559. defaultListenerScope: true
  560. });
  561. container = createContainer();
  562. container.add(chart);
  563. axis = chart.getAxes()[0];
  564. axis.setTestScope = setTestScope;
  565. });
  566. afterEach(function () {
  567. chart.destroy();
  568. container.destroy();
  569. });
  570. it("listener scoped to 'this' should refer to the axis", function () {
  571. axis.on({
  572. test: 'setTestScope',
  573. scope: 'this'
  574. });
  575. axis.fireEvent('test', axis);
  576. expect(testScope).toBe(axis);
  577. });
  578. it("listener scoped to an arbitrary object should refer to that object", function () {
  579. axis.on({
  580. test: 'setTestScope',
  581. scope: scopeObject
  582. });
  583. axis.fireEvent('test', axis);
  584. expect(testScope).toBe(scopeObject);
  585. });
  586. it("listener scoped to 'controller' should refer to the chart controller", function () {
  587. axis.on({
  588. test: 'setTestScope',
  589. scope: 'controller'
  590. });
  591. axis.fireEvent('test', axis);
  592. expect(testScope).toBe(chartController);
  593. });
  594. it("listener with no explicit scope should be scoped to the chart", function () {
  595. axis.on('test', 'setTestScope');
  596. axis.fireEvent('test', axis);
  597. expect(testScope).toBe(chart);
  598. });
  599. });
  600. });
  601. // #######################################################################################
  602. describe('axis class listener', function () {
  603. describe('no chart controller, chart container controller', function () {
  604. var chart, axis,
  605. container, containerController;
  606. beforeEach(function () {
  607. testScope = undefined;
  608. containerController = createController();
  609. chart = createChart({
  610. axes: []
  611. });
  612. container = createContainer({
  613. controller: containerController
  614. });
  615. container.add(chart);
  616. });
  617. afterEach(function () {
  618. chart.destroy();
  619. container.destroy();
  620. });
  621. it("listener scoped to 'this' should refer to the axis", function () {
  622. axis = new (createAxisClass('this'))();
  623. chart.setAxes(axis);
  624. axis.fireEvent('test', axis);
  625. expect(testScope).toBe(axis);
  626. });
  627. it("listener scoped to an arbitrary object should refer to that object", function () {
  628. axis = new (createAxisClass(scopeObject))();
  629. chart.setAxes(axis);
  630. axis.fireEvent('test', axis);
  631. expect(testScope).toBe(scopeObject);
  632. });
  633. it("listener scoped to 'controller' should refer to chart container controller", function () {
  634. axis = new (createAxisClass('controller'))();
  635. chart.setAxes(axis);
  636. axis.fireEvent('test', axis);
  637. expect(testScope).toBe(containerController);
  638. });
  639. it("listener with no explicit scope should be scoped to chart container controller", function () {
  640. axis = new (createAxisClass())();
  641. chart.setAxes(axis);
  642. axis.fireEvent('test', axis);
  643. expect(testScope).toBe(containerController);
  644. });
  645. });
  646. describe('chart controller, no chart container controller', function () {
  647. var chart, axis,
  648. container, chartController;
  649. beforeEach(function () {
  650. testScope = undefined;
  651. chartController = createController();
  652. chart = createChart({
  653. axes: [],
  654. controller: chartController
  655. });
  656. container = createContainer();
  657. container.add(chart);
  658. });
  659. afterEach(function () {
  660. chart.destroy();
  661. container.destroy();
  662. });
  663. it("listener scoped to 'this' should refer to the axis", function () {
  664. axis = new (createAxisClass('this'))();
  665. chart.setAxes(axis);
  666. axis.fireEvent('test', axis);
  667. expect(testScope).toBe(axis);
  668. });
  669. it("listener scoped to an arbitrary object should refer to that object", function () {
  670. axis = new (createAxisClass(scopeObject))();
  671. chart.setAxes(axis);
  672. axis.fireEvent('test', axis);
  673. expect(testScope).toBe(scopeObject);
  674. });
  675. it("listener scoped to 'controller' should refer to chart controller", function () {
  676. axis = new (createAxisClass('controller'))();
  677. chart.setAxes(axis);
  678. axis.fireEvent('test', axis);
  679. expect(testScope).toBe(chartController);
  680. });
  681. it("listener with no explicit scope should be scoped to chart controller", function () {
  682. axis = new (createAxisClass())();
  683. chart.setAxes(axis);
  684. axis.fireEvent('test', axis);
  685. expect(testScope).toBe(chartController);
  686. });
  687. });
  688. describe('chart controller, chart container controller', function () {
  689. var chart, container, axis,
  690. chartController,
  691. containerController;
  692. beforeEach(function () {
  693. testScope = undefined;
  694. chartController = createController();
  695. containerController = createController();
  696. chart = createChart({
  697. axes: [],
  698. controller: chartController
  699. });
  700. container = createContainer({
  701. controller: containerController
  702. });
  703. container.add(chart);
  704. });
  705. afterEach(function () {
  706. chart.destroy();
  707. container.destroy();
  708. });
  709. it("listener scoped to 'this' should refer to the axis", function () {
  710. axis = new (createAxisClass('this'))();
  711. chart.setAxes(axis);
  712. axis.fireEvent('test', axis);
  713. expect(testScope).toBe(axis);
  714. });
  715. it("listener scoped to an arbitrary object should refer to that object", function () {
  716. axis = new (createAxisClass(scopeObject))();
  717. chart.setAxes(axis);
  718. axis.fireEvent('test', axis);
  719. expect(testScope).toBe(scopeObject);
  720. });
  721. it("listener scoped to 'controller' should refer to chart controller", function () {
  722. axis = new (createAxisClass('controller'))();
  723. chart.setAxes(axis);
  724. axis.fireEvent('test', axis);
  725. expect(testScope).toBe(chartController);
  726. });
  727. it("listener with no explicit scope should be scoped to chart controller", function () {
  728. axis = new (createAxisClass())();
  729. chart.setAxes(axis);
  730. axis.fireEvent('test', axis);
  731. expect(testScope).toBe(chartController);
  732. });
  733. });
  734. describe('no chart controller, no chart container controller', function () {
  735. var chart, axis, container;
  736. beforeEach(function () {
  737. testScope = undefined;
  738. chart = createChart({
  739. axes: []
  740. });
  741. container = createContainer();
  742. container.add(chart);
  743. });
  744. afterEach(function () {
  745. chart.destroy();
  746. container.destroy();
  747. });
  748. it("listener scoped to 'this' should refer to the axis", function () {
  749. axis = new (createAxisClass('this'))();
  750. chart.setAxes(axis);
  751. axis.fireEvent('test', axis);
  752. expect(testScope).toBe(axis);
  753. });
  754. it("listener scoped to an arbitrary object should refer to that object", function () {
  755. axis = new (createAxisClass(scopeObject))();
  756. chart.setAxes(axis);
  757. axis.fireEvent('test', axis);
  758. expect(testScope).toBe(scopeObject);
  759. });
  760. it("listener scoped to 'controller' should fail", function () {
  761. axis = new (createAxisClass('controller'))();
  762. chart.setAxes(axis);
  763. expect(function () {
  764. axis.fireEvent('test', axis);
  765. }).toThrow();
  766. });
  767. it("listener with no explicit scope should be scoped to the axis", function () {
  768. axis = new (createAxisClass())();
  769. chart.setAxes(axis);
  770. axis.fireEvent('test', axis);
  771. expect(testScope).toBe(axis);
  772. });
  773. });
  774. describe('chart inside container with defaultListenerScope: true (no controllers)', function () {
  775. var chart, axis, container;
  776. beforeEach(function () {
  777. testScope = undefined;
  778. chart = createChart({
  779. axes: []
  780. });
  781. container = createContainer({
  782. defaultListenerScope: true
  783. });
  784. container.add(chart);
  785. });
  786. afterEach(function () {
  787. chart.destroy();
  788. container.destroy();
  789. });
  790. it("listener scoped to 'this' should refer to the axis", function () {
  791. axis = new (createAxisClass('this'))();
  792. chart.setAxes(axis);
  793. axis.fireEvent('test', axis);
  794. expect(testScope).toBe(axis);
  795. });
  796. it("listener scoped to an arbitrary object should refer to that object", function () {
  797. axis = new (createAxisClass(scopeObject))();
  798. chart.setAxes(axis);
  799. axis.fireEvent('test', axis);
  800. expect(testScope).toBe(scopeObject);
  801. });
  802. it("listener scoped to 'controller' should fail", function () {
  803. axis = new (createAxisClass('controller'))();
  804. chart.setAxes(axis);
  805. expect(function () {
  806. axis.fireEvent('test', axis);
  807. }).toThrow();
  808. });
  809. it("listener with no explicit scope should be scoped to chart container", function () {
  810. axis = new (createAxisClass())();
  811. chart.setAxes(axis);
  812. axis.fireEvent('test', axis);
  813. expect(testScope).toBe(container);
  814. });
  815. });
  816. describe('chart with a controller and defaultListenerScope: true', function () {
  817. var chart, axis, chartController;
  818. beforeEach(function () {
  819. testScope = undefined;
  820. chartController = createController();
  821. chart = createChart({
  822. axes: [],
  823. controller: chartController,
  824. defaultListenerScope: true
  825. });
  826. });
  827. afterEach(function () {
  828. chart.destroy();
  829. });
  830. it("listener scoped to 'this' should refer to the axis", function () {
  831. axis = new (createAxisClass('this'))();
  832. chart.setAxes(axis);
  833. axis.fireEvent('test', axis);
  834. expect(testScope).toBe(axis);
  835. });
  836. it("listener scoped to an arbitrary object should refer to that object", function () {
  837. axis = new (createAxisClass(scopeObject))();
  838. chart.setAxes(axis);
  839. axis.fireEvent('test', axis);
  840. expect(testScope).toBe(scopeObject);
  841. });
  842. it("listener scoped to 'controller' should refer to chart controller", function () {
  843. axis = new (createAxisClass('controller'))();
  844. chart.setAxes(axis);
  845. axis.fireEvent('test', axis);
  846. expect(testScope).toBe(chartController);
  847. });
  848. it("listener with no explicit scope should be scoped to chart", function () {
  849. axis = new (createAxisClass())();
  850. chart.setAxes(axis);
  851. axis.fireEvent('test', axis);
  852. expect(testScope).toBe(chart);
  853. });
  854. });
  855. describe('chart with a controller (no container)', function () {
  856. var chart, axis, chartController;
  857. beforeEach(function () {
  858. testScope = undefined;
  859. chartController = createController();
  860. chart = createChart({
  861. axes: [],
  862. controller: chartController
  863. });
  864. });
  865. afterEach(function () {
  866. chart.destroy();
  867. });
  868. it("listener scoped to 'this' should refer to the axis", function () {
  869. axis = new (createAxisClass('this'))();
  870. chart.setAxes(axis);
  871. axis.fireEvent('test', axis);
  872. expect(testScope).toBe(axis);
  873. });
  874. it("listener scoped to an arbitrary object should refer to that object", function () {
  875. axis = new (createAxisClass(scopeObject))();
  876. chart.setAxes(axis);
  877. axis.fireEvent('test', axis);
  878. expect(testScope).toBe(scopeObject);
  879. });
  880. it("listener scoped to 'controller' should refer to chart controller", function () {
  881. axis = new (createAxisClass('controller'))();
  882. chart.setAxes(axis);
  883. axis.fireEvent('test', axis);
  884. expect(testScope).toBe(chartController);
  885. });
  886. it("listener with no explicit scope should be scoped to chart controller", function () {
  887. axis = new (createAxisClass())();
  888. chart.setAxes(axis);
  889. axis.fireEvent('test', axis);
  890. expect(testScope).toBe(chartController);
  891. });
  892. });
  893. describe('chart with defaultListenerScope: true (container, no controllers)', function () {
  894. var chart, container, axis, chartController;
  895. beforeEach(function () {
  896. testScope = undefined;
  897. chartController = createController();
  898. chart = createChart({
  899. axes: [],
  900. controller: chartController,
  901. defaultListenerScope: true
  902. });
  903. container = createContainer();
  904. container.add(chart);
  905. });
  906. afterEach(function () {
  907. chart.destroy();
  908. container.destroy();
  909. });
  910. it("listener scoped to 'this' should refer to the axis", function () {
  911. axis = new (createAxisClass('this'))();
  912. chart.setAxes(axis);
  913. axis.fireEvent('test', axis);
  914. expect(testScope).toBe(axis);
  915. });
  916. it("listener scoped to an arbitrary object should refer to that object", function () {
  917. axis = new (createAxisClass(scopeObject))();
  918. chart.setAxes(axis);
  919. axis.fireEvent('test', axis);
  920. expect(testScope).toBe(scopeObject);
  921. });
  922. it("listener scoped to 'controller' should refer to chart controller", function () {
  923. axis = new (createAxisClass('controller'))();
  924. chart.setAxes(axis);
  925. axis.fireEvent('test', axis);
  926. expect(testScope).toBe(chartController);
  927. });
  928. it("listener with no explicit scope should be scoped to chart", function () {
  929. axis = new (createAxisClass())();
  930. chart.setAxes(axis);
  931. axis.fireEvent('test', axis);
  932. expect(testScope).toBe(chart);
  933. });
  934. });
  935. })
  936. });
  937. });