{"size": 11908, "ext": "js", "max_stars_repo_path": "test/spec/index.js", "max_stars_repo_name": "MiguelCastillo/pak", "max_stars_repo_stars_event_min_datetime": "2016-12-18T17:12:20.000Z", "max_stars_repo_stars_event_max_datetime": "2017-12-18T17:39:51.000Z", "max_issues_repo_path": "test/spec/index.js", "max_issues_repo_name": "MiguelCastillo/pak", "max_issues_repo_issues_event_min_datetime": "2016-12-16T22:22:21.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-21T14:22:33.000Z", "max_forks_repo_path": "test/spec/index.js", "max_forks_repo_name": "MiguelCastillo/pak", "max_forks_repo_forks_event_min_datetime": "2017-12-18T10:36:08.000Z", "max_forks_repo_forks_event_max_datetime": "2017-12-18T10:36:08.000Z"}
{"max_stars_count": 5, "max_issues_count": 18, "max_forks_count": 1, "avg_line_length": 71.3053892216, "max_line_length": 1332, "alphanum_fraction": 0.6350352704}
import { expect } from "chai";
import pakit from "../../src/pakit";
import path from "path";

describe("pakit test suite", function() {
  this.timeout(10000);

  describe("when bundling a module with no dependencies", function() {
    var result;
    before(function () {
      return pakit("test/sample/hello-world/all.js", { log: false }).then(ctx => result = ctx);
    });

    it("then result has a 'main' shard", function() {
      expect(result.shards).to.have.property("main");
    });

    it("then content of the 'main' shard with correct  value", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = `Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(r,e){var t={};function n(e){if(!t.hasOwnProperty(e)){var i={exports:{}},f=r[e][0],u=r[e][1];t[e]=i.exports,f((a=u,function(r){var e=a[r];if(o(e))return n(e);for(var t=n.next;t;t=t.next)if(t.has(e))return t.get(e);for(var i=n.prev;i;i=i.prev)if(i.has(e))return i.get(e);throw new Error("Module \'"+r+"\' with id "+e+" was not found")}),i,i.exports),t[e]=i.exports}var a;return t[e]}function o(e){return r[e]}if(n.get=n,n.has=o,n.next="undefined"==typeof _bb$iter?null:_bb$iter,e.length)for(var i=n,f=n.next;f;)f.prev=i,i=f,f=f.next;return e.forEach(n),n}({1:[function(r,e,t){"use strict";console.log("hello world")},{}]},[1]);`;
      expect(actual).to.be.equal(expected);
    });

    it("then the result has one module", function() {
      expect(result.shards["main"].modules).to.have.lengthOf(1);
    });
  });

  describe("when bundling a module and defining shard rules", function() {
    var result;
    before(function () {
      return pakit("test/sample/hello-world/index.js", {
        log: false,
        shards: {
          hello: { match: "hello.js$" },
          world: { match: /world.js$/ }
        }
      })
      .then(ctx => result = ctx);
    });

    it("then the 'main' bundle is created", function() {
      expect(result.shards).to.have.property("main");
    });

    it("then the 'main' bundle has the expected content", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = `Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(r,e){var t={};function n(e){if(!t.hasOwnProperty(e)){var i={exports:{}},f=r[e][0],u=r[e][1];t[e]=i.exports,f((a=u,function(r){var e=a[r];if(o(e))return n(e);for(var t=n.next;t;t=t.next)if(t.has(e))return t.get(e);for(var i=n.prev;i;i=i.prev)if(i.has(e))return i.get(e);throw new Error("Module \'"+r+"\' with id "+e+" was not found")}),i,i.exports),t[e]=i.exports}var a;return t[e]}function o(e){return r[e]}if(n.get=n,n.has=o,n.next="undefined"==typeof _bb$iter?null:_bb$iter,e.length)for(var i=n,f=n.next;f;)f.prev=i,i=f,f=f.next;return e.forEach(n),n}({1:[function(r,e,t){"use strict";r("./hello"),console.log(" == "),r("./world")},{"./hello":2,"./world":3}]},[1]);`;
      expect(actual).to.be.equal(expected);
    });

    it("then the 'main' bundle has one module", function() {
      expect(result.shards["main"].modules).to.have.lengthOf(1);
    });

    it("then the 'hello' bundle is created", function() {
      expect(result.shards).to.have.property("hello");
    });

    it("then the 'hello' bundle has the expected content", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = `Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(r,e){var t={};function n(e){if(!t.hasOwnProperty(e)){var i={exports:{}},f=r[e][0],u=r[e][1];t[e]=i.exports,f((a=u,function(r){var e=a[r];if(o(e))return n(e);for(var t=n.next;t;t=t.next)if(t.has(e))return t.get(e);for(var i=n.prev;i;i=i.prev)if(i.has(e))return i.get(e);throw new Error("Module '"+r+"' with id "+e+" was not found")}),i,i.exports),t[e]=i.exports}var a;return t[e]}function o(e){return r[e]}if(n.get=n,n.has=o,n.next="undefined"==typeof _bb$iter?null:_bb$iter,e.length)for(var i=n,f=n.next;f;)f.prev=i,i=f,f=f.next;return e.forEach(n),n}({1:[function(r,e,t){"use strict";r("./hello"),console.log(" == "),r("./world")},{"./hello":2,"./world":3}]},[1]);`;
      expect(actual).to.be.equal(expected);
    });

    it("then the 'hello' bundle has one module", function() {
      expect(result.shards["hello"].modules).to.have.lengthOf(4);
    });

    it("then the 'world' bundle is created", function() {
      expect(result.shards).to.have.property("world");
    });

    it("then the 'world' bundle has the expected content", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = escapeChars(`Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(r,e){var t={};function n(e){if(!t.hasOwnProperty(e)){var i={exports:{}},f=r[e][0],u=r[e][1];t[e]=i.exports,f((a=u,function(r){var e=a[r];if(o(e))return n(e);for(var t=n.next;t;t=t.next)if(t.has(e))return t.get(e);for(var i=n.prev;i;i=i.prev)if(i.has(e))return i.get(e);throw new Error("Module '"+r+"' with id "+e+" was not found")}),i,i.exports),t[e]=i.exports}var a;return t[e]}function o(e){return r[e]}if(n.get=n,n.has=o,n.next="undefined"==typeof _bb$iter?null:_bb$iter,e.length)for(var i=n,f=n.next;f;)f.prev=i,i=f,f=f.next;return e.forEach(n),n}({1:[function(r,e,t){"use strict";r("./hello"),console.log(" == "),r("./world")},{"./hello":2,"./world":3}]},[1]);`);
      expect(actual).to.be.equal(expected);
    });

    it("then the 'world' bundle has one module", function() {
      expect(result.shards["world"].modules).to.have.lengthOf(1);
    });
  });

  describe("when bundling a module with two dependencies", function() {
    var result;
    before(function () {
      return pakit("test/sample/hello-world/index.js", { log: false }).then(ctx => result = ctx);
    });

    it("then result has a 'main' shard", function() {
      expect(result.shards).to.have.property("main");
    });

    it("then content of the 'main' shard with correct  value", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = escapeChars(`Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(e,t){var o={};function n(t){if(!o.hasOwnProperty(t)){var l={exports:{}},s=e[t][0],i=e[t][1];o[t]=l.exports,s((u=i,function(e){var t=u[e];if(r(t))return n(t);for(var o=n.next;o;o=o.next)if(o.has(t))return o.get(t);for(var l=n.prev;l;l=l.prev)if(l.has(t))return l.get(t);throw new Error("Module '"+e+"' with id "+t+" was not found")}),l,l.exports),o[t]=l.exports}var u;return o[t]}function r(t){return e[t]}if(n.get=n,n.has=r,n.next="undefined"==typeof _bb$iter?null:_bb$iter,t.length)for(var l=n,s=n.next;s;)s.prev=l,l=s,s=s.next;return t.forEach(n),n}({1:[function(e,t,o){"use strict";e("./hello"),console.log(" == "),e("./world")},{"./hello":2,"./world":3}],2:[function(e,t,o){"use strict";e("./hello.css"),e("./hello.json"),console.log("hello")},{"./hello.css":4,"./hello.json":5}],3:[function(e,t,o){"use strict";console.log("world")},{}],4:[function(e,t,o){e("$bit-loader-css/loadstyle")(".hello {\n  color: blue;\n}\n")},{"$bit-loader-css/loadstyle":6}],5:[function(e,t,o){t.exports={hello:"json"}},{}],6:[function(e,t,o){t.exports=function(e){var t=document.getElementsByTagName("head")[0],o=document.createElement("style");o.setAttribute("type","text/css"),o.innerHTML=e,t.appendChild(o)}},{}]},[1]);`);
      expect(actual).to.be.equal(expected);
    });

    it("then the result has 6 module", function() {
      expect(result.shards["main"].modules).to.have.lengthOf(6);
    });
  });

  describe("when bundling a file content with no dependencies", function() {
    var result, content;

    before(function () {
      content = "console.log('hello world - content');";
      return pakit({ content: content }, { log: false }).then(ctx => result = ctx);
    });

    it("then result has a 'main' shard", function() {
      expect(result.shards).to.have.property("main");
    });

    it("then content of the 'main' shard with correct  value", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = escapeChars(`Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(r,e){var t={};function n(e){if(!t.hasOwnProperty(e)){var f={exports:{}},i=r[e][0],u=r[e][1];t[e]=f.exports,i((a=u,function(r){var e=a[r];if(o(e))return n(e);for(var t=n.next;t;t=t.next)if(t.has(e))return t.get(e);for(var f=n.prev;f;f=f.prev)if(f.has(e))return f.get(e);throw new Error("Module '"+r+"' with id "+e+" was not found")}),f,f.exports),t[e]=f.exports}var a;return t[e]}function o(e){return r[e]}if(n.get=n,n.has=o,n.next="undefined"==typeof _bb$iter?null:_bb$iter,e.length)for(var f=n,i=n.next;i;)i.prev=f,f=i,i=i.next;return e.forEach(n),n}({1:[function(r,e,t){console.log("hello world - content")},{}]},[1]);`);
      expect(actual).to.be.equal(expected);
    });

    it("then the result has one module", function() {
      expect(result.shards["main"].modules).to.have.lengthOf(1);
    });
  });

  describe("when bundling a file content with two dependencies", function() {
    var result, content, filePath = path.join(process.cwd(), "test/sample/hello-world/");

    before(function () {
      content = `require("./hello");console.log(" == ");require("./world");`;
      return pakit({ content: content, path: filePath }, { log: false }).then(ctx => result = ctx);
    });

    it("then result has a 'main' shard", function() {
      expect(result.shards).to.have.property("main");
    });

    it("then content of the 'main' shard with correct  value", function() {
      const actual = removeToIndexOf(trimResult(result.shards["main"].content), "Miguel Castillo <manchagnu@gmail.com>");
      const expected = escapeChars(`Miguel Castillo <manchagnu@gmail.com>. Licensed under MIT */require=_bb$iter=function(e,t){var o={};function n(t){if(!o.hasOwnProperty(t)){var l={exports:{}},s=e[t][0],i=e[t][1];o[t]=l.exports,s((u=i,function(e){var t=u[e];if(r(t))return n(t);for(var o=n.next;o;o=o.next)if(o.has(t))return o.get(t);for(var l=n.prev;l;l=l.prev)if(l.has(t))return l.get(t);throw new Error("Module '"+e+"' with id "+t+" was not found")}),l,l.exports),o[t]=l.exports}var u;return o[t]}function r(t){return e[t]}if(n.get=n,n.has=r,n.next="undefined"==typeof _bb$iter?null:_bb$iter,t.length)for(var l=n,s=n.next;s;)s.prev=l,l=s,s=s.next;return t.forEach(n),n}({1:[function(e,t,o){e("./hello"),console.log(" == "),e("./world")},{"./hello":2,"./world":3}],2:[function(e,t,o){"use strict";e("./hello.css"),e("./hello.json"),console.log("hello")},{"./hello.css":4,"./hello.json":5}],3:[function(e,t,o){"use strict";console.log("world")},{}],4:[function(e,t,o){e("$bit-loader-css/loadstyle")(".hello {\n  color: blue;\n}\n")},{"$bit-loader-css/loadstyle":6}],5:[function(e,t,o){t.exports={hello:"json"}},{}],6:[function(e,t,o){t.exports=function(e){var t=document.getElementsByTagName("head")[0],o=document.createElement("style");o.setAttribute("type","text/css"),o.innerHTML=e,t.appendChild(o)}},{}]},[1]);`);
      expect(actual).to.be.equal(expected);
    });

    it("then the result has 6 module", function() {
      expect(result.shards["main"].modules).to.have.lengthOf(6);
    });
  });
});

function trimResult(data) {
  return data
    .toString()
    .replace(/\n/g, "")
    .replace(/\/\/# sourceMappingURL=.*/, "");
}

function removeToIndexOf(str, pattern) {
  return str.substr(str.indexOf(pattern));
}

function escapeChars(str) {
  return str.replace(/\n/g, "\\n");
}
