helper.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. /**
  3. * 辅助方法扩展单元测试
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/24
  7. * @version
  8. */
  9. const { app, assert } = require('egg-mock/bootstrap');
  10. describe('test/app/extend/helper.test.js', () => {
  11. it('validateConvert test', function* () {
  12. // 创建 ctx
  13. const ctx = app.mockContext();
  14. const rule = {
  15. username: { type: 'string', required: true, allowEmpty: false, min: 4, max: 10 },
  16. group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
  17. password: { type: 'password', required: true, allowEmpty: false, min: 4 },
  18. confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
  19. ip: 'isIP',
  20. telephone: 'isMobile',
  21. };
  22. // 转换为json验证
  23. let convertRule = ctx.helper.validateConvert(rule);
  24. convertRule = JSON.stringify(convertRule);
  25. let expectRule = {
  26. username: { required: true, minlength: 4, maxlength: 10 },
  27. group_id: { required: true, min: 4, max: 10 },
  28. password: { required: true, minlength: 4 },
  29. confirm_password: { required: true, minlength: 4, equalTo: '#password' },
  30. ip: 'isIP',
  31. telephone: 'isMobile',
  32. };
  33. expectRule = JSON.stringify(expectRule);
  34. assert(convertRule === expectRule);
  35. });
  36. it('generateRandomString test', function* () {
  37. // 创建 ctx
  38. const ctx = app.mockContext();
  39. const randomMix = ctx.helper.generateRandomString(4);
  40. const mixReg = /[A-Za-z0-9]{4}/;
  41. assert(mixReg.test(randomMix));
  42. // 测试纯数字
  43. const randomNumber = ctx.helper.generateRandomString(4, 2);
  44. const numberReg = /\d{4}/;
  45. assert(numberReg.test(randomNumber));
  46. // 测试纯字母
  47. const randomString = ctx.helper.generateRandomString(4, 3);
  48. const stringReg = /[A-Za-z]{4}/;
  49. assert(stringReg.test(randomString));
  50. });
  51. it('showSortFlag test', function* () {
  52. // 创建 ctx
  53. const ctx = app.mockContext({
  54. // sort: { test: 1 },
  55. sort: ['id', 'asc'],
  56. });
  57. const flag = ctx.helper.showSortFlag('id');
  58. assert(flag === '-');
  59. });
  60. it('isAjax test', function* () {
  61. // 创建 ctx
  62. const ctx = app.mockContext();
  63. const result = ctx.helper.isAjax(ctx);
  64. assert(!result);
  65. });
  66. it('sendRequest test', function* () {
  67. // 创建 ctx
  68. const ctx = app.mockContext();
  69. const url = 'http://ip.taobao.com/service/getIpInfo.php';
  70. const responseData = yield ctx.helper.sendRequest(url, { ip: '116.7.222.12' }, 'GET');
  71. assert(responseData.data.ip === '116.7.222.12');
  72. });
  73. it('convertBreadCrumb append test', function* () {
  74. // 创建 ctx
  75. const ctx = app.mockContext({
  76. topPermission: {
  77. name: 'test',
  78. },
  79. currentName: 'current',
  80. });
  81. const breadCrumb = [{
  82. name: 'append',
  83. url: 'http://www.baidu.com',
  84. }];
  85. const result = ctx.helper.convertBreadCrumb(breadCrumb);
  86. const expect = 'test / <a href="/http://www.baidu.com">append</a> / current';
  87. assert(result === expect);
  88. });
  89. it('convertBreadCrumb replace test', function* () {
  90. // 创建 ctx
  91. const ctx = app.mockContext();
  92. const breadCrumb = [
  93. { name: 'test' },
  94. { name: 'append', url: 'http://www.baidu.com' },
  95. { name: 'current' },
  96. ];
  97. const result = ctx.helper.convertBreadCrumb(breadCrumb, 'replace');
  98. const expect = 'test / <a href="/http://www.baidu.com">append</a> / current';
  99. assert(result === expect);
  100. });
  101. });