helper.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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: { type: 'ip', allowEmpty: false },
  20. telephone: { type: 'mobile', allowEmpty: false },
  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: { ip: true, required: true },
  31. telephone: { mobile: true, required: true },
  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('explode paths', function* () {
  74. // 创建 ctx
  75. const ctx = app.mockContext();
  76. const paths = ['1.2.3.4.5', '1.2.3.5.7'];
  77. const normal = ctx.helper.explodePath(paths, '.');
  78. const normalExpect = ['1', '1.2', '1.2.3', '1.2.3.4', '1.2.3.4.5', '1.2.3.5', '1.2.3.5.7'];
  79. let result = true;
  80. try {
  81. for (const index in normal) {
  82. if (normal[index] !== normalExpect[index]) {
  83. throw '数据错误';
  84. }
  85. }
  86. } catch (error) {
  87. result = false;
  88. }
  89. assert(result);
  90. });
  91. });