Files
Chris Troutner e77e174c92 Refactor post image rendering and cover image helpers
Extract the pure failed-image set transition into a testable service,
DRY the acceptance no-link/no-image assertions, and add property tests
for image URL detection, alt text, and image rendering.

By refactorer.
2026-09-16 07:20:40 -07:00

38 lines
1.3 KiB
JavaScript

/*
Unit tests for the failed-image state transition used by the post renderer.
The transition is pure: it either returns the same set (when the URL is
already tracked, so React can skip a re-render) or a new set with the URL
added, and it never mutates the input set.
*/
'use strict'
const test = require('node:test')
const assert = require('node:assert/strict')
const { addFailedImage } = require('../../src/services/failed-images')
const URL_A = 'https://i.imgur.com/swCI56T.jpeg'
const URL_B = 'https://cdn.example.com/pics/Sunset.PNG'
test('addFailedImage returns a new set containing the added URL', () => {
const original = new Set()
const next = addFailedImage(original, URL_A)
assert.notEqual(next, original)
assert.deepEqual([...next], [URL_A])
assert.equal(original.size, 0)
})
test('addFailedImage returns the same set when the URL is already present', () => {
const original = new Set([URL_A])
const next = addFailedImage(original, URL_A)
assert.equal(next, original)
})
test('addFailedImage preserves existing entries and does not mutate the input', () => {
const original = new Set([URL_A])
const next = addFailedImage(original, URL_B)
assert.deepEqual([...next].sort(), [URL_B, URL_A].sort())
assert.deepEqual([...original], [URL_A])
})