2017-01-18 22:27:19 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RawModule = require("../lib/RawModule");
|
|
|
|
const OriginalSource = require("webpack-sources").OriginalSource;
|
|
|
|
const RawSource = require("webpack-sources").RawSource;
|
|
|
|
const RequestShortener = require("../lib/RequestShortener");
|
|
|
|
const should = require("should");
|
|
|
|
const path = require("path");
|
2017-06-19 01:25:03 +08:00
|
|
|
const crypto = require("crypto");
|
2017-01-18 22:27:19 +08:00
|
|
|
|
|
|
|
describe("RawModule", () => {
|
|
|
|
let myRawModule;
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
const source = "sourceStr attribute";
|
|
|
|
const identifier = "identifierStr attribute";
|
|
|
|
const readableIdentifier = "readableIdentifierStr attribute";
|
2017-01-06 22:23:29 +08:00
|
|
|
myRawModule = new RawModule(source, identifier, readableIdentifier);
|
|
|
|
});
|
|
|
|
|
2017-01-18 22:27:19 +08:00
|
|
|
describe("identifier", () => {
|
|
|
|
it("returns value for identifierStr attribute", () =>
|
|
|
|
should(myRawModule.identifier()).be.exactly("identifierStr attribute"));
|
2017-01-06 22:23:29 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 22:27:19 +08:00
|
|
|
describe("size", () => {
|
2018-02-25 09:00:20 +08:00
|
|
|
it('returns value for sourceStr attribute"s length property', () => {
|
2017-01-18 22:27:19 +08:00
|
|
|
const sourceStrLength = myRawModule.sourceStr.length;
|
2017-01-06 22:23:29 +08:00
|
|
|
should(myRawModule.size()).be.exactly(sourceStrLength);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-18 22:27:19 +08:00
|
|
|
describe("readableIdentifier", () => {
|
2018-02-25 09:00:20 +08:00
|
|
|
it(
|
|
|
|
'returns result of calling provided requestShortener"s shorten method ' +
|
|
|
|
"on readableIdentifierStr attribute",
|
2017-02-10 06:38:39 +08:00
|
|
|
() => {
|
|
|
|
const requestShortener = new RequestShortener(path.resolve());
|
|
|
|
should.exist(myRawModule.readableIdentifier(requestShortener));
|
|
|
|
}
|
|
|
|
);
|
2017-01-06 22:23:29 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 22:27:19 +08:00
|
|
|
describe("needRebuild", () => {
|
|
|
|
it("returns false", () => should(myRawModule.needRebuild()).be.false());
|
2017-01-06 22:23:29 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 22:27:19 +08:00
|
|
|
describe("source", () => {
|
2018-02-25 09:00:20 +08:00
|
|
|
it(
|
|
|
|
"returns a new OriginalSource instance with sourceStr attribute and " +
|
|
|
|
"return value of identifier() function provided as constructor arguments",
|
2017-01-18 22:27:19 +08:00
|
|
|
() => {
|
2018-02-25 09:00:20 +08:00
|
|
|
const originalSource = new OriginalSource(
|
|
|
|
myRawModule.sourceStr,
|
|
|
|
myRawModule.identifier()
|
|
|
|
);
|
2017-01-06 22:23:29 +08:00
|
|
|
myRawModule.useSourceMap = true;
|
|
|
|
myRawModule.source().should.match(originalSource);
|
2017-02-10 06:38:39 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-06 22:23:29 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
it(
|
|
|
|
"returns a new RawSource instance with sourceStr attribute provided " +
|
|
|
|
"as constructor argument if useSourceMap is falsey",
|
2017-02-10 06:38:39 +08:00
|
|
|
() => {
|
|
|
|
const rawSource = new RawSource(myRawModule.sourceStr);
|
|
|
|
myRawModule.useSourceMap = false;
|
|
|
|
myRawModule.source().should.match(rawSource);
|
|
|
|
}
|
|
|
|
);
|
2017-01-06 22:23:29 +08:00
|
|
|
});
|
2017-06-19 01:25:03 +08:00
|
|
|
|
|
|
|
describe("updateHash", () => {
|
|
|
|
it("should include sourceStr in its hash", () => {
|
2018-02-25 09:00:20 +08:00
|
|
|
const hashModule = module => {
|
2017-06-19 01:25:03 +08:00
|
|
|
const hash = crypto.createHash("sha256");
|
|
|
|
module.updateHash(hash);
|
|
|
|
return hash.digest("hex");
|
|
|
|
};
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
const hashFoo = hashModule(new RawModule('"foo"'));
|
|
|
|
const hashBar = hashModule(new RawModule('"bar"'));
|
2017-06-19 01:25:03 +08:00
|
|
|
hashFoo.should.not.equal(hashBar);
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 22:23:29 +08:00
|
|
|
});
|