platform.schema.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { describe, it, expect } from 'vitest';
  2. import { CreatePlatformSchema, UpdatePlatformSchema } from '../../src/schemas/platform.schema';
  3. describe('平台Schema验证测试', () => {
  4. describe('CreatePlatformSchema', () => {
  5. it('应该验证空字符串转换为undefined', () => {
  6. const data = {
  7. platformName: '测试平台',
  8. contactPerson: '张三',
  9. contactPhone: '13800138000',
  10. contactEmail: '' // 空字符串
  11. };
  12. const result = CreatePlatformSchema.safeParse(data);
  13. expect(result.success).toBe(true);
  14. if (result.success) {
  15. expect(result.data.contactEmail).toBeUndefined();
  16. }
  17. });
  18. it('应该验证undefined通过验证(可选字段)', () => {
  19. const data = {
  20. platformName: '测试平台',
  21. contactPerson: '张三',
  22. contactPhone: '13800138000'
  23. // contactEmail字段不存在,应该是undefined
  24. };
  25. const result = CreatePlatformSchema.safeParse(data);
  26. expect(result.success).toBe(true);
  27. if (result.success) {
  28. expect(result.data.contactEmail).toBeUndefined();
  29. }
  30. });
  31. it('应该验证有效邮箱格式通过验证', () => {
  32. const data = {
  33. platformName: '测试平台',
  34. contactPerson: '张三',
  35. contactPhone: '13800138000',
  36. contactEmail: 'zhangsan@example.com'
  37. };
  38. const result = CreatePlatformSchema.safeParse(data);
  39. expect(result.success).toBe(true);
  40. if (result.success) {
  41. expect(result.data.contactEmail).toBe('zhangsan@example.com');
  42. }
  43. });
  44. it('应该验证无效邮箱格式显示中文错误提示', () => {
  45. const data = {
  46. platformName: '测试平台',
  47. contactPerson: '张三',
  48. contactPhone: '13800138000',
  49. contactEmail: '无效邮箱'
  50. };
  51. const result = CreatePlatformSchema.safeParse(data);
  52. expect(result.success).toBe(false);
  53. if (!result.success) {
  54. // Zod错误格式:result.error.issues[0].message
  55. const errorMessage = result.error.issues[0].message;
  56. expect(errorMessage).toBe('请输入有效的邮箱地址');
  57. }
  58. });
  59. it('应该验证超长邮箱地址显示中文长度错误提示', () => {
  60. const longEmail = 'a'.repeat(100) + '@example.com'; // 超过100个字符
  61. const data = {
  62. platformName: '测试平台',
  63. contactPerson: '张三',
  64. contactPhone: '13800138000',
  65. contactEmail: longEmail
  66. };
  67. const result = CreatePlatformSchema.safeParse(data);
  68. expect(result.success).toBe(false);
  69. if (!result.success) {
  70. const errorMessage = result.error.issues[0].message;
  71. expect(errorMessage).toBe('邮箱地址不能超过100个字符');
  72. }
  73. });
  74. it('应该验证平台名称必填', () => {
  75. const data = {
  76. // platformName字段缺失
  77. contactPerson: '张三',
  78. contactPhone: '13800138000'
  79. };
  80. const result = CreatePlatformSchema.safeParse(data);
  81. expect(result.success).toBe(false);
  82. if (!result.success) {
  83. const errorMessage = result.error.issues[0].message;
  84. // Zod的错误消息可能是"Required"或其他,我们检查它是否包含错误
  85. expect(errorMessage).toBeDefined();
  86. }
  87. });
  88. it('应该验证平台名称不能为空字符串', () => {
  89. const data = {
  90. platformName: '', // 空字符串
  91. contactPerson: '张三',
  92. contactPhone: '13800138000'
  93. };
  94. const result = CreatePlatformSchema.safeParse(data);
  95. expect(result.success).toBe(false);
  96. if (!result.success) {
  97. const errorMessage = result.error.issues[0].message;
  98. expect(errorMessage).toBe('平台名称不能为空');
  99. }
  100. });
  101. it('应该验证平台名称超长显示中文错误提示', () => {
  102. const longName = 'a'.repeat(101); // 超过100个字符
  103. const data = {
  104. platformName: longName,
  105. contactPerson: '张三',
  106. contactPhone: '13800138000'
  107. };
  108. const result = CreatePlatformSchema.safeParse(data);
  109. expect(result.success).toBe(false);
  110. if (!result.success) {
  111. const errorMessage = result.error.issues[0].message;
  112. expect(errorMessage).toBe('平台名称不能超过100个字符');
  113. }
  114. });
  115. });
  116. describe('UpdatePlatformSchema', () => {
  117. it('应该验证空字符串转换为undefined', () => {
  118. const data = {
  119. id: 1,
  120. platformName: '更新后的平台',
  121. contactEmail: '' // 空字符串
  122. };
  123. const result = UpdatePlatformSchema.safeParse(data);
  124. expect(result.success).toBe(true);
  125. if (result.success) {
  126. expect(result.data.contactEmail).toBeUndefined();
  127. }
  128. });
  129. it('应该验证undefined通过验证(可选字段)', () => {
  130. const data = {
  131. id: 1,
  132. platformName: '更新后的平台'
  133. // contactEmail字段不存在,应该是undefined
  134. };
  135. const result = UpdatePlatformSchema.safeParse(data);
  136. expect(result.success).toBe(true);
  137. if (result.success) {
  138. expect(result.data.contactEmail).toBeUndefined();
  139. }
  140. });
  141. it('应该验证有效邮箱格式通过验证', () => {
  142. const data = {
  143. id: 1,
  144. platformName: '更新后的平台',
  145. contactEmail: 'lisi@example.com'
  146. };
  147. const result = UpdatePlatformSchema.safeParse(data);
  148. expect(result.success).toBe(true);
  149. if (result.success) {
  150. expect(result.data.contactEmail).toBe('lisi@example.com');
  151. }
  152. });
  153. it('应该验证无效邮箱格式显示中文错误提示', () => {
  154. const data = {
  155. id: 1,
  156. platformName: '更新后的平台',
  157. contactEmail: '无效邮箱'
  158. };
  159. const result = UpdatePlatformSchema.safeParse(data);
  160. expect(result.success).toBe(false);
  161. if (!result.success) {
  162. const errorMessage = result.error.issues[0].message;
  163. expect(errorMessage).toBe('请输入有效的邮箱地址');
  164. }
  165. });
  166. it('应该验证超长邮箱地址显示中文长度错误提示', () => {
  167. const longEmail = 'a'.repeat(100) + '@example.com'; // 超过100个字符
  168. const data = {
  169. id: 1,
  170. platformName: '更新后的平台',
  171. contactEmail: longEmail
  172. };
  173. const result = UpdatePlatformSchema.safeParse(data);
  174. expect(result.success).toBe(false);
  175. if (!result.success) {
  176. const errorMessage = result.error.issues[0].message;
  177. expect(errorMessage).toBe('邮箱地址不能超过100个字符');
  178. }
  179. });
  180. it('应该验证ID必填', () => {
  181. const data = {
  182. // id字段缺失
  183. platformName: '更新后的平台'
  184. };
  185. const result = UpdatePlatformSchema.safeParse(data);
  186. expect(result.success).toBe(false);
  187. if (!result.success) {
  188. const errorMessage = result.error.issues[0].message;
  189. expect(errorMessage).toContain('number');
  190. }
  191. });
  192. it('应该验证平台名称超长显示错误', () => {
  193. const longName = 'a'.repeat(101); // 超过100个字符
  194. const data = {
  195. id: 1,
  196. platformName: longName
  197. };
  198. const result = UpdatePlatformSchema.safeParse(data);
  199. expect(result.success).toBe(false);
  200. if (!result.success) {
  201. const errorMessage = result.error.issues[0].message;
  202. expect(errorMessage).toContain('100');
  203. }
  204. });
  205. });
  206. });