/test/test-foo.js

https://bitbucket.org/mcsaatchi/generator-backbone · JavaScript · 158 lines · 115 code · 18 blank · 25 comment · 1 complexity · a64ee0daaa64871766db0cf6b8ab1f63 MD5 · raw file

  1. /*global describe:true, beforeEach:true, it:true */
  2. 'use strict';
  3. var path = require('path');
  4. var helpers = require('yeoman-generator').test;
  5. var assert = require('assert');
  6. // XXX With current API, (prior v2), that's a complete mess to setup generators
  7. // if they differ from the standard lib/generators layout.
  8. //
  9. // Even for workarounds, the API is awful and doesn't let you do anything.
  10. //
  11. // With the new API, it will be much easier to manually register one or a set
  12. // of generators, and manage multiple environments.
  13. //
  14. // Something like:
  15. //
  16. // generators()
  17. // .register(require('../all'), 'backbone:all')
  18. // .register(require('../app'), 'backbone:app')
  19. // .register(require('../view'), 'backbone:view')
  20. // .register(require('../router'), 'backbone:router')
  21. // .register(require('../model'), 'backbone:model')
  22. // .register(require('../collection'), 'backbone:collection')
  23. //
  24. // Or for the lazy guy:
  25. //
  26. // generators()
  27. // .lookup('*:*', path.join(__dirname, '..'))
  28. //
  29. describe('Backbone generator test', function () {
  30. beforeEach(function (done) {
  31. helpers.testDirectory(path.join(__dirname, './temp'), function (err) {
  32. if (err) {
  33. return done(err);
  34. }
  35. this.backbone = {};
  36. this.backbone.app = helpers.createGenerator('backbone:app', [
  37. '../../app', [
  38. helpers.createDummyGenerator(),
  39. 'mocha:app'
  40. ]
  41. ]);
  42. this.backbone.app.options['skip-install'] = true;
  43. helpers.mockPrompt(this.backbone.app, {
  44. 'compassBootstrap': true,
  45. 'includeRequireJS': false
  46. });
  47. done();
  48. }.bind(this));
  49. });
  50. it('every generator can be required without throwing', function () {
  51. // not testing the actual run of generators yet
  52. this.all = require('../all');
  53. this.app = require('../app');
  54. this.collection = require('../collection');
  55. this.model = require('../model');
  56. this.router = require('../router');
  57. this.view = require('../view');
  58. });
  59. it('creates expected files', function (done) {
  60. var expected = [
  61. ['bower.json', /"name": "temp"/],
  62. ['package.json', /"name": "temp"/],
  63. 'Gruntfile.js',
  64. 'app/404.html',
  65. 'app/favicon.ico',
  66. 'app/robots.txt',
  67. 'app/index.html',
  68. 'app/.htaccess',
  69. '.gitignore',
  70. '.gitattributes',
  71. '.bowerrc',
  72. '.jshintrc',
  73. '.editorconfig',
  74. 'Gruntfile.js',
  75. 'package.json',
  76. 'app/scripts/main.js',
  77. 'app/styles/main.scss',
  78. 'app/images/glyphicons-halflings-white.png',
  79. 'app/images/glyphicons-halflings.png'
  80. ];
  81. this.backbone.app.run({}, function () {
  82. helpers.assertFiles(expected);
  83. done();
  84. });
  85. });
  86. describe('Backbone Model', function () {
  87. it('creates backbone model', function (done) {
  88. var model = helpers.createGenerator('backbone:model', ['../../model'], ['foo']);
  89. this.backbone.app.run({}, function () {
  90. model.run([], function () {
  91. helpers.assertFiles([
  92. ['app/scripts/models/foo-model.js',
  93. /Models.FooModel = Backbone.Model.extend\(\{/]
  94. ]);
  95. });
  96. done();
  97. });
  98. });
  99. });
  100. describe('Backbone Collection', function () {
  101. it('creates backbone collection', function (done) {
  102. var collection = helpers.createGenerator('backbone:collection', ['../../collection'], ['foo']);
  103. this.backbone.app.run({}, function () {
  104. collection.run([], function () {
  105. helpers.assertFiles([
  106. ['app/scripts/collections/foo-collection.js', /Collections.FooCollection = Backbone.Collection.extend\(\{/]
  107. ]);
  108. });
  109. done();
  110. });
  111. });
  112. });
  113. describe('Backbone Router', function () {
  114. it('creates backbone router', function (done) {
  115. var router = helpers.createGenerator('backbone:router', ['../../router'], ['foo']);
  116. this.backbone.app.run({}, function () {
  117. router.run([], function () {
  118. helpers.assertFiles([
  119. ['app/scripts/routes/foo-router.js', /Routers.FooRouter = Backbone.Router.extend\(\{/]
  120. ]);
  121. });
  122. done();
  123. });
  124. });
  125. });
  126. describe('Backbone View', function () {
  127. it('creates backbone view', function (done) {
  128. var view = helpers.createGenerator('backbone:view', ['../../view'], ['foo']);
  129. this.backbone.app.run({}, function () {
  130. view.run([], function () {
  131. helpers.assertFiles([
  132. ['app/scripts/views/foo-view.js', /Views.FooView = Backbone.View.extend\(\{(.|\n)*app\/scripts\/templates\/foo.ejs/],
  133. 'app/scripts/templates/foo.ejs'
  134. ]);
  135. });
  136. done();
  137. });
  138. });
  139. });
  140. });