PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/project-grunt/node_modules/grunt-bower-task/node_modules/bower/node_modules/bower-logger/test/test.js

https://gitlab.com/nVySin/Mo_Grabber
JavaScript | 389 lines | 338 code | 51 blank | 0 comment | 0 complexity | 2e3ef66bb8af7f295940614e01039ae2 MD5 | raw file
  1. var expect = require('expect.js');
  2. var EventEmitter = require('events').EventEmitter;
  3. var Logger = require('../');
  4. describe('Logger', function () {
  5. var logger;
  6. beforeEach(function () {
  7. logger = new Logger();
  8. });
  9. describe('.constructor', function () {
  10. it('should provide an instance of Logger', function () {
  11. expect(logger instanceof Logger).to.be(true);
  12. });
  13. it('should provide an instance of EventEmitter', function () {
  14. expect(logger instanceof EventEmitter).to.be(true);
  15. });
  16. it('should have prototype methods', function () {
  17. var methods = [
  18. 'intercept', 'pipe', 'geminate', 'log'
  19. ];
  20. methods.forEach(function (method) {
  21. expect(logger).to.have.property(method);
  22. });
  23. });
  24. });
  25. describe('events', function () {
  26. var logData = {
  27. foo: 'bar',
  28. baz: 'string'
  29. };
  30. it('should pass through {}', function (next) {
  31. logger.on('log', function (log) {
  32. expect(log.data).to.eql({});
  33. next();
  34. });
  35. logger.info();
  36. });
  37. it('should pass through logData', function (next) {
  38. logger.on('log', function (log) {
  39. expect(log.data).to.eql(logData);
  40. next();
  41. });
  42. logger.info('foo', 'message', logData);
  43. });
  44. it('should emit error event', function (next) {
  45. logger.on('log', function (log) {
  46. expect(log.level).to.eql('error');
  47. expect(log.id).to.eql('foo');
  48. expect(log.message).to.eql('error message');
  49. expect(log.data).to.eql({});
  50. next();
  51. });
  52. logger.error('foo', 'error message');
  53. });
  54. it('should emit conflict event', function (next) {
  55. logger.on('log', function (log) {
  56. expect(log.level).to.eql('conflict');
  57. expect(log.id).to.eql('foo');
  58. expect(log.message).to.eql('conflict message');
  59. expect(log.data).to.eql({});
  60. next();
  61. });
  62. logger.conflict('foo', 'conflict message');
  63. });
  64. it('should emit warn event', function (next) {
  65. logger.on('log', function (log) {
  66. expect(log.level).to.eql('warn');
  67. expect(log.id).to.eql('foo');
  68. expect(log.message).to.eql('warn message');
  69. expect(log.data).to.eql({});
  70. next();
  71. });
  72. logger.warn('foo', 'warn message');
  73. });
  74. it('should emit action event', function (next) {
  75. logger.on('log', function (log) {
  76. expect(log.level).to.eql('action');
  77. expect(log.id).to.eql('foo');
  78. expect(log.message).to.eql('action message');
  79. expect(log.data).to.eql({});
  80. next();
  81. });
  82. logger.action('foo', 'action message');
  83. });
  84. it('should emit info event', function (next) {
  85. logger.on('log', function (log) {
  86. expect(log.level).to.eql('info');
  87. expect(log.id).to.eql('foo');
  88. expect(log.message).to.eql('info message');
  89. expect(log.data).to.eql({});
  90. next();
  91. });
  92. logger.info('foo', 'info message');
  93. });
  94. it('should emit debug event', function (next) {
  95. logger.on('log', function (log) {
  96. expect(log.level).to.eql('debug');
  97. expect(log.id).to.eql('foo');
  98. expect(log.message).to.eql('debug message');
  99. expect(log.data).to.eql({});
  100. next();
  101. });
  102. logger.debug('foo', 'debug message');
  103. });
  104. });
  105. describe('.intercept', function () {
  106. it('should add the function and call it when a log occurs', function (next) {
  107. var called;
  108. var data = {
  109. 'some': 'thing'
  110. };
  111. logger.intercept(function (log) {
  112. called = true;
  113. expect(log).to.eql({
  114. level: 'warn',
  115. id: 'foo',
  116. message: 'bar',
  117. data: data
  118. });
  119. expect(log.data).to.equal(data);
  120. });
  121. logger.log('warn', 'foo', 'bar', data);
  122. expect(called).to.be(true);
  123. next();
  124. });
  125. it('should call the interceptors by order before emitting the event', function (next) {
  126. var called = [];
  127. logger.intercept(function () {
  128. called.push(1);
  129. });
  130. logger.intercept(function () {
  131. called.push(2);
  132. });
  133. logger.log('warn', 'foo', 'bar');
  134. expect(called).to.eql([1, 2]);
  135. next();
  136. });
  137. it('should call the interceptors along the chain', function (next) {
  138. var called = [];
  139. var childLogger = logger.geminate();
  140. childLogger.intercept(function () {
  141. called.push(1);
  142. });
  143. logger.intercept(function () {
  144. called.push(3);
  145. });
  146. childLogger.on('log', function () {
  147. called.push(2);
  148. });
  149. logger.on('log', function () {
  150. called.push(4);
  151. });
  152. childLogger.log('warn', 'foo', 'bar');
  153. expect(called).to.eql([1, 2, 3, 4]);
  154. next();
  155. });
  156. });
  157. describe('.pipe', function () {
  158. it('should return the passed emitter', function () {
  159. var otherEmitter = new EventEmitter();
  160. expect(logger.pipe(otherEmitter)).to.equal(otherEmitter);
  161. });
  162. it('should pipe log events to another emitter', function (next) {
  163. var otherEmitter = new EventEmitter();
  164. var data = {
  165. 'some': 'thing'
  166. };
  167. var piped;
  168. logger.pipe(otherEmitter);
  169. otherEmitter.on('log', function (log) {
  170. piped = true;
  171. expect(log).to.eql({
  172. level: 'warn',
  173. id: 'foo',
  174. message: 'bar',
  175. data: data
  176. });
  177. });
  178. logger.log('warn', 'foo', 'bar', data);
  179. expect(piped).to.be(true);
  180. next();
  181. });
  182. });
  183. describe('.geminate', function () {
  184. it('should return a new logger instance', function () {
  185. var newLogger = logger.geminate();
  186. expect(newLogger).to.be.an(Logger);
  187. expect(newLogger).to.be.an(EventEmitter);
  188. expect(newLogger).to.not.be.equal(logger);
  189. });
  190. it('should pipe the new logger events to the original logger', function (next) {
  191. var piped = [];
  192. var childLogger = logger.geminate();
  193. var data = {
  194. 'some': 'thing'
  195. };
  196. childLogger.on('log', function (log) {
  197. piped.push(1);
  198. expect(log).to.eql({
  199. level: 'warn',
  200. id: 'foo',
  201. message: 'bar',
  202. data: data
  203. });
  204. expect(log.data).to.equal(data);
  205. });
  206. logger.on('log', function (log) {
  207. piped.push(2);
  208. expect(log).to.eql({
  209. level: 'warn',
  210. id: 'foo',
  211. message: 'bar',
  212. data: data
  213. });
  214. expect(log.data).to.equal(data);
  215. });
  216. childLogger.log('warn', 'foo', 'bar', data);
  217. expect(piped).to.eql([1, 2]);
  218. next();
  219. });
  220. });
  221. describe('.prompt', function () {
  222. it('should only allow calling the callback once', function () {
  223. var calls = 0;
  224. logger
  225. .once('prompt', function (prompts, callback) {
  226. callback({ prompt: 'bar' });
  227. callback({ prompt: 'foo' });
  228. })
  229. .prompt({
  230. type: 'input',
  231. message: 'foo'
  232. }, function () {
  233. calls += 1;
  234. });
  235. expect(calls).to.equal(1);
  236. });
  237. it('should accept a prompt', function (next) {
  238. logger
  239. .once('prompt', function (prompts, callback) {
  240. callback({
  241. prompt: 'bar'
  242. });
  243. })
  244. .prompt({
  245. type: 'input',
  246. message: 'foo'
  247. }, function (err, answer) {
  248. expect(err).to.not.be.ok();
  249. expect(answer).to.equal('bar');
  250. next();
  251. });
  252. });
  253. it('should accept several prompts', function (next) {
  254. logger
  255. .once('prompt', function (prompts, callback) {
  256. callback({
  257. foo: 'bar',
  258. foz: 'baz'
  259. });
  260. })
  261. .prompt([
  262. {
  263. name: 'foo',
  264. type: 'input',
  265. message: 'foo'
  266. },
  267. {
  268. name: 'foz',
  269. type: 'confirm',
  270. message: 'foz'
  271. }
  272. ], function (err, answer) {
  273. expect(err).to.not.be.ok();
  274. expect(answer.foo).to.equal('bar');
  275. expect(answer.foz).to.equal('baz');
  276. logger
  277. .once('prompt', function (prompts, callback) {
  278. callback({
  279. foo: 'bar'
  280. });
  281. })
  282. .prompt([
  283. {
  284. name: 'foo',
  285. type: 'input',
  286. message: 'foo'
  287. }
  288. ], function (err, answer) {
  289. expect(err).to.not.be.ok();
  290. expect(answer.foo).to.equal('bar');
  291. next();
  292. });
  293. });
  294. });
  295. it('should error on invalid prompt type', function (next) {
  296. logger.prompt({
  297. type: 'xxx',
  298. message: 'foo'
  299. }, function (err) {
  300. expect(err).to.be.an(Error);
  301. expect(err.code).to.be('ENOTSUP');
  302. next();
  303. });
  304. });
  305. it('should trim the answers', function (next) {
  306. logger
  307. .once('prompt', function (prompts, callback) {
  308. callback({
  309. prompt: ' bar '
  310. });
  311. })
  312. .prompt({
  313. type: 'input',
  314. message: 'foo'
  315. }, function (err, answer) {
  316. expect(err).to.not.be.ok();
  317. expect(answer).to.equal('bar');
  318. next();
  319. });
  320. });
  321. it('should trim multiple response answers', function (next) {
  322. logger
  323. .once('prompt', function (prompts, callback) {
  324. callback({
  325. prompt: [' bar ', ' foo', 'baz ']
  326. });
  327. })
  328. .prompt({
  329. type: 'checkbox',
  330. message: 'foo'
  331. }, function (err, answer) {
  332. expect(err).to.not.be.ok();
  333. expect(answer).to.eql(['bar', 'foo', 'baz']);
  334. next();
  335. });
  336. });
  337. });
  338. });