Files
NODEDC_SITE/test/carousel-render.test.mjs
T

41 lines
2.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, cp, readFile, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';
// Exercise the real renderer and both supported templates in an isolated project.
test('image opening is opt-in per carousel and survives ordinary model rendering', async t => {
const root = await mkdtemp(join(tmpdir(), 'nodedc-carousel-'));
t.after(() => rm(root, { recursive: true, force: true }));
for (const path of ['tools', 'templates', 'content']) await cp(new URL('../'+path, import.meta.url), join(root,path), {recursive:true});
const modelPath = join(root, 'content/pages/home.json');
const page = JSON.parse(await readFile(modelPath, 'utf8'));
const galleries = page.regions.flatMap(r=>r.blocks).filter(b=>b.content?.sliderItems);
assert.equal(galleries.length, 4);
const render = async () => {
await writeFile(modelPath, JSON.stringify(page));
execFileSync(process.execPath, ['tools/render-home.mjs'], {cwd:root});
return await readFile(join(root, 'index.html'),'utf8');
};
const count = (html, pattern) => [...html.matchAll(pattern)].length;
const baseline = await render();
assert.equal(count(baseline, /— открыть изображение/g), 0);
const mediaCount = count(baseline, /class="carousel-media"/g);
const videoCount = count(baseline, /<video /g);
galleries[0].content.openImagesInFullscreen = true;
galleries[1].template = 'project-composition.html';
galleries[1].content.openImagesInFullscreen = true;
const enabled = await render();
const expected = galleries.slice(0,2).flatMap(b=>b.content.sliderItems).filter(i=>i.enabled && i.imageSrc).length;
assert.equal(count(enabled, /— открыть изображение/g), expected);
assert.equal(count(enabled, /class="carousel-media"/g), mediaCount);
assert.equal(count(enabled, /<video /g), videoCount);
delete galleries[0].content.openImagesInFullscreen;
galleries[1].content.openImagesInFullscreen = 'false';
assert.equal(count(await render(), /— открыть изображение/g), 0);
galleries.forEach(b=>b.content.openImagesInFullscreen=false);
assert.equal(count(await render(), /— открыть изображение/g), 0);
});