2024-03-24 19:38:18 +02:00
|
|
|
import {strict as assert} from 'node:assert';
|
2023-08-08 06:38:52 +02:00
|
|
|
import fs from 'node:fs/promises';
|
2021-12-25 16:22:56 +02:00
|
|
|
import path from 'node:path';
|
2024-03-24 19:38:18 +02:00
|
|
|
import {describe, it} from 'mocha';
|
2023-08-08 06:38:52 +02:00
|
|
|
import {
|
|
|
|
SVG_PATH_REGEX,
|
|
|
|
URL_REGEX,
|
|
|
|
getDirnameFromImportMeta,
|
|
|
|
titleToSlug,
|
|
|
|
} from '../sdk.mjs';
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const iconsDirectory = path.resolve(
|
2023-08-08 06:38:52 +02:00
|
|
|
getDirnameFromImportMeta(import.meta.url),
|
|
|
|
'..',
|
|
|
|
'icons',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('..').SimpleIcon} SimpleIcon
|
|
|
|
*/
|
2021-11-08 12:55:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if icon data matches a subject icon.
|
2023-08-08 06:38:52 +02:00
|
|
|
* @param {SimpleIcon} icon Icon data
|
|
|
|
* @param {SimpleIcon} subject Icon to check against icon data
|
2021-11-08 12:55:47 +02:00
|
|
|
* @param {String} slug Icon data slug
|
|
|
|
*/
|
2021-12-25 16:22:56 +02:00
|
|
|
export const testIcon = (icon, subject, slug) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
const svgPath = path.resolve(iconsDirectory, `${slug}.svg`);
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
describe(icon.title, () => {
|
|
|
|
it('has the correct "title"', () => {
|
|
|
|
assert.equal(subject.title, icon.title);
|
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it('has the correct "slug"', () => {
|
|
|
|
assert.equal(subject.slug, slug);
|
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it('has the correct "hex" value', () => {
|
|
|
|
assert.equal(subject.hex, icon.hex);
|
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it('has the correct "source"', () => {
|
|
|
|
assert.equal(subject.source, icon.source);
|
2022-09-24 17:37:03 +02:00
|
|
|
assert.match(subject.source, URL_REGEX);
|
2022-01-19 19:23:32 +02:00
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it('has an "svg" value', () => {
|
|
|
|
assert.equal(typeof subject.svg, 'string');
|
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it('has a valid "path" value', () => {
|
2023-08-08 06:38:52 +02:00
|
|
|
assert.match(subject.path, SVG_PATH_REGEX);
|
2022-01-19 19:23:32 +02:00
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it(`has ${icon.guidelines ? 'the correct' : 'no'} "guidelines"`, () => {
|
|
|
|
if (icon.guidelines) {
|
|
|
|
assert.equal(subject.guidelines, icon.guidelines);
|
|
|
|
} else {
|
|
|
|
assert.equal(subject.guidelines, undefined);
|
|
|
|
}
|
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2022-01-19 19:23:32 +02:00
|
|
|
it(`has ${icon.license ? 'the correct' : 'no'} "license"`, () => {
|
|
|
|
if (icon.license) {
|
|
|
|
assert.equal(subject.license.type, icon.license.type);
|
|
|
|
if (icon.license.type === 'custom') {
|
|
|
|
assert.equal(subject.license.url, icon.license.url);
|
|
|
|
} else {
|
2022-09-24 17:37:03 +02:00
|
|
|
assert.match(subject.license.url, URL_REGEX);
|
2022-01-19 19:23:32 +02:00
|
|
|
}
|
2021-11-08 12:55:47 +02:00
|
|
|
} else {
|
2022-01-19 19:23:32 +02:00
|
|
|
assert.equal(subject.license, undefined);
|
2021-11-08 12:55:47 +02:00
|
|
|
}
|
2022-01-19 19:23:32 +02:00
|
|
|
});
|
2021-11-08 12:55:47 +02:00
|
|
|
|
2023-08-08 06:38:52 +02:00
|
|
|
it('has a valid svg value', async () => {
|
|
|
|
const svgFileContents = await fs.readFile(svgPath, 'utf8');
|
2022-01-19 19:23:32 +02:00
|
|
|
assert.equal(subject.svg, svgFileContents);
|
|
|
|
});
|
2022-11-28 13:32:57 +02:00
|
|
|
|
|
|
|
if (icon.slug) {
|
2024-03-24 19:38:18 +02:00
|
|
|
// If an icon data has a slug, it must be different to the
|
2022-11-28 13:32:57 +02:00
|
|
|
// slug inferred from the title, which prevents adding
|
|
|
|
// unnecessary slugs to icons data
|
|
|
|
it(`'${icon.title}' slug must be necessary`, () => {
|
|
|
|
assert.notEqual(titleToSlug(icon.title), icon.slug);
|
|
|
|
});
|
|
|
|
}
|
2021-11-08 12:55:47 +02:00
|
|
|
});
|
|
|
|
};
|