refactor: reorganize tests, reduce repetition (#6829)

* refactor: reorganize tests, reduce repetition

* fix conflicts

* fix conflicts

* remove unnecessary type checks and use toStrictEqual

* move index tests to separate file

* test svg values in testIcon
This commit is contained in:
Sachin Raja 2021-11-08 02:55:47 -08:00 committed by GitHub
parent 761d82b044
commit 7c8146e475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 113 additions and 146 deletions

4
jest.config.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
testMatch: ['**/tests/**/*.test.?(m)js'],
moduleFileExtensions: ['js', 'mjs', 'json'],
};

View File

@ -61,7 +61,7 @@
"prepare": "is-ci || husky install",
"prepublishOnly": "npm run build",
"postpublish": "npm run clean",
"test": "jest",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"pretest": "npm run prepublishOnly",
"posttest": "npm run postpublish",
"svgo": "svgo --config svgo.config.js",

View File

@ -30,7 +30,12 @@ const indexTemplate = fs.readFileSync(indexTemplateFile, UTF8);
const iconObjectTemplate = fs.readFileSync(iconObjectTemplateFile, UTF8);
const data = require(dataFile);
const { getIconSlug, svgToPath, titleToHtmlFriendly } = require('../utils.js');
const {
getIconSlug,
svgToPath,
titleToHtmlFriendly,
slugToVariableName,
} = require('../utils.js');
// Local helper functions
const escape = (value) => {
@ -62,11 +67,6 @@ const iconToObject = (icon) => {
licenseToObject(icon.license),
);
};
const slugToVariableName = (slug) => {
const slugFirstLetter = slug[0].toUpperCase();
const slugRest = slug.slice(1);
return `si${slugFirstLetter}${slugRest}`;
};
const writeJs = (filepath, rawJavaScript) => {
const { error, code } = minify(rawJavaScript);
if (error) {

View File

@ -50,6 +50,16 @@ module.exports = {
(_, ref) => ({ quot: '"', amp: '&', lt: '<', gt: '>' }[ref]),
),
/**
* Converts a slug into a variable name that can be exported.
* @param {String} slug The slug to convert
*/
slugToVariableName: (slug) => {
const slugFirstLetter = slug[0].toUpperCase();
const slugRest = slug.slice(1);
return `si${slugFirstLetter}${slugRest}`;
},
/**
* Converts a brand title (as it is seen in simple-icons.json) into a brand
* title in HTML/SVG friendly format.

View File

@ -1,75 +1,10 @@
const fs = require('fs');
const path = require('path');
const { icons } = require('../_data/simple-icons.json');
const { getIconSlug } = require('../scripts/utils.js');
const iconsDir = path.resolve(__dirname, '..', 'icons');
const testIcon = require('./test-icon.js');
icons.forEach((icon) => {
const filename = getIconSlug(icon);
const subject = require(`../icons/${filename}.js`);
const svgPath = path.resolve(iconsDir, `${filename}.svg`);
const slug = getIconSlug(icon);
const subject = require(`../icons/${slug}.js`);
test(`${icon.title} has the correct "title"`, () => {
expect(typeof subject.title).toBe('string');
expect(subject.title).toEqual(icon.title);
});
test(`${icon.title} has the correct "slug"`, () => {
expect(typeof subject.slug).toBe('string');
expect(subject.slug).toEqual(getIconSlug(icon));
});
test(`${icon.title} has the correct "hex" value`, () => {
expect(typeof subject.hex).toBe('string');
expect(subject.hex).toEqual(icon.hex);
});
test(`${icon.title} has the correct "source"`, () => {
expect(typeof subject.source).toBe('string');
expect(subject.source).toEqual(icon.source);
});
test(`${icon.title} has a valid "svg" value`, () => {
expect(typeof subject.svg).toBe('string');
const svgFileContents = fs
.readFileSync(svgPath, 'utf8')
.replace(/\r?\n/, '');
expect(subject.svg.substring(subject.svg.indexOf('<title>'))).toEqual(
svgFileContents.substring(svgFileContents.indexOf('<title>')),
);
});
test(`${icon.title} has a valid "path" value`, () => {
expect(typeof subject.path).toBe('string');
expect(subject.path).toMatch(/^[MmZzLlHhVvCcSsQqTtAaEe0-9-,.\s]+$/g);
});
test(`${icon.title} has ${
icon.guidelines ? 'the correct' : 'no'
} "guidelines"`, () => {
if (icon.guidelines) {
expect(typeof subject.guidelines).toBe('string');
expect(subject.guidelines).toEqual(icon.guidelines);
} else {
expect(subject.guidelines).toBeUndefined();
}
});
test(`${icon.title} has ${
icon.license ? 'the correct' : 'no'
} "license"`, () => {
if (icon.license) {
expect(typeof subject.license).toBe('object');
expect(subject.license).toHaveProperty('type', icon.license.type);
if (icon.license.type === 'custom') {
expect(subject.license).toHaveProperty('url', icon.license.url);
} else {
expect(typeof subject.license.url).toBe('string');
expect(subject.license.url).toMatch(/^https?:\/\/[^\s]+$/);
}
} else {
expect(subject.license).toBeUndefined();
}
});
testIcon(icon, subject, slug);
});

14
tests/icons.test.mjs Normal file
View File

@ -0,0 +1,14 @@
import simpleIconsData from '../_data/simple-icons.json';
import utils from '../scripts/utils.js';
import * as simpleIcons from '../icons.mjs';
import testIcon from './test-icon.js';
const { getIconSlug, slugToVariableName } = utils;
simpleIconsData.icons.forEach((icon) => {
const slug = getIconSlug(icon);
const variableName = slugToVariableName(slug);
const subject = simpleIcons[variableName];
testIcon(icon, subject, slug);
});

View File

@ -1,78 +1,9 @@
const path = require('path');
const fs = require('fs');
const { icons } = require('../_data/simple-icons.json');
const simpleIcons = require('../index.js');
const { getIconSlug } = require('../scripts/utils.js');
const iconsDir = path.resolve(__dirname, '..', 'icons');
const { getIconSlug } = require('../scripts/utils');
icons.forEach((icon) => {
const slug = getIconSlug(icon);
const subject = simpleIcons[slug];
const svgPath = path.resolve(iconsDir, `${slug}.svg`);
test(`${icon.title} has the correct "title"`, () => {
expect(typeof subject.title).toBe('string');
expect(subject.title).toEqual(icon.title);
});
test(`${icon.title} has the correct "slug"`, () => {
expect(typeof subject.slug).toBe('string');
expect(subject.slug).toEqual(slug);
});
test(`${icon.title} has the correct "hex" value`, () => {
expect(typeof subject.hex).toBe('string');
expect(subject.hex).toEqual(icon.hex);
});
test(`${icon.title} has the correct "source"`, () => {
expect(typeof subject.source).toBe('string');
expect(subject.source).toEqual(icon.source);
});
test(`${icon.title} has a valid "svg" value`, () => {
expect(typeof subject.svg).toBe('string');
const svgFileContents = fs
.readFileSync(svgPath, 'utf8')
.replace(/\r?\n/, '');
expect(subject.svg.substring(subject.svg.indexOf('<title>'))).toEqual(
svgFileContents.substring(svgFileContents.indexOf('<title>')),
);
});
test(`${icon.title} has a valid "path" value`, () => {
expect(typeof subject.path).toBe('string');
expect(subject.path).toMatch(/^[MmZzLlHhVvCcSsQqTtAaEe0-9-,.\s]+$/g);
});
test(`${icon.title} has ${
icon.guidelines ? 'the correct' : 'no'
} "guidelines"`, () => {
if (icon.guidelines) {
expect(typeof subject.guidelines).toBe('string');
expect(subject.guidelines).toEqual(icon.guidelines);
} else {
expect(subject.guidelines).toBeUndefined();
}
});
test(`${icon.title} has ${
icon.license ? 'the correct' : 'no'
} "license"`, () => {
if (icon.license) {
expect(typeof subject.license).toBe('object');
expect(subject.license).toHaveProperty('type', icon.license.type);
if (icon.license.type === 'custom') {
expect(subject.license).toHaveProperty('url', icon.license.url);
} else {
expect(typeof subject.license.url).toBe('string');
expect(subject.license.url).toMatch(/^https?:\/\/[^\s]+$/);
}
} else {
expect(subject.license).toBeUndefined();
}
});
test(`'Get' ${icon.title} by its slug`, () => {
const found = simpleIcons.Get(slug);

73
tests/test-icon.js Normal file
View File

@ -0,0 +1,73 @@
const fs = require('fs');
const path = require('path');
const iconsDir = path.resolve(process.cwd(), 'icons');
/**
* Checks if icon data matches a subject icon.
* @param {import('..').SimpleIcon} icon Icon data
* @param {import('..').SimpleIcon} subject Icon to check against icon data
* @param {String} slug Icon data slug
*/
const testIcon = (icon, subject, slug) => {
describe(icon.title, () => {
const svgPath = path.resolve(iconsDir, `${slug}.svg`);
it('has the correct "title"', () => {
expect(subject.title).toStrictEqual(icon.title);
});
it('has the correct "slug"', () => {
expect(subject.slug).toStrictEqual(slug);
});
it('has the correct "hex" value', () => {
expect(subject.hex).toStrictEqual(icon.hex);
});
it('has the correct "source"', () => {
expect(subject.source).toStrictEqual(icon.source);
});
it('has an "svg" value', () => {
expect(typeof subject.svg).toBe('string');
});
it('has a valid "path" value', () => {
expect(subject.path).toMatch(/^[MmZzLlHhVvCcSsQqTtAaEe0-9-,.\s]+$/g);
});
it(`has ${icon.guidelines ? 'the correct' : 'no'} "guidelines"`, () => {
if (icon.guidelines) {
expect(subject.guidelines).toStrictEqual(icon.guidelines);
} else {
expect(subject.guidelines).toBeUndefined();
}
});
it(`has ${icon.license ? 'the correct' : 'no'} "license"`, () => {
if (icon.license) {
expect(subject.license).toHaveProperty('type', icon.license.type);
if (icon.license.type === 'custom') {
expect(subject.license).toHaveProperty('url', icon.license.url);
} else {
expect(subject.license.url).toMatch(/^https?:\/\/[^\s]+$/);
}
} else {
expect(subject.license).toBeUndefined();
}
});
it('has a valid svg value', () => {
const svgFileContents = fs
.readFileSync(svgPath, 'utf8')
.replace(/\r?\n/, '');
expect(subject.svg.substring(subject.svg.indexOf('<title>'))).toEqual(
svgFileContents.substring(svgFileContents.indexOf('<title>')),
);
});
});
};
module.exports = testIcon;