@realfavicongenerator/check-favicon
Version:
Check the favicon of a website
141 lines (140 loc) • 7.05 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const helper_1 = require("./helper");
const test_helper_1 = require("./test-helper");
const getTestProcessor = () => {
const messages = [];
const processor = {
noHref: () => { messages.push('noHref'); },
icon404: () => { messages.push('icon404'); },
cannotGet: (httpStatusCode) => { messages.push(`cannotGet ${httpStatusCode}`); },
downloadable: () => { messages.push('downloadable'); },
square: (widthHeight) => { messages.push(`square ${widthHeight}`); },
notSquare: (width, Height) => { messages.push(`notSquare ${width}x${Height}`); },
rightSize: (width) => { messages.push(`rightSize ${width}x${width}`); },
wrongSize: (widthHeight) => { messages.push(`wrongSize ${widthHeight}x${widthHeight}`); }
};
return { processor, messages };
};
const testIcon = './fixtures/logo-transparent.png';
const nonSquareIcon = './fixtures/non-square.png';
test('checkIcon - noHref', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)(undefined, processor.processor, (0, test_helper_1.testFetcher)({}), 'image/png')).toBeNull();
expect(processor.messages).toEqual(['noHref']);
}));
test('checkIcon - icon404', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/does-not-exist.png', processor.processor, (0, test_helper_1.testFetcher)({}), 'image/png')).toEqual({
content: null,
url: '/does-not-exist.png',
width: null,
height: null
});
expect(processor.messages).toEqual(['icon404']);
}));
test('checkIcon - icon404', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/bad-icon.png', processor.processor, (0, test_helper_1.testFetcher)({
'/bad-icon.png': {
contentType: 'image/png',
status: 500
}
}), 'image/png')).toEqual({
content: null,
url: '/bad-icon.png',
width: null,
height: null
});
expect(processor.messages).toEqual(['cannotGet 500']);
}));
test('checkIcon - downloadable & square', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/some-icon.png', processor.processor, (0, test_helper_1.testFetcher)({
'/some-icon.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(testIcon)
}
}), 'image/png')).not.toBeNull();
expect(processor.messages).toEqual([
'downloadable',
'square 754'
]);
}));
test('checkIcon - downloadable & rightSize', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/some-icon.png', processor.processor, (0, test_helper_1.testFetcher)({
'/some-icon.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(testIcon)
}
}), 'image/png', 754)).not.toBeNull();
expect(processor.messages).toEqual([
'downloadable',
'square 754',
'rightSize 754x754'
]);
}));
test('checkIcon - downloadable & wrongSize', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/some-icon.png', processor.processor, (0, test_helper_1.testFetcher)({
'/some-icon.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(testIcon)
}
}), 'image/png', 500)).not.toBeNull();
expect(processor.messages).toEqual([
'downloadable',
'square 754',
'wrongSize 754x754'
]);
}));
test('checkIcon - downloadable & notSquare', () => __awaiter(void 0, void 0, void 0, function* () {
const processor = getTestProcessor();
expect(yield (0, helper_1.checkIcon)('/non-square-icon.png', processor.processor, (0, test_helper_1.testFetcher)({
'/non-square-icon.png': {
status: 200,
contentType: 'image/png',
readableStream: yield (0, helper_1.filePathToReadableStream)(nonSquareIcon)
}
}), 'image/png', 500)).toEqual({
content: yield (0, helper_1.filePathToDataUrl)(nonSquareIcon),
url: '/non-square-icon.png',
width: 240,
height: 180
});
expect(processor.messages).toEqual([
'downloadable',
'notSquare 240x180'
]);
}));
test('mergeUrlAndPath', () => {
expect((0, helper_1.mergeUrlAndPath)('https://example.com', '/some-path')).toBe('https://example.com/some-path');
expect((0, helper_1.mergeUrlAndPath)('https://example.com', 'some/path')).toBe('https://example.com/some/path');
expect((0, helper_1.mergeUrlAndPath)('https://example.com', 'some/path?some=param&and=other-param')).toBe('https://example.com/some/path?some=param&and=other-param');
expect((0, helper_1.mergeUrlAndPath)('https://example.com/sub-page', '/some-path')).toBe('https://example.com/some-path');
expect((0, helper_1.mergeUrlAndPath)('https://example.com/sub-page', 'some/path')).toBe('https://example.com/sub-page/some/path');
expect((0, helper_1.mergeUrlAndPath)('https://example.com', 'https://elsewhere.com/some-path')).toBe('https://elsewhere.com/some-path');
// Protocol-relative URL
// For https://github.com/RealFaviconGenerator/core/issues/2
expect((0, helper_1.mergeUrlAndPath)('https://example.com', '//elsewhere.com/some-path')).toBe('https://elsewhere.com/some-path');
expect((0, helper_1.mergeUrlAndPath)('http://example.com', '//elsewhere.com/some-other/path')).toBe('http://elsewhere.com/some-other/path');
});
test('parseSizesAttribute', () => {
expect((0, helper_1.parseSizesAttribute)(null)).toEqual(null);
expect((0, helper_1.parseSizesAttribute)('dummy')).toEqual(null);
expect((0, helper_1.parseSizesAttribute)("16x16")).toEqual(16);
expect((0, helper_1.parseSizesAttribute)("50x170")).toEqual(null);
});