Skip to content

Commit

Permalink
add more unit test to reach 100% lines coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zenz34 committed Apr 9, 2021
1 parent fd924b3 commit 80fbe68
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/webpack-config-composer/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class WebpackConfigComposer {

return p.partials || {};
});
/* eslint-enable max-statements */


profPartials = _.merge({}, ...profPartials);

Expand Down Expand Up @@ -195,6 +195,7 @@ export class WebpackConfigComposer {

return currentConfig;
}
/* eslint-enable max-statements */

deleteCustomProps(config) {
return deleteCustomProps(config);
Expand Down
76 changes: 66 additions & 10 deletions packages/webpack-config-composer/test/spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import loaderPartial from "../fixtures/partial/loader";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const _ = require("lodash");
import { FooPlugin } from "../fixtures/plugins/foo-plugin";
import { Partial } from "../../lib/partial";

/* eslint-disable max-statements */
describe("composer", () => {
it("should accept partials and generate config", () => {
const composer = new WebpackConfigComposer({
Expand All @@ -22,22 +24,22 @@ describe("composer", () => {
b: {
partials: {
bar: {
order: 200
order: 200,
},
test: {
order: 300
}
}
order: 300,
},
},
},

a: {
partials: {
foo: {
order: "100"
order: "100"
}
}
}
}
}
});
composer.addPartials([fooPartial, barPartial]);
expect(composer.profiles).to.have.keys("a", "b");
Expand Down Expand Up @@ -201,6 +203,31 @@ describe("composer", () => {
expect(config.module.rules[0]).to.equal("loader-rule1");
});

it("should return", () => {
const composer = new WebpackConfigComposer({});
composer.addPartials([
fooPartial,
{
loader: {
config: (options) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = require("../fixtures/partial/loader").default.loader.config(options);
_.merge(options.currentConfig, config);
},
},
},
]);
const config = composer.compose({ meta: true }, { partials: { loader: {} } });

expect(config.config).to.eql({
module: {
rules: [
"loader-rule1"
]
}
});
});

describe("addProfiles", () => {
it("should accept multiple profiles", () => {
const composer = new WebpackConfigComposer({});
Expand Down Expand Up @@ -298,17 +325,17 @@ describe("composer", () => {

it("should merge into existing partial", () => {
const composer = new WebpackConfigComposer({
partials: [fooPartial, barPartial]
partials: [fooPartial, barPartial],
});

composer.addPartials({
foo: {
config: {
plugins: ["fooTest"]
plugins: ["fooTest"],
},
addOptions: {
concatArray: "tail"
}
concatArray: "tail",
},
},
bar: {
config: {
Expand Down Expand Up @@ -355,4 +382,33 @@ describe("composer", () => {
expect(composer.partials.bar.config.plugins[0]).to.equal("barTest");
});
});

describe("enablePartial", () => {
it("should enable partial", () => {
const testName = "test_9527";
const composer = new WebpackConfigComposer({});
expect(composer.getPartial(testName)).to.equal(undefined);
composer.addPartial(testName, { foor: "bar" }, null);
expect(composer.getPartial(testName).enable).to.equal(undefined);
composer.enablePartial(testName, true);
expect(composer.getPartial(testName).enable).to.equal(true);
});
});

describe("replacePartial", () => {
it("should replacePartial", () => {
const testName = "test_9528";
const composer = new WebpackConfigComposer({});
expect(composer.getPartial(testName)).to.equal(undefined);
composer.addPartial(testName, { foo1: "bar1" }, null);
expect(composer.getPartial(testName)).to.deep.equal(
new Partial(testName, { config: { foo1: "bar1" } })
);
composer.replacePartial(testName, { foo2: "bar2" }, null);
expect(composer.getPartial(testName)).to.deep.equal(
new Partial(testName, { config: { foo2: "bar2" } })
);
});
});
});
/* eslint-enable max-statements */
3 changes: 2 additions & 1 deletion packages/webpack-config-composer/test/spec/partial.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Partial, DATA, OVERRIDE } from "../../lib/partial";
import { expect } from "chai";
// import sinon from "sinon";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const _ = require("lodash");

describe("partial", () => {
it("should default partials to {}", () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const fn = () => {};
const partial = new Partial("test", fn);
expect(partial[DATA].config).to.deep.equal(fn);
Expand Down

0 comments on commit 80fbe68

Please sign in to comment.