PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/test.js

https://github.com/setdirection/viewjs
JavaScript | 1056 lines | 1056 code | 0 blank | 0 comment | 12 complexity | a183d233d7c1a73a06890c9e8de74024 MD5 | raw file
  1. (function() {
  2. var Backbone, Builder, RouteResolver, Router, TestServer, View, ViewServer, array_from, assert, file_exists, fs, http, jQuery, jsdom, public, test_stack, _ref;
  3. var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  4. assert = require('assert');
  5. _ref = require('./view.client.js'), View = _ref.View, Builder = _ref.Builder, Router = _ref.Router, RouteResolver = _ref.RouteResolver;
  6. jsdom = require('jsdom').jsdom;
  7. jQuery = require('jquery');
  8. Backbone = require('backbone');
  9. global.Backbone = Backbone;
  10. array_from = function(object) {
  11. var length, results;
  12. if (!object) {
  13. return [];
  14. }
  15. length = object.length || 0;
  16. results = new Array(length);
  17. while (length--) {
  18. results[length] = object[length];
  19. }
  20. return results;
  21. };
  22. View.extend({
  23. env: {
  24. set: {
  25. test: true
  26. }
  27. },
  28. document: jsdom('<html><head></head><body></body></html>')
  29. });
  30. module.exports.parses = function() {
  31. return assert.ok(true);
  32. };
  33. module.exports.stack = function(before_exit) {
  34. var StackView, i, sequence;
  35. i = 0;
  36. sequence = [];
  37. StackView = View.create({
  38. StackView: {
  39. stack: {
  40. initialize: {
  41. add: function(next) {
  42. sequence.push('a');
  43. this.a = 'a';
  44. ++i;
  45. return next();
  46. }
  47. }
  48. }
  49. }
  50. }).StackView;
  51. StackView.extend({
  52. stack: {
  53. initialize: {
  54. add: function(next) {
  55. sequence.push('b');
  56. this.b = 'b';
  57. ++i;
  58. return next();
  59. }
  60. }
  61. }
  62. });
  63. return StackView.initialize(function() {
  64. return before_exit(function() {
  65. assert.equal(StackView._stack.initialize.stack.length, 3);
  66. assert.equal(2, i);
  67. assert.equal(StackView.a, 'a');
  68. assert.equal(StackView.b, 'b');
  69. assert.equal(sequence[0], 'a');
  70. return assert.equal(sequence[1], 'b');
  71. });
  72. });
  73. };
  74. module.exports.envBasics = function() {
  75. var call_count;
  76. View.env({
  77. set: {
  78. a: false,
  79. b: function() {
  80. return true;
  81. }
  82. }
  83. });
  84. call_count = 0;
  85. View.env({
  86. a: function() {
  87. return ++call_count;
  88. },
  89. b: function() {
  90. return ++call_count;
  91. }
  92. });
  93. assert.equal(call_count, 1);
  94. View.extend({
  95. env: {
  96. set: {
  97. a: function() {
  98. return true;
  99. },
  100. b: false
  101. }
  102. }
  103. });
  104. View.env({
  105. a: function() {
  106. return ++call_count;
  107. }
  108. });
  109. assert.equal(call_count, 2);
  110. View.env({
  111. b: function() {
  112. return ++call_count;
  113. }
  114. });
  115. assert.equal(call_count, 2);
  116. View.env({
  117. c: function() {
  118. return ++call_count;
  119. }
  120. });
  121. return assert.equal(call_count, 2);
  122. };
  123. module.exports.canDeferViewManagerCallback = function() {
  124. var call_count;
  125. call_count = 0;
  126. View({
  127. QuantumView: function() {
  128. return ++call_count;
  129. }
  130. });
  131. View.create({
  132. QuantumView: {}
  133. });
  134. return assert.equal(call_count, 1);
  135. };
  136. module.exports.canTriggerEvents = function() {
  137. var TestView, i;
  138. TestView = View.create({
  139. TestView: {}
  140. }).TestView;
  141. i = 0;
  142. TestView.bind('test', function() {
  143. return ++i;
  144. });
  145. TestView.trigger('test');
  146. return assert.equal(i, 1);
  147. };
  148. module.exports.canDetectModel = function(before_exit) {
  149. var ModelView, model, _model;
  150. _model = false;
  151. model = new Backbone.Model;
  152. model.set({
  153. key: 'value'
  154. });
  155. ModelView = View.create({
  156. ModelView: {
  157. model: model,
  158. initialize: function(next) {
  159. _model = this.model;
  160. return next();
  161. }
  162. }
  163. }).ModelView;
  164. return ModelView.initialize(function() {
  165. return before_exit(function() {
  166. assert.equal(ModelView.model, model);
  167. return assert.equal(_model, model);
  168. });
  169. });
  170. };
  171. module.exports.canDetectCollection = function(before_exit) {
  172. var CollectionView, collection, _collection;
  173. _collection = false;
  174. collection = new Backbone.Collection;
  175. CollectionView = View.create({
  176. CollectionView: {
  177. collection: collection,
  178. initialize: function(next) {
  179. _collection = this.collection;
  180. return next();
  181. }
  182. }
  183. }).CollectionView;
  184. return CollectionView.initialize(function() {
  185. return before_exit(function() {
  186. assert.equal(CollectionView.collection, collection);
  187. return assert.equal(CollectionView.collection, _collection);
  188. });
  189. });
  190. };
  191. module.exports.canRender = function() {
  192. var BuilderView;
  193. BuilderView = View.create({
  194. BuilderView: [
  195. Builder, {
  196. render: function() {
  197. return this.p('test');
  198. },
  199. on: {
  200. ready: function() {
  201. return assert.equal(this[0].firstChild.innerHTML, 'test');
  202. }
  203. }
  204. }
  205. ]
  206. }).BuilderView;
  207. return View({
  208. BuilderView: function() {
  209. return this.initialize();
  210. }
  211. });
  212. };
  213. module.exports.canRenderCollection = function() {
  214. var Item, List, ListView, contents, render_count;
  215. Item = Backbone.Model.extend();
  216. List = new (Backbone.Collection.extend({
  217. model: Item
  218. }));
  219. render_count = 0;
  220. contents = [
  221. {
  222. content: 'One'
  223. }, {
  224. content: 'Two'
  225. }, {
  226. content: 'Three'
  227. }
  228. ];
  229. List.add(array_from(contents));
  230. ListView = View.create({
  231. ListView: {
  232. collection: List,
  233. element: function() {
  234. return this.tag('ul');
  235. },
  236. render: function(item) {
  237. ++render_count;
  238. return this.tag('li', item.get('content'));
  239. }
  240. }
  241. }).ListView;
  242. return ListView.initialize(function() {
  243. assert.equal(this[0].tagName.toLowerCase(), 'ul');
  244. assert.equal(render_count, 3);
  245. assert.equal(ListView[0].childNodes.length, 3);
  246. assert.equal(ListView[0].firstChild.innerHTML, 'One');
  247. List.remove(List.at(0));
  248. assert.equal(render_count, 3);
  249. assert.equal(ListView[0].childNodes.length, 2);
  250. assert.equal(ListView[0].firstChild.innerHTML, 'Two');
  251. List.add({
  252. content: 'Four'
  253. });
  254. assert.equal(render_count, 4);
  255. assert.equal(ListView[0].childNodes.length, 3);
  256. assert.equal(ListView[0].firstChild.innerHTML, 'Two');
  257. assert.equal(ListView[0].childNodes[2].innerHTML, 'Four');
  258. List.refresh(contents);
  259. assert.equal(render_count, 7);
  260. assert.equal(ListView[0].childNodes.length, 3);
  261. assert.equal(ListView[0].firstChild.innerHTML, 'One');
  262. return assert.equal(ListView[0].childNodes[2].innerHTML, 'Three');
  263. });
  264. };
  265. module.exports.viewManager = function() {
  266. var TestView2, TestView3, _ref;
  267. View.create({
  268. TestView2: {
  269. key: 'value'
  270. }
  271. });
  272. View.create({
  273. TestView3: {
  274. key: 'value2'
  275. }
  276. });
  277. _ref = View(['TestView2', 'TestView3']), TestView2 = _ref[0], TestView3 = _ref[1];
  278. assert.equal(TestView2.key, 'value');
  279. return assert.equal(TestView3.key, 'value2');
  280. };
  281. module.exports.canUseCreateAsCallback = function() {
  282. var instance;
  283. instance = View.create(function() {
  284. return this.key = 'value';
  285. });
  286. return assert.equal(instance.key, 'value');
  287. };
  288. module.exports.canDepend = function() {
  289. var sequence;
  290. sequence = [];
  291. View.create({
  292. ParentView: {
  293. views: ['ChildView1', 'ChildView2', 'ChildView3'],
  294. initialize: function(next) {
  295. assert.equal(this.ChildView1.name, 'ChildView1');
  296. assert.equal(this.ChildView2.name, 'ChildView2');
  297. assert.equal(this.ChildView3.name, 'ChildView3');
  298. return next();
  299. }
  300. },
  301. ChildView1: {
  302. name: 'ChildView1',
  303. render: function() {
  304. sequence.push('a');
  305. return this.document.createElement('div');
  306. }
  307. },
  308. ChildView2: {
  309. name: 'ChildView2',
  310. render: function() {
  311. sequence.push('b');
  312. return this.document.createElement('div');
  313. }
  314. },
  315. ChildView3: {
  316. name: 'ChildView3',
  317. render: function() {
  318. sequence.push('c');
  319. return this.document.createElement('div');
  320. }
  321. }
  322. });
  323. return View({
  324. ParentView: function() {
  325. this.initialize();
  326. return this.on({
  327. ready: function() {
  328. assert.equal(sequence[0], 'a');
  329. assert.equal(sequence[1], 'b');
  330. return assert.equal(sequence[2], 'c');
  331. }
  332. });
  333. }
  334. });
  335. };
  336. module.exports.canDependAsync = function(before_exit) {
  337. View.create({
  338. AsyncParentView: [
  339. Builder, {
  340. views: ['AsyncChildView1', 'AsyncChildView2'],
  341. initialize: function(next) {
  342. return setTimeout(next, 50);
  343. },
  344. render: function() {
  345. return this.ul(this.AsyncChildView1, this.AsyncChildView2);
  346. }
  347. }
  348. ],
  349. AsyncChildView1: [
  350. Builder, {
  351. initialize: function(next) {
  352. return setTimeout(next, 1);
  353. },
  354. render: function() {
  355. return this.li('A');
  356. }
  357. }
  358. ],
  359. AsyncChildView2: [
  360. Builder, {
  361. initialize: function(next) {
  362. return setTimeout(next, 100);
  363. },
  364. render: function() {
  365. return this.li('B');
  366. }
  367. }
  368. ]
  369. });
  370. return View({
  371. AsyncParentView: function() {
  372. this.initialize();
  373. return this.on({
  374. ready: function() {
  375. return before_exit(__bind(function() {
  376. return assert.equal(jQuery('li:first', this[0]).html(), 'A');
  377. }, this));
  378. }
  379. });
  380. }
  381. });
  382. };
  383. module.exports.canPassViewsToBuilder = function() {
  384. var InnerView, OuterView;
  385. OuterView = View.create({
  386. OuterView: [
  387. Builder, {
  388. initialize: function(next) {
  389. InnerView.on({
  390. ready: next
  391. });
  392. return InnerView.initialize();
  393. },
  394. render: function() {
  395. return this.div(InnerView, {
  396. "class": 'test'
  397. });
  398. },
  399. on: {
  400. ready: function() {
  401. return assert.equal(this[0].firstChild.firstChild.firstChild.innerHTML, 'test');
  402. }
  403. }
  404. }
  405. ]
  406. }).OuterView;
  407. InnerView = View.create({
  408. InnerView: [
  409. Builder, {
  410. render: function() {
  411. return this.p('test');
  412. }
  413. }
  414. ]
  415. }).InnerView;
  416. return OuterView.initialize();
  417. };
  418. module.exports.canDiscardMixin = function() {
  419. var DiscardChildView, DiscardView;
  420. View.extend({
  421. extend: {
  422. discard: function(value, discard) {
  423. this.discard = value;
  424. return discard();
  425. }
  426. }
  427. });
  428. DiscardView = View.create({
  429. DiscardView: {}
  430. }).DiscardView;
  431. DiscardView.extend({
  432. discard: 'discard'
  433. });
  434. DiscardChildView = DiscardView.create({
  435. DiscardChildView: {}
  436. }).DiscardChildView;
  437. assert.equal(DiscardView.discard, 'discard');
  438. return assert.equal(typeof DiscardChildView.discard, 'undefined');
  439. };
  440. module.exports.canObserveKeyChanges = function() {
  441. var KeyChangeView, _a, _b, _c;
  442. _a = '';
  443. _b = '';
  444. _c = '';
  445. KeyChangeView = View.create({
  446. KeyChangeView: {
  447. on: {
  448. change: {
  449. a: function(a) {
  450. return _a = a;
  451. },
  452. b: function(b) {
  453. return _b = b;
  454. }
  455. }
  456. }
  457. }
  458. }).KeyChangeView;
  459. KeyChangeView.bind('change:c', function(c) {
  460. return _c = c;
  461. });
  462. KeyChangeView.set({
  463. a: 'a',
  464. b: 'b',
  465. c: 'c'
  466. });
  467. assert.equal(_a, 'a');
  468. assert.equal(_b, 'b');
  469. return assert.equal(_c, 'c');
  470. };
  471. module.exports.canHaveDefaults = function() {
  472. var DefaultsView;
  473. DefaultsView = View.create({
  474. DefaultsView: {
  475. defaults: {
  476. key: 'value'
  477. }
  478. }
  479. }).DefaultsView;
  480. assert.equal(DefaultsView.get('key'), 'value');
  481. return assert.equal(DefaultsView.create().get('key'), 'value');
  482. };
  483. module.exports.canUseArrayInBuilder = function(before_exit) {
  484. var ArrayBuilderViewA, ArrayBuilderViewB, ArrayBuilderViewC, _ref;
  485. _ref = View.create({
  486. ArrayBuilderViewA: {
  487. views: ['ArrayBuilderViewB', 'ArrayBuilderViewC'],
  488. render: function() {
  489. return this.tag('ul', [this.ArrayBuilderViewB, this.ArrayBuilderViewC]);
  490. }
  491. },
  492. ArrayBuilderViewB: {
  493. render: function() {
  494. return this.tag('li', 'b');
  495. }
  496. },
  497. ArrayBuilderViewC: {
  498. render: function() {
  499. return this.tag('li', 'c');
  500. }
  501. }
  502. }), ArrayBuilderViewA = _ref.ArrayBuilderViewA, ArrayBuilderViewB = _ref.ArrayBuilderViewB, ArrayBuilderViewC = _ref.ArrayBuilderViewC;
  503. return ArrayBuilderViewA.initialize(function() {
  504. return before_exit(__bind(function() {
  505. assert.ok(this[0].firstChild.firstChild != null);
  506. return assert.equal(this[0].firstChild.firstChild.firstChild.innerHTML, 'b');
  507. }, this));
  508. });
  509. };
  510. module.exports.router = function(before_exit) {
  511. var ContainerView, IndexView, PostView, callback_count, index_view_render_count, post_view_render_count, _ref;
  512. View.extend({
  513. routes: {
  514. '/': 'IndexView',
  515. '/post/:id?': 'PostView',
  516. '/:a/:b/:c?': 'AlphabetView'
  517. }
  518. });
  519. View.extend({
  520. env: {
  521. set: {
  522. server: function() {
  523. return false;
  524. },
  525. browser: function() {
  526. return false;
  527. }
  528. }
  529. }
  530. });
  531. post_view_render_count = 0;
  532. index_view_render_count = 0;
  533. _ref = View.create({
  534. SidebarView: {
  535. render: function() {
  536. return this.tag('div', {
  537. "class": 'sidebar'
  538. });
  539. }
  540. },
  541. PostView: {
  542. on: {
  543. change: {
  544. id: function() {
  545. return this.render();
  546. }
  547. }
  548. },
  549. render: function() {
  550. ++post_view_render_count;
  551. return this.tag('div', 'post');
  552. }
  553. },
  554. IndexView: {
  555. render: function() {
  556. ++index_view_render_count;
  557. return this.tag('div', 'index');
  558. }
  559. },
  560. ContainerView: [
  561. Router, {
  562. views: ['SidebarView'],
  563. render: function() {
  564. var element;
  565. element = this.tag('div', this.SidebarView, this.tag('div', this.Router));
  566. return element;
  567. }
  568. }
  569. ],
  570. AlphabetView: {}
  571. }), PostView = _ref.PostView, IndexView = _ref.IndexView, ContainerView = _ref.ContainerView;
  572. assert.deepEqual('/post/5', RouteResolver({
  573. PostView: {
  574. id: 5
  575. }
  576. }));
  577. assert.deepEqual({
  578. PostView: {
  579. id: "5"
  580. }
  581. }, RouteResolver('/post/5'));
  582. assert.deepEqual('/', RouteResolver({
  583. IndexView: {}
  584. }));
  585. assert.deepEqual({
  586. IndexView: {}
  587. }, RouteResolver('/'));
  588. assert.deepEqual({
  589. AlphabetView: {
  590. a: 'a',
  591. b: 'b',
  592. c: 'c'
  593. }
  594. }, RouteResolver('/a/b/c'));
  595. assert.deepEqual({
  596. AlphabetView: {
  597. a: 'a',
  598. b: 'b',
  599. c: 'c'
  600. }
  601. }, RouteResolver('/a/b/c'));
  602. assert.deepEqual('/a/b/', RouteResolver({
  603. AlphabetView: {
  604. a: 'a',
  605. b: 'b'
  606. }
  607. }));
  608. assert.equal('/a/b/c', RouteResolver({
  609. AlphabetView: ['a', 'b', 'c']
  610. }));
  611. assert.equal('/post/5', PostView.url({
  612. id: 5
  613. }));
  614. assert.equal('/', IndexView.url());
  615. assert.equal('/post/5', IndexView.url({
  616. PostView: {
  617. id: 5
  618. }
  619. }));
  620. assert.equal('/', RouteResolver('IndexView'));
  621. callback_count = 0;
  622. ContainerView.initialize(function() {
  623. return RouteResolver('/post/5', function(view, params) {
  624. assert.equal(view.get('id'), '5');
  625. assert.ok(PostView.element().style.display !== 'none');
  626. assert.ok(IndexView.element().style.display === 'none');
  627. ++callback_count;
  628. return RouteResolver({
  629. IndexView: {}
  630. }, function(view, params) {
  631. assert.ok(IndexView.element().style.display !== 'none');
  632. assert.ok(PostView.element().style.display === 'none');
  633. ++callback_count;
  634. return RouteResolver({
  635. PostView: ['4']
  636. }, function(view, params) {
  637. assert.equal(view.get('id'), '4');
  638. assert.ok(PostView.element().style.display !== 'none');
  639. assert.ok(IndexView.element().style.display === 'none');
  640. ++callback_count;
  641. return RouteResolver({
  642. IndexView: {}
  643. }, function(view, params) {
  644. assert.ok(IndexView.element().style.display !== 'none');
  645. assert.ok(PostView.element().style.display === 'none');
  646. ++callback_count;
  647. PostView.unbind('route');
  648. return RouteResolver({
  649. PostView: {
  650. id: 4
  651. }
  652. }, function(view, params) {
  653. assert.ok(IndexView.element().style.display === 'none');
  654. assert.ok(PostView.element().style.display !== 'none');
  655. ++callback_count;
  656. return before_exit(function() {
  657. assert.equal(5, callback_count);
  658. assert.equal(4, post_view_render_count);
  659. return assert.equal(1, index_view_render_count);
  660. });
  661. });
  662. });
  663. });
  664. });
  665. });
  666. });
  667. return View.extend({
  668. env: {
  669. server: function() {
  670. return true;
  671. },
  672. browser: function() {
  673. return false;
  674. }
  675. }
  676. });
  677. };
  678. module.exports.canPassDataInitialize = function(before_exit) {
  679. var attributes, collection, model, model_view;
  680. model = new Backbone.Model;
  681. collection = new Backbone.Collection;
  682. attributes = {
  683. key: 'value'
  684. };
  685. model_view = View.create();
  686. return model_view.initialize(model, function() {
  687. var collection_view;
  688. assert.equal(model_view.model, model);
  689. collection_view = View.create();
  690. return collection_view.initialize(collection, function() {
  691. var attributes_view;
  692. assert.equal(collection_view.collection, collection);
  693. attributes_view = View.create();
  694. attributes_view.initialize(attributes, function() {});
  695. return before_exit(function() {
  696. return assert.equal(attributes.key, attributes_view.get('key'));
  697. });
  698. });
  699. });
  700. };
  701. module.exports.canUse$InBuilder = function(before_exit) {
  702. var $BuilderView, click_count;
  703. click_count = 0;
  704. $BuilderView = View.create({
  705. $BuilderView: [
  706. Builder, {
  707. $: jQuery,
  708. delegate: {
  709. 'click div': function() {
  710. return ++click_count;
  711. },
  712. click: {
  713. div: function() {
  714. return ++click_count;
  715. }
  716. }
  717. },
  718. render: function() {
  719. this.key = 'test';
  720. return this.table(this.tr(this.td(), this.td(this.div().click(__bind(function() {
  721. ++click_count;
  722. return assert.equal(this.key, 'test');
  723. }, this)))));
  724. }
  725. }
  726. ]
  727. }).$BuilderView;
  728. return $BuilderView.initialize(function() {
  729. return before_exit(function() {
  730. $BuilderView.$('div').trigger('click');
  731. assert.equal(click_count, 3);
  732. return assert.equal($BuilderView.$('td').length, 2);
  733. });
  734. });
  735. };
  736. module.exports.canReverseLookup = function() {
  737. var ReverseLookupView;
  738. ReverseLookupView = View.create({
  739. ReverseLookupView: {
  740. $: jQuery
  741. }
  742. }).ReverseLookupView;
  743. return ReverseLookupView.initialize(function() {
  744. assert.equal(jQuery(this).view().name, 'ReverseLookupView');
  745. return assert.equal(this.$.view().name, 'ReverseLookupView');
  746. });
  747. };
  748. module.exports.anonViewHasElement = function() {
  749. var anon;
  750. anon = View.create();
  751. assert.ok(anon[0]);
  752. return assert.equal(anon[0].tagName, 'DIV');
  753. };
  754. http = require('http');
  755. fs = require('fs');
  756. file_exists = function(path) {
  757. try {
  758. fs.lstatSync(path);
  759. return true;
  760. } catch (e) {
  761. return false;
  762. }
  763. };
  764. public = __dirname + '/../test/public';
  765. ViewServer = require('./view.server.js').ViewServer;
  766. TestServer = ViewServer.create({
  767. TestServer: {
  768. public: public
  769. }
  770. }).TestServer;
  771. module.exports.serverWithoutPortDoesNotListen = function() {
  772. return assert.isUndefined(TestServer.server);
  773. };
  774. test_stack = [];
  775. test_stack.push(function(next) {
  776. var StaticServer1, StaticServer2, request_count;
  777. StaticServer1 = TestServer.create({
  778. StaticServer1: {
  779. port: 40001
  780. }
  781. }).StaticServer1;
  782. StaticServer2 = TestServer.create({
  783. StaticServer2: {
  784. port: 40002
  785. }
  786. }).StaticServer2;
  787. request_count = 0;
  788. return http.get({
  789. host: 'localhost',
  790. port: 40001,
  791. path: '/test.html'
  792. }, function(response) {
  793. return response.on('data', function(data) {
  794. assert.equal('Test', data.toString());
  795. StaticServer1.server.close();
  796. return http.get({
  797. host: 'localhost',
  798. port: 40002,
  799. path: '/test.html'
  800. }, function(response) {
  801. return response.on('data', function(data) {
  802. assert.equal('Test', data.toString());
  803. StaticServer2.server.close();
  804. return next();
  805. });
  806. });
  807. });
  808. });
  809. });
  810. test_stack.push(function(next) {
  811. var BasicAppServer, request_count;
  812. request_count = 0;
  813. BasicAppServer = TestServer.create({
  814. BasicAppServer: {
  815. port: 40003,
  816. routes: {
  817. '/': 'BasicView',
  818. '/template': 'TemplateView'
  819. },
  820. execute: ["" + public + "/javascripts/lib/jquery.js", "" + public + "/javascripts/lib/underscore.js", "" + public + "/javascripts/lib/backbone.js", "" + public + "/javascripts/lib/view.js", "" + public + "/javascripts/templates.js", "" + public + "/javascripts/views/basic.js"],
  821. javascripts: ["http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"]
  822. }
  823. }).BasicAppServer;
  824. return http.get({
  825. host: 'localhost',
  826. port: 40003,
  827. path: '/'
  828. }, function(response) {
  829. ++request_count;
  830. return response.on('data', function(data) {
  831. assert.match(data.toString(), /<div>test<\/div>/);
  832. assert.match(data.toString(), /src="http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.5.1\/jquery.min.js"/);
  833. return http.get({
  834. host: 'localhost',
  835. port: 40003,
  836. path: '/template'
  837. }, function(response) {
  838. ++request_count;
  839. return response.on('data', function(data) {
  840. assert.match(data.toString(), /<p>value<\/p>/);
  841. BasicAppServer.server.close();
  842. assert.equal(request_count, 2);
  843. return next();
  844. });
  845. });
  846. });
  847. });
  848. });
  849. test_stack.push(function(next) {
  850. var ProxyServer, ProxyServerSource1, ProxyServerSource2;
  851. ProxyServerSource1 = ViewServer.create({
  852. ProxyServerSource1: {
  853. port: 40004
  854. }
  855. }).ProxyServerSource1;
  856. ProxyServerSource1.server.get('/1', function(request, response) {
  857. return response.send('a');
  858. });
  859. ProxyServerSource2 = ViewServer.create({
  860. ProxyServerSource2: {
  861. port: 40005
  862. }
  863. }).ProxyServerSource2;
  864. ProxyServerSource2.server.get('/2', function(request, response) {
  865. return response.send('b');
  866. });
  867. ProxyServer = ViewServer.create({
  868. ProxyServer: {
  869. port: 40006,
  870. routes: {
  871. '/a/': 40004,
  872. '/b': 40005
  873. }
  874. }
  875. }).ProxyServer;
  876. return http.get({
  877. host: 'localhost',
  878. port: 40006,
  879. path: '/a/1'
  880. }, function(response) {
  881. return response.on('data', function(data) {
  882. assert.equal(data.toString(), 'a');
  883. return http.get({
  884. host: 'localhost',
  885. port: 40006,
  886. path: '/b/2'
  887. }, function(response) {
  888. return response.on('data', function(data) {
  889. ProxyServerSource1.server.close();
  890. ProxyServerSource2.server.close();
  891. ProxyServer.server.close();
  892. assert.equal(data.toString(), 'b');
  893. return next();
  894. });
  895. });
  896. });
  897. });
  898. });
  899. test_stack.push(function(next) {
  900. var ConditionalViewServer, a_was_set, b_was_set, c_was_set;
  901. ViewServer.env({
  902. set: {
  903. a: function() {
  904. return false;
  905. },
  906. b: true,
  907. c: function() {
  908. return true;
  909. }
  910. }
  911. });
  912. a_was_set = false;
  913. b_was_set = false;
  914. c_was_set = false;
  915. ViewServer.env(false, {
  916. a: function() {
  917. return a_was_set = true;
  918. },
  919. b: function() {
  920. return b_was_set = true;
  921. },
  922. c: function() {
  923. return c_was_set = true;
  924. }
  925. });
  926. assert.equal(a_was_set, false);
  927. assert.equal(b_was_set, true);
  928. assert.equal(c_was_set, true);
  929. ConditionalViewServer = TestServer.create({
  930. ConditionalViewServer: {
  931. port: 40007,
  932. routes: {
  933. '/': 'BasicView'
  934. },
  935. execute: ["" + public + "/javascripts/lib/jquery.js", "" + public + "/javascripts/lib/underscore.js", "" + public + "/javascripts/lib/backbone.js", "" + public + "/javascripts/lib/view.js", "" + public + "/javascripts/templates.js", "" + public + "/javascripts/views/basic.js"],
  936. env: {
  937. a: function() {
  938. return {
  939. stylesheets: ['a']
  940. };
  941. },
  942. b: {
  943. stylesheets: ['b']
  944. },
  945. c: function() {
  946. return {
  947. stylesheets: 'c'
  948. };
  949. }
  950. }
  951. }
  952. }).ConditionalViewServer;
  953. return http.get({
  954. host: 'localhost',
  955. port: 40007,
  956. path: '/'
  957. }, function(response) {
  958. return response.on('data', function(data) {
  959. assert.match(data.toString(), /href="b"/);
  960. assert.match(data.toString(), /href="c"/);
  961. assert.isNull(data.toString().match(/href="a"/));
  962. ConditionalViewServer.server.close();
  963. return next();
  964. });
  965. });
  966. });
  967. test_stack.push(function(next) {
  968. var CachingViewServer, cache_location;
  969. CachingViewServer = TestServer.create({
  970. CachingViewServer: {
  971. port: 40008,
  972. routes: {
  973. '/': 'BasicView',
  974. '/test/test2/test3/test4/test5/template': 'TemplateView',
  975. '/text': 'TextView'
  976. },
  977. cache: ['BasicView', 'TemplateView'],
  978. execute: ["" + public + "/javascripts/lib/jquery.js", "" + public + "/javascripts/lib/underscore.js", "" + public + "/javascripts/lib/backbone.js", "" + public + "/javascripts/lib/view.js", "" + public + "/javascripts/templates.js", "" + public + "/javascripts/views/basic.js"]
  979. }
  980. }).CachingViewServer;
  981. cache_location = public + '/../cache';
  982. if (file_exists(cache_location + '/index.html')) {
  983. fs.unlinkSync(cache_location + '/index.html');
  984. }
  985. if (file_exists(cache_location + '/test/test2/test3/test4/test5/template.html')) {
  986. fs.unlinkSync(cache_location + '/test/test2/test3/test4/test5/template.html');
  987. }
  988. if (file_exists(cache_location + '/test/test2/test3/test4/test5')) {
  989. fs.rmdirSync(cache_location + '/test/test2/test3/test4/test5');
  990. }
  991. if (file_exists(cache_location + '/test/test2/test3/test4')) {
  992. fs.rmdirSync(cache_location + '/test/test2/test3/test4');
  993. }
  994. if (file_exists(cache_location + '/test/test2/test3')) {
  995. fs.rmdirSync(cache_location + '/test/test2/test3');
  996. }
  997. if (file_exists(cache_location + '/test/test2')) {
  998. fs.rmdirSync(cache_location + '/test/test2');
  999. }
  1000. if (file_exists(cache_location + '/test')) {
  1001. fs.rmdirSync(cache_location + '/test');
  1002. }
  1003. assert.equal(false, file_exists(cache_location + '/index.html'));
  1004. return http.get({
  1005. host: 'localhost',
  1006. port: 40008,
  1007. path: '/'
  1008. }, function(response) {
  1009. assert.equal(true, file_exists(cache_location + '/index.html'));
  1010. return response.on('data', function(data) {
  1011. assert.equal(data.toString(), fs.readFileSync(cache_location + '/index.html'));
  1012. return http.get({
  1013. host: 'localhost',
  1014. port: 40008,
  1015. path: '/text'
  1016. }, function(response) {
  1017. assert.equal(false, file_exists(cache_location + '/text.html'));
  1018. return response.on('data', function(data) {
  1019. assert.match(data.toString(), /<div>test<\/div>/);
  1020. assert.equal(false, file_exists(cache_location + '/test/test2/test3/test4/test5/template.html'));
  1021. return http.get({
  1022. host: 'localhost',
  1023. port: 40008,
  1024. path: '/test/test2/test3/test4/test5/template'
  1025. }, function(response) {
  1026. assert.equal(true, file_exists(cache_location + '/test/test2/test3/test4/test5/template.html'));
  1027. return response.on('data', function(data) {
  1028. assert.equal(data.toString(), fs.readFileSync(cache_location + '/test/test2/test3/test4/test5/template.html'));
  1029. fs.unlinkSync(cache_location + '/index.html');
  1030. fs.unlinkSync(cache_location + '/test/test2/test3/test4/test5/template.html');
  1031. fs.rmdirSync(cache_location + '/test/test2/test3/test4/test5');
  1032. fs.rmdirSync(cache_location + '/test/test2/test3/test4');
  1033. fs.rmdirSync(cache_location + '/test/test2/test3');
  1034. fs.rmdirSync(cache_location + '/test/test2');
  1035. fs.rmdirSync(cache_location + '/test');
  1036. CachingViewServer.server.close();
  1037. return next();
  1038. });
  1039. });
  1040. });
  1041. });
  1042. });
  1043. });
  1044. });
  1045. module.exports.runServerTests = function() {
  1046. var test_iterator;
  1047. test_iterator = function() {
  1048. var test;
  1049. test = test_stack.shift();
  1050. if (test) {
  1051. return test(test_iterator);
  1052. }
  1053. };
  1054. return test_iterator();
  1055. };
  1056. }).call(this);