@realfavicongenerator/check-favicon
Version:
Check the favicon of a website
173 lines (172 loc) • 8.03 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_html_parser_1 = __importDefault(require("node-html-parser"));
const types_1 = require("./types");
const web_app_manifest_1 = require("./web-app-manifest");
const test_helper_1 = require("./test-helper");
const helper_1 = require("./helper");
const filterOutput = (report) => (Object.assign(Object.assign({}, report), { messages: report.messages.map(m => ({ status: m.status, id: m.id })) }));
const runCheckTouchIconTitleTest = (headFragment_1, output_1, ...args_1) => __awaiter(void 0, [headFragment_1, output_1, ...args_1], void 0, function* (headFragment, output, fetchDatabase = {}) {
const root = headFragment ? (0, node_html_parser_1.default)(headFragment) : null;
const result = yield (0, web_app_manifest_1.checkWebAppManifest)('https://example.com/', root, (0, test_helper_1.testFetcher)(fetchDatabase));
expect(filterOutput(result)).toEqual(Object.assign({ name: undefined, shortName: undefined, backgroundColor: undefined, themeColor: undefined, icon: null }, output));
});
test('checkWebAppManifest - noHead', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest(null, { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noHead,
}] });
}));
test('checkWebAppManifest - noManifest', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest('<title>Hey</title>', { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifest,
}] });
}));
test('checkWebAppManifest - noManifestHref', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest('<link rel="manifest" />', { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestHref,
}] });
}));
test('checkWebAppManifest - manifest404', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest('<link rel="manifest" href="not-found.json" />', { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.manifest404,
}] });
}));
test('checkWebAppManifest - manifestCannotGet', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest('<link rel="manifest" href="/error.json" />', { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.manifestCannotGet,
}] }, {
'https://example.com/error.json': {
status: 500,
contentType: 'application/json',
readableStream: null
}
});
}));
test('checkWebAppManifest - manifestInvalidJson', () => __awaiter(void 0, void 0, void 0, function* () {
yield runCheckTouchIconTitleTest('<link rel="manifest" href="/bad-manifest.json" />', { messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.manifestInvalidJson,
}] }, {
'https://example.com/bad-manifest.json': {
status: 200,
contentType: 'application/json',
readableStream: stringToReadableStream('{ bad JSON }')
}
});
}));
const stringToReadableStream = (str) => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(str));
controller.close();
}
});
return stream;
};
test('checkWebAppManifestFile - Missing fields', () => __awaiter(void 0, void 0, void 0, function* () {
const report = yield (0, web_app_manifest_1.checkWebAppManifestFile)({
name: null,
short_name: null,
background_color: null,
theme_color: null,
icons: []
}, 'https://example.com/', (0, test_helper_1.testFetcher)({}));
expect(filterOutput(report)).toEqual({
messages: [{
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestName,
}, {
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestShortName,
}, {
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestBackgroundColor,
}, {
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestThemeColor,
}, {
status: types_1.CheckerStatus.Error,
id: types_1.MessageId.noManifestIcons,
}],
name: undefined,
shortName: undefined,
backgroundColor: undefined,
themeColor: undefined,
icon: null
});
}));
const testIcon192 = './fixtures/192x192.png';
const testIcon512 = './fixtures/512x512.png';
test('checkWebAppManifestFile - Everything is fine', () => __awaiter(void 0, void 0, void 0, function* () {
const report = yield (0, web_app_manifest_1.checkWebAppManifestFile)({
name: 'My long name',
short_name: 'Short!',
background_color: '#123456',
theme_color: '#abcdef',
icons: [
{ src: '/icon-192.png', sizes: '192x192', type: 'image/png' },
{ src: '/icon-512.png', sizes: '512x512', type: 'image/png' }
]
}, 'https://example.com/', (0, test_helper_1.testFetcher)({
'https://example.com/icon-192.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(testIcon192)
},
'https://example.com/icon-512.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(testIcon512)
}
}));
const expectedIconReport = [
{
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestIconDeclared,
}, {
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestIconDownloadable,
}, {
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestIconRightSize,
}
];
expect(filterOutput(report)).toEqual({
messages: [{
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestName,
}, {
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestShortName,
}, {
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestBackgroundColor,
}, {
status: types_1.CheckerStatus.Ok,
id: types_1.MessageId.manifestThemeColor,
},
...expectedIconReport, ...expectedIconReport], // Two icons
name: 'My long name',
shortName: 'Short!',
backgroundColor: '#123456',
themeColor: '#abcdef',
icon: (0, helper_1.bufferToDataUrl)(yield (0, helper_1.readableStreamToBuffer)(yield (0, helper_1.filePathToReadableStream)(testIcon512)), 'image/png')
});
}));