confirm-spec.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. var assert = require('assert');
  3. //Gulp uses vinyl source streams
  4. var source = require('vinyl-source-stream');
  5. var proxyrequire = require('proxyquire');
  6. /**
  7. * The following test spec will verify the operation of the confirm function
  8. */
  9. describe('gulp confirm function unit tests', function () {
  10. describe('verify that confirm function operates correctly', function () {
  11. it('verify that confirm converts string message to options', function ( done ){
  12. var prompt = function ( listOptions ){
  13. return new Promise ( ( resolve, reject ) => {
  14. if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string'){
  15. done();
  16. }else{
  17. done('options is not a string');
  18. }
  19. resolve('Test Completed');
  20. });
  21. };
  22. //Mock inquirer to capture response
  23. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  24. let srcStream = source('../README.md');
  25. let resp = srcStream.pipe( gulpPrompt.confirm( 'options string' ) );
  26. resp.write('../test.txt');
  27. });
  28. it('verify that confirm leaves object messages as is', function ( done ){
  29. var prompt = function ( listOptions ){
  30. return new Promise( (resolve,reject) => {
  31. resolve('Test Completed');
  32. if( ( Array.isArray( listOptions) ) && (typeof listOptions[0] !== 'string') ){
  33. assert.equal( listOptions[0].message, 'Test Message?' );
  34. assert.equal( listOptions[0].default, true );
  35. done();
  36. }else{
  37. done('options convertion not handled');
  38. }
  39. });
  40. };
  41. //Mock inquirer to capture response
  42. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  43. let srcStream = source('../README.md');
  44. let options = {
  45. type: 'confirm',
  46. name: 'val',
  47. message: 'Test Message?',
  48. default: true
  49. };
  50. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  51. resp.write('../test.txt');
  52. });
  53. it('verify that confirm sets default message and value if non provided', function ( done ){
  54. var prompt = function ( listOptions ){
  55. return new Promise( (resolve,reject) => {
  56. resolve('Test Completed');
  57. if( ( Array.isArray( listOptions) ) && (typeof listOptions[0] !== 'string') ){
  58. //The following are defaults
  59. assert.equal( listOptions[0].message, 'Are you sure?' );
  60. assert.equal( listOptions[0].default, false );
  61. done();
  62. }else{
  63. done('options defaults not set');
  64. }
  65. });
  66. };
  67. //Mock inquirer to capture response
  68. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  69. let srcStream = source('../README.md');
  70. let options = {
  71. type: 'confirm',
  72. name: 'val'
  73. };
  74. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  75. resp.write('../test.txt');
  76. });
  77. it('verify that confirm creates default object if not provided', function ( done ){
  78. var prompt = function ( listOptions ){
  79. return new Promise( (resolve,reject) => {
  80. resolve('Test Completed');
  81. if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
  82. //The following are defaults
  83. assert.equal( listOptions[0].message, 'Are you sure?' );
  84. assert.equal( listOptions[0].default, false );
  85. assert.equal( listOptions[0].type, 'confirm' );
  86. assert.equal( listOptions[0].name, 'val' );
  87. done();
  88. }else{
  89. done('options defaults not set');
  90. }
  91. });
  92. };
  93. //Mock inquirer to capture response
  94. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  95. let srcStream = source('../README.md');
  96. let options;//Intentionally set to not defined
  97. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  98. resp.write('../test.txt');
  99. });
  100. it('verify that lodash templates replacement works', function ( done ){
  101. var prompt = function ( listOptions ){
  102. return new Promise( (resolve,reject) => {
  103. resolve('Test Completed');
  104. if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
  105. assert.equal( listOptions[0].message, 'hello fred! what is your choice?' );
  106. done();
  107. }else{
  108. console.log( 'Failed test');
  109. done('options defaults not set');
  110. }
  111. });
  112. };
  113. //Mock inquirer to capture response
  114. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  115. let srcStream = source('../README.md');
  116. let options = {
  117. type: 'confirm',
  118. name: 'val',
  119. message: 'hello <%= user %>! what is your choice?',
  120. templateOptions:{ 'user': 'fred' }
  121. };
  122. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  123. console.log('Executing test');
  124. resp.write('../test.txt');
  125. });
  126. it('verify that lodash templates is only called when template options are defined', function ( done ){
  127. var prompt = function ( listOptions ){
  128. return new Promise( (resolve,reject) => {
  129. resolve('Test Completed');
  130. if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
  131. assert.equal( listOptions[0].message, 'hello <%= user %>! what is your choice?' );
  132. done();
  133. }else{
  134. console.log( 'Failed test');
  135. done('options defaults not set');
  136. }
  137. });
  138. };
  139. //Mock inquirer to capture response
  140. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  141. let srcStream = source('../README.md');
  142. let options = {
  143. type: 'confirm',
  144. name: 'val',
  145. message: 'hello <%= user %>! what is your choice?'
  146. };
  147. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  148. resp.write('../test.txt');
  149. });
  150. it('verify that lodash templates replacement works for two fields', function ( done ){
  151. var prompt = function ( listOptions ){
  152. return new Promise( (resolve,reject) => {
  153. resolve('Test Completed');
  154. if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
  155. assert.equal( listOptions[0].message, 'hello fred! what is today your choice?' );
  156. done();
  157. }else{
  158. console.log( 'Failed test');
  159. done('options defaults not set');
  160. }
  161. });
  162. };
  163. //Mock inquirer to capture response
  164. let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
  165. let srcStream = source('../README.md');
  166. let options = {
  167. type: 'confirm',
  168. name: 'val',
  169. message: 'hello <%= user %>! what is <%= date %> your choice?',
  170. templateOptions:{ 'user': 'fred', 'date':'today' }
  171. };
  172. let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
  173. console.log('Executing test');
  174. resp.write('../test.txt');
  175. });
  176. });
  177. });