mp-crm-product-card.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import assert from 'node:assert/strict'
  2. import { describe, it } from 'node:test'
  3. import {
  4. MP_PRODUCT_STATUS_FILTERS,
  5. buildProductListQuery,
  6. buildProductCard,
  7. productStatusLabel,
  8. productStatusTagType
  9. } from './mp-crm-product-card'
  10. describe('MP_PRODUCT_STATUS_FILTERS', () => {
  11. it('defaults on first and includes all/off', () => {
  12. assert.equal(MP_PRODUCT_STATUS_FILTERS[0].value, 'on')
  13. assert.deepEqual(
  14. MP_PRODUCT_STATUS_FILTERS.map((x) => x.value),
  15. ['on', 'all', 'off']
  16. )
  17. })
  18. })
  19. describe('buildProductListQuery', () => {
  20. it('defaults statusIn on and passes keyword/page', () => {
  21. assert.deepEqual(
  22. buildProductListQuery({ page: 1, pageSize: 20, statusFilter: 'on', keyword: ' 酸 ' }),
  23. { page: 1, pageSize: 20, statusIn: 'on', keyword: '酸' }
  24. )
  25. })
  26. it('omits statusIn when filter is all', () => {
  27. const q = buildProductListQuery({ page: 2, pageSize: 10, statusFilter: 'all', keyword: '' })
  28. assert.equal(q.page, 2)
  29. assert.equal(q.statusIn, undefined)
  30. assert.equal('keyword' in q, false)
  31. })
  32. it('sets statusIn off', () => {
  33. assert.equal(
  34. buildProductListQuery({ page: 1, pageSize: 20, statusFilter: 'off', keyword: '' }).statusIn,
  35. 'off'
  36. )
  37. })
  38. })
  39. describe('buildProductCard', () => {
  40. it('maps fields and tags', () => {
  41. const card = buildProductCard({
  42. id: 'p1',
  43. productNo: 'SX001',
  44. productName: '硫酸',
  45. categoryPath: '化工 / 酸类',
  46. model: '98%',
  47. status: 'on',
  48. isToxic: 1,
  49. isExplosive: 0
  50. })
  51. assert.equal(card.id, 'p1')
  52. assert.equal(card.productNo, 'SX001')
  53. assert.equal(card.statusLabel, '上架')
  54. assert.equal(card.statusTagType, 'success')
  55. assert.equal(card.showToxic, true)
  56. assert.equal(card.showExplosive, false)
  57. })
  58. })
  59. describe('productStatusTagType', () => {
  60. it('maps on→success off→info', () => {
  61. assert.equal(productStatusTagType('on'), 'success')
  62. assert.equal(productStatusTagType('off'), 'info')
  63. assert.equal(productStatusLabel('on'), '上架')
  64. assert.equal(productStatusLabel('off'), '下架')
  65. })
  66. })