A bit of JS to improve the UX slightly... More tests. Everything is kosher now.
This commit is contained in:
44
tests/unit/copyright-year.test.ts
Normal file
44
tests/unit/copyright-year.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const SCRIPT_PATH = join(__dirname, '../../static/assets/js/copyright-year.js');
|
||||
|
||||
describe('copyright-year.js', () => {
|
||||
let el: HTMLSpanElement;
|
||||
|
||||
beforeEach(() => {
|
||||
el = document.createElement('span');
|
||||
el.id = 'copyright-year';
|
||||
el.textContent = 'placeholder';
|
||||
document.body.appendChild(el);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
el.remove();
|
||||
});
|
||||
|
||||
it('sets #copyright-year textContent to current year when element exists', () => {
|
||||
const code = readFileSync(SCRIPT_PATH, 'utf8');
|
||||
|
||||
eval(code);
|
||||
|
||||
const expected = new Date().getFullYear().toString();
|
||||
expect(el.textContent).toBe(expected);
|
||||
});
|
||||
|
||||
it('does not throw when #copyright-year is missing', () => {
|
||||
el.remove();
|
||||
const code = readFileSync(SCRIPT_PATH, 'utf8');
|
||||
|
||||
expect(() => {
|
||||
|
||||
eval(code);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user