indicatorSeeds.test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { parseSeedsText } from '@/utils/indicatorSeeds'
  2. describe('parseSeedsText', () => {
  3. test('empty/null/undefined → []', () => {
  4. expect(parseSeedsText('')).toEqual([])
  5. expect(parseSeedsText(null)).toEqual([])
  6. expect(parseSeedsText(undefined)).toEqual([])
  7. })
  8. test('JSON array with definitionId', () => {
  9. const input = '[{"rawName":"乳酸菌","definitionId":3},{"rawName":"双歧杆菌"}]'
  10. const res = parseSeedsText(input)
  11. expect(res).toHaveLength(2)
  12. expect(res[0]).toEqual({ rawName: '乳酸菌', definitionId: 3 })
  13. expect(res[1]).toEqual({ rawName: '双歧杆菌', definitionId: null })
  14. })
  15. test('CSV two columns', () => {
  16. const input = '乳酸菌,3\n双歧杆菌\n'
  17. const res = parseSeedsText(input)
  18. expect(res).toHaveLength(2)
  19. expect(res[0]).toEqual({ rawName: '乳酸菌', definitionId: 3 })
  20. expect(res[1]).toEqual({ rawName: '双歧杆菌', definitionId: null })
  21. })
  22. test('skip empty rawName', () => {
  23. const input = '乳酸菌,3\n ,\n双歧杆菌'
  24. const res = parseSeedsText(input)
  25. expect(res).toHaveLength(2)
  26. expect(res[0].rawName).toBe('乳酸菌')
  27. expect(res[1].rawName).toBe('双歧杆菌')
  28. })
  29. test('skip blank lines', () => {
  30. const input = '乳酸菌,3\n\n\n双歧杆菌\n'
  31. const res = parseSeedsText(input)
  32. expect(res).toHaveLength(2)
  33. })
  34. test('invalid JSON falls back to CSV row parsing', () => {
  35. const input = 'not json at all\n乳酸菌,3\n'
  36. const res = parseSeedsText(input)
  37. expect(res).toEqual([{ rawName: '乳酸菌', definitionId: 3 }])
  38. })
  39. test('non-numeric definitionId in CSV → null', () => {
  40. const input = '乳酸菌,abc\n'
  41. const res = parseSeedsText(input)
  42. expect(res).toEqual([{ rawName: '乳酸菌', definitionId: null }])
  43. })
  44. test('Chinese comma delimiter', () => {
  45. const input = '乳酸菌,3\n'
  46. const res = parseSeedsText(input)
  47. expect(res).toEqual([{ rawName: '乳酸菌', definitionId: 3 }])
  48. })
  49. })