PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/test.js

http://github.com/beastridge/viewjs
JavaScript | 680 lines | 680 code | 0 blank | 0 comment | 4 complexity | 6f597ca62a2a8c6b9b88efb33bfe5ba3 MD5 | raw file
  1. (function() {
  2. var Backbone, Builder, RouteResolver, Router, View, array_from, assert, document, jQuery, jsdom, _ref;
  3. var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  4. assert = require('assert');
  5. jsdom = require('jsdom').jsdom;
  6. document = jsdom('<html><head></head><body></body></html>');
  7. global.window = document.createWindow();
  8. _ref = require(__dirname + '/../lib/view.js'), View = _ref.View, Builder = _ref.Builder, Router = _ref.Router, RouteResolver = _ref.RouteResolver;
  9. jQuery = require('jquery');
  10. Backbone = require('backbone');
  11. global.Backbone = Backbone;
  12. array_from = function(object) {
  13. var length, results;
  14. if (!object) {
  15. return [];
  16. }
  17. length = object.length || 0;
  18. results = new Array(length);
  19. while (length--) {
  20. results[length] = object[length];
  21. }
  22. return results;
  23. };
  24. View.extend({
  25. document: document
  26. });
  27. module.exports.parses = function() {
  28. return assert.ok(true);
  29. };
  30. module.exports.stack = function(before_exit) {
  31. var StackView, i, sequence;
  32. i = 0;
  33. sequence = [];
  34. StackView = View.create({
  35. StackView: {
  36. stack: {
  37. initialize: {
  38. add: function(next) {
  39. sequence.push('a');
  40. this.a = 'a';
  41. ++i;
  42. return next();
  43. }
  44. }
  45. }
  46. }
  47. }).StackView;
  48. StackView.extend({
  49. stack: {
  50. initialize: {
  51. add: function(next) {
  52. sequence.push('b');
  53. this.b = 'b';
  54. ++i;
  55. return next();
  56. }
  57. }
  58. }
  59. });
  60. return StackView.initialize(function() {
  61. return before_exit(function() {
  62. assert.equal(StackView._stack.initialize.stack.length, 3);
  63. assert.equal(2, i);
  64. assert.equal(StackView.a, 'a');
  65. assert.equal(StackView.b, 'b');
  66. assert.equal(sequence[0], 'a');
  67. return assert.equal(sequence[1], 'b');
  68. });
  69. });
  70. };
  71. module.exports.canDeferViewManagerCallback = function() {
  72. var call_count;
  73. call_count = 0;
  74. View({
  75. QuantumView: function() {
  76. return ++call_count;
  77. }
  78. });
  79. View.create({
  80. QuantumView: {}
  81. });
  82. return assert.equal(call_count, 1);
  83. };
  84. module.exports.canTriggerEvents = function() {
  85. var TestView, i;
  86. TestView = View.create({
  87. TestView: {}
  88. }).TestView;
  89. i = 0;
  90. TestView.bind('test', function() {
  91. return ++i;
  92. });
  93. TestView.trigger('test');
  94. return assert.equal(i, 1);
  95. };
  96. module.exports.canDetectModel = function(before_exit) {
  97. var ModelView, model, _model;
  98. _model = false;
  99. model = new Backbone.Model;
  100. model.set({
  101. key: 'value'
  102. });
  103. ModelView = View.create({
  104. ModelView: {
  105. model: model,
  106. initialize: function(next) {
  107. _model = this.model;
  108. return next();
  109. }
  110. }
  111. }).ModelView;
  112. return ModelView.initialize(function() {
  113. return before_exit(function() {
  114. assert.equal(ModelView.model, model);
  115. return assert.equal(_model, model);
  116. });
  117. });
  118. };
  119. module.exports.canDetectCollection = function(before_exit) {
  120. var CollectionView, collection, _collection;
  121. _collection = false;
  122. collection = new Backbone.Collection;
  123. CollectionView = View.create({
  124. CollectionView: {
  125. collection: collection,
  126. initialize: function(next) {
  127. _collection = this.collection;
  128. return next();
  129. }
  130. }
  131. }).CollectionView;
  132. return CollectionView.initialize(function() {
  133. return before_exit(function() {
  134. assert.equal(CollectionView.collection, collection);
  135. return assert.equal(CollectionView.collection, _collection);
  136. });
  137. });
  138. };
  139. module.exports.canRender = function() {
  140. var BuilderView;
  141. BuilderView = View.create({
  142. BuilderView: [
  143. Builder, {
  144. render: function() {
  145. return this.p('test');
  146. },
  147. on: {
  148. ready: function() {
  149. return assert.equal(this[0].firstChild.innerHTML, 'test');
  150. }
  151. }
  152. }
  153. ]
  154. }).BuilderView;
  155. return View({
  156. BuilderView: function() {
  157. return this.initialize();
  158. }
  159. });
  160. };
  161. module.exports.canRenderCollection = function() {
  162. var Item, List, ListView, contents, render_count;
  163. Item = Backbone.Model.extend();
  164. List = new (Backbone.Collection.extend({
  165. model: Item
  166. }));
  167. render_count = 0;
  168. contents = [
  169. {
  170. content: 'One'
  171. }, {
  172. content: 'Two'
  173. }, {
  174. content: 'Three'
  175. }
  176. ];
  177. List.add(array_from(contents));
  178. ListView = View.create({
  179. ListView: {
  180. collection: List,
  181. element: function() {
  182. return this.tag('ul');
  183. },
  184. render: function(item) {
  185. ++render_count;
  186. return this.tag('li', item.get('content'));
  187. }
  188. }
  189. }).ListView;
  190. return ListView.initialize(function() {
  191. assert.equal(this[0].tagName.toLowerCase(), 'ul');
  192. assert.equal(render_count, 3);
  193. assert.equal(ListView[0].childNodes.length, 3);
  194. assert.equal(ListView[0].firstChild.innerHTML, 'One');
  195. List.remove(List.at(0));
  196. assert.equal(render_count, 3);
  197. assert.equal(ListView[0].childNodes.length, 2);
  198. assert.equal(ListView[0].firstChild.innerHTML, 'Two');
  199. List.add({
  200. content: 'Four'
  201. });
  202. assert.equal(render_count, 4);
  203. assert.equal(ListView[0].childNodes.length, 3);
  204. assert.equal(ListView[0].firstChild.innerHTML, 'Two');
  205. assert.equal(ListView[0].childNodes[2].innerHTML, 'Four');
  206. List.refresh(contents);
  207. assert.equal(render_count, 7);
  208. assert.equal(ListView[0].childNodes.length, 3);
  209. assert.equal(ListView[0].firstChild.innerHTML, 'One');
  210. return assert.equal(ListView[0].childNodes[2].innerHTML, 'Three');
  211. });
  212. };
  213. module.exports.viewManager = function() {
  214. var TestView2, TestView3, _ref;
  215. View.create({
  216. TestView2: {
  217. key: 'value'
  218. }
  219. });
  220. View.create({
  221. TestView3: {
  222. key: 'value2'
  223. }
  224. });
  225. _ref = View(['TestView2', 'TestView3']), TestView2 = _ref[0], TestView3 = _ref[1];
  226. assert.equal(TestView2.key, 'value');
  227. return assert.equal(TestView3.key, 'value2');
  228. };
  229. module.exports.canUseCreateAsCallback = function() {
  230. var instance;
  231. instance = View.create(function() {
  232. return this.key = 'value';
  233. });
  234. return assert.equal(instance.key, 'value');
  235. };
  236. module.exports.canDepend = function() {
  237. var sequence;
  238. sequence = [];
  239. View.create({
  240. ParentView: {
  241. views: ['ChildView1', 'ChildView2', 'ChildView3'],
  242. initialize: function(next) {
  243. assert.equal(this.ChildView1.name, 'ChildView1');
  244. assert.equal(this.ChildView2.name, 'ChildView2');
  245. assert.equal(this.ChildView3.name, 'ChildView3');
  246. return next();
  247. }
  248. },
  249. ChildView1: {
  250. name: 'ChildView1',
  251. render: function() {
  252. sequence.push('a');
  253. return this.document.createElement('div');
  254. }
  255. },
  256. ChildView2: {
  257. name: 'ChildView2',
  258. render: function() {
  259. sequence.push('b');
  260. return this.document.createElement('div');
  261. }
  262. },
  263. ChildView3: {
  264. name: 'ChildView3',
  265. render: function() {
  266. sequence.push('c');
  267. return this.document.createElement('div');
  268. }
  269. }
  270. });
  271. return View({
  272. ParentView: function() {
  273. this.initialize();
  274. return this.on({
  275. ready: function() {
  276. assert.equal(sequence[0], 'a');
  277. assert.equal(sequence[1], 'b');
  278. return assert.equal(sequence[2], 'c');
  279. }
  280. });
  281. }
  282. });
  283. };
  284. module.exports.canDependAsync = function(before_exit) {
  285. View.create({
  286. AsyncParentView: [
  287. Builder, {
  288. views: ['AsyncChildView1', 'AsyncChildView2'],
  289. initialize: function(next) {
  290. return setTimeout(next, 50);
  291. },
  292. render: function() {
  293. return this.ul(this.AsyncChildView1, this.AsyncChildView2);
  294. }
  295. }
  296. ],
  297. AsyncChildView1: [
  298. Builder, {
  299. initialize: function(next) {
  300. return setTimeout(next, 1);
  301. },
  302. render: function() {
  303. return this.li('A');
  304. }
  305. }
  306. ],
  307. AsyncChildView2: [
  308. Builder, {
  309. initialize: function(next) {
  310. return setTimeout(next, 100);
  311. },
  312. render: function() {
  313. return this.li('B');
  314. }
  315. }
  316. ]
  317. });
  318. return View({
  319. AsyncParentView: function() {
  320. this.initialize();
  321. return this.on({
  322. ready: function() {
  323. return before_exit(__bind(function() {
  324. return assert.equal(jQuery('li:first', this[0]).html(), 'A');
  325. }, this));
  326. }
  327. });
  328. }
  329. });
  330. };
  331. module.exports.canPassViewsToBuilder = function() {
  332. var InnerView, OuterView;
  333. OuterView = View.create({
  334. OuterView: [
  335. Builder, {
  336. initialize: function(next) {
  337. InnerView.on({
  338. ready: next
  339. });
  340. return InnerView.initialize();
  341. },
  342. render: function() {
  343. return this.div(InnerView, {
  344. "class": 'test'
  345. });
  346. },
  347. on: {
  348. ready: function() {
  349. return assert.equal(this[0].firstChild.firstChild.firstChild.innerHTML, 'test');
  350. }
  351. }
  352. }
  353. ]
  354. }).OuterView;
  355. InnerView = View.create({
  356. InnerView: [
  357. Builder, {
  358. render: function() {
  359. return this.p('test');
  360. }
  361. }
  362. ]
  363. }).InnerView;
  364. return OuterView.initialize();
  365. };
  366. module.exports.canDiscardMixin = function() {
  367. var DiscardChildView, DiscardView;
  368. View.extend({
  369. extend: {
  370. discard: function(value, discard) {
  371. this.discard = value;
  372. return discard();
  373. }
  374. }
  375. });
  376. DiscardView = View.create({
  377. DiscardView: {}
  378. }).DiscardView;
  379. DiscardView.extend({
  380. discard: 'discard'
  381. });
  382. DiscardChildView = DiscardView.create({
  383. DiscardChildView: {}
  384. }).DiscardChildView;
  385. assert.equal(DiscardView.discard, 'discard');
  386. return assert.equal(typeof DiscardChildView.discard, 'undefined');
  387. };
  388. module.exports.canObserveKeyChanges = function() {
  389. var KeyChangeView, _a, _b, _c;
  390. _a = '';
  391. _b = '';
  392. _c = '';
  393. KeyChangeView = View.create({
  394. KeyChangeView: {
  395. on: {
  396. change: {
  397. a: function(a) {
  398. return _a = a;
  399. },
  400. b: function(b) {
  401. return _b = b;
  402. }
  403. }
  404. }
  405. }
  406. }).KeyChangeView;
  407. KeyChangeView.bind('change:c', function(c) {
  408. return _c = c;
  409. });
  410. KeyChangeView.set({
  411. a: 'a',
  412. b: 'b',
  413. c: 'c'
  414. });
  415. assert.equal(_a, 'a');
  416. assert.equal(_b, 'b');
  417. return assert.equal(_c, 'c');
  418. };
  419. module.exports.canHaveDefaults = function() {
  420. var DefaultsView;
  421. DefaultsView = View.create({
  422. DefaultsView: {
  423. defaults: {
  424. key: 'value'
  425. }
  426. }
  427. }).DefaultsView;
  428. assert.equal(DefaultsView.get('key'), 'value');
  429. return assert.equal(DefaultsView.create().get('key'), 'value');
  430. };
  431. module.exports.canUseArrayInBuilder = function(before_exit) {
  432. var ArrayBuilderViewA, ArrayBuilderViewB, ArrayBuilderViewC, _ref;
  433. _ref = View.create({
  434. ArrayBuilderViewA: {
  435. views: ['ArrayBuilderViewB', 'ArrayBuilderViewC'],
  436. render: function() {
  437. return this.tag('ul', [this.ArrayBuilderViewB, this.ArrayBuilderViewC]);
  438. }
  439. },
  440. ArrayBuilderViewB: {
  441. render: function() {
  442. return this.tag('li', 'b');
  443. }
  444. },
  445. ArrayBuilderViewC: {
  446. render: function() {
  447. return this.tag('li', 'c');
  448. }
  449. }
  450. }), ArrayBuilderViewA = _ref.ArrayBuilderViewA, ArrayBuilderViewB = _ref.ArrayBuilderViewB, ArrayBuilderViewC = _ref.ArrayBuilderViewC;
  451. return ArrayBuilderViewA.initialize(function() {
  452. return before_exit(__bind(function() {
  453. assert.ok(this[0].firstChild.firstChild != null);
  454. return assert.equal(this[0].firstChild.firstChild.firstChild.innerHTML, 'b');
  455. }, this));
  456. });
  457. };
  458. module.exports.router = function(before_exit) {
  459. var ContainerView, IndexView, PostView, callback_count, index_view_render_count, post_view_render_count, _ref;
  460. View.extend({
  461. routes: {
  462. '/': 'IndexView',
  463. '/post/:id?': 'PostView',
  464. '/:a/:b/:c?': 'AlphabetView'
  465. }
  466. });
  467. post_view_render_count = 0;
  468. index_view_render_count = 0;
  469. _ref = View.create({
  470. SidebarView: {
  471. render: function() {
  472. return this.tag('div', {
  473. "class": 'sidebar'
  474. });
  475. }
  476. },
  477. PostView: {
  478. on: {
  479. change: {
  480. id: function() {
  481. return this.render();
  482. }
  483. }
  484. },
  485. render: function() {
  486. ++post_view_render_count;
  487. return this.tag('div', 'post');
  488. }
  489. },
  490. IndexView: {
  491. render: function() {
  492. ++index_view_render_count;
  493. return this.tag('div', 'index');
  494. }
  495. },
  496. ContainerView: [
  497. Router, {
  498. views: ['SidebarView'],
  499. render: function() {
  500. var element;
  501. element = this.tag('div', this.SidebarView, this.tag('div', this.Router));
  502. return element;
  503. }
  504. }
  505. ],
  506. AlphabetView: {}
  507. }), PostView = _ref.PostView, IndexView = _ref.IndexView, ContainerView = _ref.ContainerView;
  508. assert.deepEqual('/post/5', RouteResolver({
  509. PostView: {
  510. id: 5
  511. }
  512. }));
  513. assert.deepEqual({
  514. PostView: {
  515. id: "5"
  516. }
  517. }, RouteResolver('/post/5'));
  518. assert.deepEqual('/', RouteResolver({
  519. IndexView: {}
  520. }));
  521. assert.deepEqual({
  522. IndexView: {}
  523. }, RouteResolver('/'));
  524. assert.deepEqual({
  525. AlphabetView: {
  526. a: 'a',
  527. b: 'b',
  528. c: 'c'
  529. }
  530. }, RouteResolver('/a/b/c'));
  531. assert.deepEqual({
  532. AlphabetView: {
  533. a: 'a',
  534. b: 'b',
  535. c: 'c'
  536. }
  537. }, RouteResolver('/a/b/c'));
  538. assert.deepEqual('/a/b/', RouteResolver({
  539. AlphabetView: {
  540. a: 'a',
  541. b: 'b'
  542. }
  543. }));
  544. assert.equal('/a/b/c', RouteResolver({
  545. AlphabetView: ['a', 'b', 'c']
  546. }));
  547. assert.equal('/post/5', PostView.url({
  548. id: 5
  549. }));
  550. assert.equal('/', IndexView.url());
  551. assert.equal('/post/5', IndexView.url({
  552. PostView: {
  553. id: 5
  554. }
  555. }));
  556. assert.equal('/', RouteResolver('IndexView'));
  557. callback_count = 0;
  558. return ContainerView.initialize(function() {
  559. return RouteResolver('/post/5', function(view, params) {
  560. assert.equal(view.get('id'), '5');
  561. assert.ok(PostView.element().style.display !== 'none');
  562. assert.ok(IndexView.element().style.display === 'none');
  563. ++callback_count;
  564. return RouteResolver({
  565. IndexView: {}
  566. }, function(view, params) {
  567. assert.ok(IndexView.element().style.display !== 'none');
  568. assert.ok(PostView.element().style.display === 'none');
  569. ++callback_count;
  570. return RouteResolver({
  571. PostView: ['4']
  572. }, function(view, params) {
  573. assert.equal(view.get('id'), '4');
  574. assert.ok(PostView.element().style.display !== 'none');
  575. assert.ok(IndexView.element().style.display === 'none');
  576. ++callback_count;
  577. return RouteResolver({
  578. IndexView: {}
  579. }, function(view, params) {
  580. assert.ok(IndexView.element().style.display !== 'none');
  581. assert.ok(PostView.element().style.display === 'none');
  582. ++callback_count;
  583. PostView.unbind('route');
  584. return RouteResolver({
  585. PostView: {
  586. id: 4
  587. }
  588. }, function(view, params) {
  589. assert.ok(IndexView.element().style.display === 'none');
  590. assert.ok(PostView.element().style.display !== 'none');
  591. ++callback_count;
  592. return before_exit(function() {
  593. assert.equal(5, callback_count);
  594. assert.equal(4, post_view_render_count);
  595. return assert.equal(1, index_view_render_count);
  596. });
  597. });
  598. });
  599. });
  600. });
  601. });
  602. });
  603. };
  604. module.exports.canPassDataInitialize = function(before_exit) {
  605. var attributes, collection, model, model_view;
  606. model = new Backbone.Model;
  607. collection = new Backbone.Collection;
  608. attributes = {
  609. key: 'value'
  610. };
  611. model_view = View.create();
  612. return model_view.initialize(model, function() {
  613. var collection_view;
  614. assert.equal(model_view.model, model);
  615. collection_view = View.create();
  616. return collection_view.initialize(collection, function() {
  617. var attributes_view;
  618. assert.equal(collection_view.collection, collection);
  619. attributes_view = View.create();
  620. attributes_view.initialize(attributes, function() {});
  621. return before_exit(function() {
  622. return assert.equal(attributes.key, attributes_view.get('key'));
  623. });
  624. });
  625. });
  626. };
  627. module.exports.canUse$InBuilder = function(before_exit) {
  628. var $BuilderView, click_count;
  629. click_count = 0;
  630. $BuilderView = View.create({
  631. $BuilderView: [
  632. Builder, {
  633. $: jQuery,
  634. delegate: {
  635. 'click div': function() {
  636. return ++click_count;
  637. },
  638. click: {
  639. div: function() {
  640. return ++click_count;
  641. }
  642. }
  643. },
  644. render: function() {
  645. this.key = 'test';
  646. return this.table(this.tr(this.td(), this.td(this.div().click(__bind(function() {
  647. ++click_count;
  648. return assert.equal(this.key, 'test');
  649. }, this)))));
  650. }
  651. }
  652. ]
  653. }).$BuilderView;
  654. return $BuilderView.initialize(function() {
  655. return before_exit(function() {
  656. $BuilderView.$('div').trigger('click');
  657. assert.equal(click_count, 3);
  658. return assert.equal($BuilderView.$('td').length, 2);
  659. });
  660. });
  661. };
  662. module.exports.canReverseLookup = function() {
  663. var ReverseLookupView;
  664. ReverseLookupView = View.create({
  665. ReverseLookupView: {
  666. $: jQuery
  667. }
  668. }).ReverseLookupView;
  669. return ReverseLookupView.initialize(function() {
  670. assert.equal(jQuery(this).view().name, 'ReverseLookupView');
  671. return assert.equal(this.$.view().name, 'ReverseLookupView');
  672. });
  673. };
  674. module.exports.anonViewHasElement = function() {
  675. var anon;
  676. anon = View.create();
  677. assert.ok(anon[0]);
  678. return assert.equal(anon[0].tagName, 'DIV');
  679. };
  680. }).call(this);