refactor(lqip-loader): split test files (#6858)

This commit is contained in:
Joshua Chen 2022-03-06 18:34:04 +08:00 committed by GitHub
parent f763ac13a9
commit 5e2168ea22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 25 deletions

View file

@ -34,7 +34,7 @@ export default {
}, },
moduleNameMapper: { moduleNameMapper: {
// Jest can't resolve CSS or asset imports // Jest can't resolve CSS or asset imports
'^.+\\.(css|jpg|jpeg|png|svg)$': '<rootDir>/jest/emptyModule.js', '^.+\\.(css|jpe?g|png|svg)$': '<rootDir>/jest/emptyModule.js',
// TODO we need to allow Jest to resolve core Webpack aliases automatically // TODO we need to allow Jest to resolve core Webpack aliases automatically
'@docusaurus/(browserContext|BrowserOnly|ComponentCreator|constants|docusaurusContext|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)': '@docusaurus/(browserContext|BrowserOnly|ComponentCreator|constants|docusaurusContext|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)':

View file

@ -56,7 +56,7 @@ export default function pluginIdealImage(
module: { module: {
rules: [ rules: [
{ {
test: /\.(?:png|jpe?g|gif)$/i, test: /\.(?:png|jpe?g)$/i,
use: [ use: [
require.resolve('@docusaurus/lqip-loader'), require.resolve('@docusaurus/lqip-loader'),
{ {

View file

@ -0,0 +1,29 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import * as lqip from '../lqip';
describe('lqip library', () => {
const imgPath = path.join(__dirname, '__fixtures__', 'endi.jpg');
const invalidPath = path.join(__dirname, '__fixtures__', 'docusaurus.svg');
it('should reject unknown or unsupported file format', async () => {
await expect(lqip.base64(invalidPath)).rejects.toBeTruthy();
});
it('should generate a valid base64', async () => {
const expectedBase64 = 'data:image/jpeg;base64,/9j/2wBDA';
await expect(lqip.base64(imgPath)).resolves.toContain(expectedBase64);
});
it('should generate a valid color palette', async () => {
const imgPalette = await lqip.palette(imgPath);
expect(imgPalette).toHaveLength(6);
expect(imgPalette).toContain('#578ca1');
});
});

View file

@ -10,7 +10,6 @@ import Vibrant from 'node-vibrant';
import type {Palette} from 'node-vibrant/lib/color'; import type {Palette} from 'node-vibrant/lib/color';
import {toPalette, toBase64} from '../utils'; import {toPalette, toBase64} from '../utils';
import * as lqip from '../lqip';
describe('lqip-loader', () => { describe('lqip-loader', () => {
describe('toBase64', () => { describe('toBase64', () => {
@ -44,24 +43,4 @@ describe('lqip-loader', () => {
expect(toPalette(testSwatchWithNull)).toHaveLength(5); expect(toPalette(testSwatchWithNull)).toHaveLength(5);
}); });
}); });
describe('lqip library', () => {
const imgPath = path.join(__dirname, '__fixtures__', 'endi.jpg');
const invalidPath = path.join(__dirname, '__fixtures__', 'docusaurus.svg');
it('should reject unknown or unsupported file format', async () => {
await expect(lqip.base64(invalidPath)).rejects.toBeTruthy();
});
it('should generate a valid base64', async () => {
const expectedBase64 = 'data:image/jpeg;base64,/9j/2wBDA';
await expect(lqip.base64(imgPath)).resolves.toContain(expectedBase64);
});
it('should generate a valid color palette', async () => {
const imgPalette = await lqip.palette(imgPath);
expect(imgPalette).toHaveLength(6);
expect(imgPalette).toContain('#578ca1');
});
});
}); });

View file

@ -25,6 +25,8 @@ export default async function lqipLoader(
const config = this.getOptions() || {}; const config = this.getOptions() || {};
config.base64 = 'base64' in config ? config.base64 : true; config.base64 = 'base64' in config ? config.base64 : true;
// color palette generation is set to false by default
// since it is little bit slower than base64 generation
config.palette = 'palette' in config ? config.palette : false; config.palette = 'palette' in config ? config.palette : false;
let content = contentBuffer.toString('utf8'); let content = contentBuffer.toString('utf8');
@ -53,8 +55,6 @@ export default async function lqipLoader(
const outputPromises: [Promise<string> | null, Promise<string[]> | null] = [ const outputPromises: [Promise<string> | null, Promise<string[]> | null] = [
config.base64 === true ? lqip.base64(imgPath) : null, config.base64 === true ? lqip.base64(imgPath) : null,
// color palette generation is set to false by default
// since it is little bit slower than base64 generation
config.palette === true ? lqip.palette(imgPath) : null, config.palette === true ? lqip.palette(imgPath) : null,
]; ];