HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode/frontend/node_modules/string-replace-webpack-plugin/test.js
/**
 * Created by jandersen on 3/23/15.
 */
var assert = require("assert");
var StringReplacePlugin = require("./index.js");

describe('StringReplacePlugin', function(){
    describe('#replace()', function(){
        it('should throw with invalid options', function(){
            assert.throws(function() {
                StringReplacePlugin.replace({
                    replacements: []
                })
            },
            /Invalid options/);

            assert.throws(function() {
                StringReplacePlugin.replace({})
            },
            /Invalid options/);
        });

        it('should not throw with valid options', function(){
            assert.doesNotThrow(function() {
                    var loaderStr = StringReplacePlugin.replace({
                        replacements: [{
                            pattern: /<!-- @secret (\w*?) -->/ig,
                            replacement: function (match, p1, offset, string) {
                                return secrets.web[p1];
                            }
                        }]
                    });

                    assert.ok(loaderStr.indexOf("!") === -1, 'No chained loaders expected');
                });
        });

        it('should add next loaders', function(){
            var loaderStr = StringReplacePlugin.replace('html-loader', {
                    replacements: [{
                        pattern: /<!-- @secret (\w*?) -->/ig,
                        replacement: function (match, p1, offset, string) {
                            return secrets.web[p1];
                        }
                    }]
                });
            assert.ok(loaderStr !== null);
            assert.ok(loaderStr.indexOf('html-loader!') === 0);
        });
    });

    describe('#apply()', function(){
        var plugin = new StringReplacePlugin(),
            loader = require("./loader.js"),
            replInst = {
                replacements: [{
                    pattern: /<!-- @secret (\w*?) -->/ig,
                    replacement: function (match, p1, offset, string) {
                        return 'replaced ==>' + p1 + '<==';
                    }
                }]
            },
            id = null,
            query = null,
            replaced = null;

        var callback = function() {
            return function(_, source) {
                replaced = source;
            };
        };
        var mockConfig = {
            options: {},
            emitWarning: console.log,
            async: callback
        };

        beforeEach(function(){
            // runs before each test in this block
            var loaderStr = StringReplacePlugin.replace('html-loader', replInst);
            var matches = loaderStr.match(/\?id=(\w*)(?=($|!))/);
            assert.ok(matches.length === 3);
            query = matches[0];
            id = matches[1];
        });

        it('should set replace options', function(){
            plugin.apply(mockConfig);

            var replOpts = mockConfig.options[StringReplacePlugin.REPLACE_OPTIONS];
            assert.ok(replOpts !== null, 'replace options should be present');
            assert.ok(replOpts[id] === replInst, 'replace options should contain id from loader');
        });

        it('should replace strings in source', function(){
            plugin.apply(mockConfig);
            mockConfig.query = query;
            loader.call(mockConfig, "some string");
            assert(replaced === "some string", "doesn't modify when there are no matches");

            loader.call(mockConfig, "some <!-- @secret stuff --> string");
            assert.equal(replaced, "some replaced ==>stuff<== string", "replaces matches");
        });

        it('should replace strings in source via options', function(){
            mockConfig.options.replacement = {
                before: 'replaced ==>',
                after: '<=='
            };
            plugin.apply(mockConfig);

            var replOpts = mockConfig.options[StringReplacePlugin.REPLACE_OPTIONS];

            replOpts[id] = {
                replacements: [{
                    pattern: /<!-- @secret (\w*?) -->/ig,
                    replacement: function (match, p1) {
                        return this.options.replacement.before + p1 + this.options.replacement.after;
                    }
                }]
            };

            mockConfig.query = query;

            loader.call(mockConfig, "some string");
            assert(replaced === "some string", "doesn't modify when there are no matches");

            loader.call(mockConfig, "some <!-- @secret stuff --> string");
            assert.equal(replaced, "some replaced ==>stuff<== string", "replaces matches");
        });
    })
});