Files
graylog2-server/graylog2-web-interface/webpack/UniqueChunkIdPlugin.test.js
Dennis Oelkers 6a8994740f Updating to Webpack 5. (#11626)
* Updating to Webpack 5.

* Adding explicit dependency for `buffer`.

* Updating vendor module ids.

* Logging error when loading component async fails.

* Adding root directory of plugin as parameter to `PluginWebpackConfig`.

* Removing unused loaders.

* Revert "Removing unused loaders."

This reverts commit 7ec4d8e0717f0a3eb35c8db829fd91b4d7f8cb03.

* Slight updates of webpack packages.

* Disabling linter hints.

* Adjusting tests.

* Updating terser plugin, removing obsolete source map option.

* Fixing import order.

* Readding `HashedModuleIdsPlugin`.

* Updating yarn lockfile.

* Updating vendor module ids.

* Removing explicit loader references.
2021-12-02 12:11:53 +01:00

100 lines
3.1 KiB
JavaScript

/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import UniqueChunkIdPlugin from './UniqueChunkIdPlugin';
jest.mock('crypto', () => ({
randomBytes: () => ({
toString: () => 'dead',
}),
}));
const mockCompiler = () => {
const compilation = {
hooks: {
optimizeChunkIds: {
tap: jest.fn(),
},
},
};
const compiler = {
hooks: {
compilation: {
tap: jest.fn((_, f) => f(compilation)),
},
},
};
return { compiler, compilation };
};
describe('UniqueChunkIdPlugin', () => {
it('register itself properly', () => {
const uniqueChunkIdPlugin = new UniqueChunkIdPlugin();
const { compiler, compilation } = mockCompiler();
uniqueChunkIdPlugin.apply(compiler);
expect(compiler.hooks.compilation.tap).toHaveBeenCalled();
expect(compiler.hooks.compilation.tap.mock.calls[0][0]).toEqual('UniqueChunkIdPlugin');
expect(compilation.hooks.optimizeChunkIds.tap).toHaveBeenCalled();
expect(compilation.hooks.optimizeChunkIds.tap.mock.calls[0][0]).toEqual('UniqueChunkIdPlugin');
});
it('adds random identifier as a prefix for any given chunk', () => {
const chunks = [{ id: 0, ids: [0] }, { id: 42, ids: [0, 42] }];
const uniqueChunkIdPlugin = new UniqueChunkIdPlugin();
const { compiler, compilation } = mockCompiler();
uniqueChunkIdPlugin.apply(compiler);
const callback = compilation.hooks.optimizeChunkIds.tap.mock.calls[0][1];
const result = callback(chunks);
expect(result.size).toBe(2);
expect(result).toMatchSnapshot();
});
it('adds random identifier as a prefix for chunks with string ids', () => {
const chunks = [{ id: 'foo', ids: ['foo'] }, { id: 'bar', ids: ['foo', 'bar'] }];
const uniqueChunkIdPlugin = new UniqueChunkIdPlugin();
const { compiler, compilation } = mockCompiler();
uniqueChunkIdPlugin.apply(compiler);
const callback = compilation.hooks.optimizeChunkIds.tap.mock.calls[0][1];
const result = callback(chunks);
expect(result.size).toBe(2);
expect(result).toMatchSnapshot();
});
it('does not add random identifier if already present', () => {
const chunks = [{ id: 'dead-0', ids: ['dead-0'] }, { id: 42, ids: [0, 42] }];
const uniqueChunkIdPlugin = new UniqueChunkIdPlugin();
const { compiler, compilation } = mockCompiler();
uniqueChunkIdPlugin.apply(compiler);
const callback = compilation.hooks.optimizeChunkIds.tap.mock.calls[0][1];
const result = callback(chunks);
expect(result.size).toBe(2);
expect(result).toMatchSnapshot();
});
});