choiceChecklistHelpers.test.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. allChoiceValues,
  5. choiceLabel,
  6. formatChecklistValue,
  7. parseChecklistValue,
  8. toggleChoice
  9. } from './choiceChecklistHelpers.js'
  10. const choices = [
  11. { title: 'Documents', value: 'documents' },
  12. { title: 'Photos', value: 'photos' }
  13. ]
  14. test('parseChecklistValue parses JSON-encoded values', () => {
  15. assert.deepEqual(parseChecklistValue('["documents","photos"]'), ['documents', 'photos'])
  16. assert.deepEqual(parseChecklistValue('["kitchen,bedroom","hallway"]'), ['kitchen,bedroom', 'hallway'])
  17. assert.deepEqual(parseChecklistValue(''), [])
  18. })
  19. test('parseChecklistValue accepts legacy comma-delimited values', () => {
  20. assert.deepEqual(parseChecklistValue('documents,photos'), ['documents', 'photos'])
  21. assert.deepEqual(parseChecklistValue('documents, photos'), ['documents', 'photos'])
  22. })
  23. test('formatChecklistValue joins selected values as JSON', () => {
  24. assert.equal(formatChecklistValue(['documents', 'photos']), '["documents","photos"]')
  25. assert.equal(formatChecklistValue(['kitchen,bedroom']), '["kitchen,bedroom"]')
  26. assert.equal(formatChecklistValue([]), '')
  27. })
  28. test('toggleChoice adds and removes values', () => {
  29. assert.deepEqual(toggleChoice([], 'documents'), ['documents'])
  30. assert.deepEqual(toggleChoice(['documents'], 'photos'), ['documents', 'photos'])
  31. assert.deepEqual(toggleChoice(['documents', 'photos'], 'documents'), ['photos'])
  32. })
  33. test('choiceLabel prefers title over value', () => {
  34. assert.equal(choiceLabel(choices[0]), 'Documents')
  35. assert.equal(choiceLabel({ value: 'music' }), 'music')
  36. })
  37. test('allChoiceValues returns every choice value', () => {
  38. assert.deepEqual(allChoiceValues(choices), ['documents', 'photos'])
  39. })