Skip to content

Commit 3812c10

Browse files
committed
add tests for preventParsing and applyNoParseRule
1 parent eb2d6d6 commit 3812c10

2 files changed

Lines changed: 127 additions & 3 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ class NormalModule extends Module {
203203
this._source = new RawSource("throw new Error(" + JSON.stringify(this.error.message) + ");");
204204
}
205205

206-
applyNoParseRule(rule, request) {
206+
applyNoParseRule(rule, content) {
207207
// must start with "rule" if rule is a string
208208
if(typeof rule === "string") {
209-
return request.indexOf(rule) === 0;
209+
return content.indexOf(rule) === 0;
210210
}
211211
// we assume rule is a regexp
212-
return rule.test(request);
212+
return rule.test(content);
213213
}
214214

215215
// check if module should not be parsed

‎test/NormalModule.test.js‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,128 @@ describe("NormalModule", function() {
297297
});
298298
});
299299
});
300+
301+
describe("#applyNoParseRule", function() {
302+
let rule;
303+
let content;
304+
describe("given a string as rule", function() {
305+
beforeEach(function() {
306+
rule = "some-rule";
307+
});
308+
describe("and the content starting with the string specified in rule", function() {
309+
beforeEach(function() {
310+
content = rule + "some-content";
311+
});
312+
it("returns true", function() {
313+
normalModule.preventParsing(rule, content).should.eql(true);
314+
});
315+
});
316+
describe("and the content does not start with the string specified in rule", function() {
317+
beforeEach(function() {
318+
content = "some-content";
319+
});
320+
it("returns false", function() {
321+
normalModule.preventParsing(rule, content).should.eql(false);
322+
});
323+
});
324+
});
325+
describe("given a regex as rule", function() {
326+
beforeEach(function() {
327+
rule = /some-rule/;
328+
});
329+
describe("and the content matches the rule", function() {
330+
beforeEach(function() {
331+
content = rule + "some-content";
332+
});
333+
it("returns true", function() {
334+
normalModule.preventParsing(rule, content).should.eql(true);
335+
});
336+
});
337+
describe("and the content does not match the rule", function() {
338+
beforeEach(function() {
339+
content = "some-content";
340+
});
341+
it("returns false", function() {
342+
normalModule.preventParsing(rule, content).should.eql(false);
343+
});
344+
});
345+
});
346+
});
347+
348+
describe("#preventParsing", function() {
349+
let applyNoParseRuleSpy;
350+
beforeEach(function() {
351+
applyNoParseRuleSpy = sinon.stub();
352+
normalModule.applyNoParseRule = applyNoParseRuleSpy;
353+
});
354+
describe("given no noParseRule", function() {
355+
it("returns false", function() {
356+
normalModule.preventParsing().should.eql(false);
357+
applyNoParseRuleSpy.callCount.should.eql(0);
358+
});
359+
});
360+
describe("given a noParseRule", function() {
361+
let returnValOfSpy;
362+
beforeEach(function() {
363+
returnValOfSpy = Math.random() >= 0.5 ? true : false;
364+
applyNoParseRuleSpy.returns(returnValOfSpy);
365+
});
366+
describe("that is a string", function() {
367+
it("calls and returns whatever applyNoParseRule returns", function() {
368+
normalModule.preventParsing("some rule").should.eql(returnValOfSpy);
369+
applyNoParseRuleSpy.callCount.should.eql(1);
370+
});
371+
});
372+
describe("that is a regex", function() {
373+
it("calls and returns whatever applyNoParseRule returns", function() {
374+
normalModule.preventParsing("some rule").should.eql(returnValOfSpy);
375+
applyNoParseRuleSpy.callCount.should.eql(1);
376+
});
377+
});
378+
describe("that is an array", function() {
379+
describe("of strings and or regexs", function() {
380+
let someRules;
381+
beforeEach(function() {
382+
someRules = [
383+
Math.random() >= 0.5 ? "some rule" : /some rule/,
384+
Math.random() >= 0.5 ? "some rule1" : /some rule1/,
385+
Math.random() >= 0.5 ? "some rule2" : /some rule2/,
386+
];
387+
});
388+
describe("and none of them match", function() {
389+
beforeEach(function() {
390+
returnValOfSpy = false;
391+
applyNoParseRuleSpy.returns(returnValOfSpy);
392+
});
393+
it("returns false", function() {
394+
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
395+
applyNoParseRuleSpy.callCount.should.eql(3);
396+
});
397+
});
398+
describe("and the first of them matches", function() {
399+
beforeEach(function() {
400+
returnValOfSpy = true;
401+
applyNoParseRuleSpy.returns(returnValOfSpy);
402+
});
403+
it("returns true", function() {
404+
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
405+
applyNoParseRuleSpy.callCount.should.eql(1);
406+
});
407+
});
408+
describe("and the last of them matches", function() {
409+
beforeEach(function() {
410+
returnValOfSpy = true;
411+
applyNoParseRuleSpy.onCall(0).returns(false);
412+
applyNoParseRuleSpy.onCall(1).returns(false);
413+
applyNoParseRuleSpy.onCall(2).returns(true);
414+
});
415+
it("returns true", function() {
416+
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
417+
applyNoParseRuleSpy.callCount.should.eql(3);
418+
});
419+
});
420+
});
421+
});
422+
});
423+
});
300424
});

0 commit comments

Comments
 (0)