Best JavaScript code snippet using rewire
isEach.test.js
Source:isEach.test.js
1"use strict"; // run code in ES5 strict mode2var expect = require("expect.js"),3 is = require("../lib/helpers/is"),4 isEach = require("../lib/helpers/isEach"),5 checkError = require("./testHelpers/checkError.js"),6 checkForTypeError = checkError(TypeError),7 checkForError = checkError(Error);8describe("isEach", function () {9 var exampleSubjectsArr,10 exampleSubjectsObj,11 abortEach, // if this is true, the loop shall be break12 // these vars get updated everytime the mock is called. this only works because all tests are synchronous13 funcsCalled,14 subjectsPassed,15 argsPassed;16 function isMock(subject) {17 subjectsPassed.push(subject);18 return {19 existent: function (arg1) {20 funcsCalled.push("existent");21 argsPassed.push(arg1);22 return abortEach === false && is(subject).existent();23 },24 notExistent: function (arg1) {25 funcsCalled.push("notExistent");26 argsPassed.push(arg1);27 return abortEach === false && is(subject).notExistent();28 },29 instanceOf: function (Class) {30 funcsCalled.push("instanceOf");31 argsPassed.push(Class);32 return abortEach === false && is(subject).instanceOf(Class);33 },34 notInstanceOf: function (Class) {35 funcsCalled.push("notInstanceOf");36 argsPassed.push(Class);37 return abortEach === false && is(subject).notInstanceOf(Class);38 },39 implementing: function (Interface) {40 funcsCalled.push("implementing");41 argsPassed.push(Interface);42 return abortEach === false && is(subject).implementing(Interface);43 },44 notImplementing: function (Interface) {45 funcsCalled.push("notImplementing");46 argsPassed.push(Interface);47 return abortEach === false && is(subject).notImplementing(Interface);48 },49 nativeConstructor: function (arg1) {50 funcsCalled.push("nativeConstructor");51 argsPassed.push(arg1);52 return abortEach === false && is(subject).nativeConstructor();53 },54 notNativeConstructor: function (arg1) {55 funcsCalled.push("notNativeConstructor");56 argsPassed.push(arg1);57 return abortEach === false && is(subject).notNativeConstructor();58 }59 };60 }61 before(function () {62 isEach.__inject({is: isMock}); // injecting mock63 });64 beforeEach(function () {65 subjectsPassed = [];66 funcsCalled = [];67 argsPassed = [];68 });69 describe("#existent", function () {70 function A() {}71 function B() {}72 function C() {}73 before(function () {74 exampleSubjectsArr = [A, B, C];75 exampleSubjectsObj = {A: A, B: B, C: C};76 });77 it("should call on every item in exampleSubjectArr method #existent()", function () {78 abortEach = false;79 isEach(exampleSubjectsArr).existent();80 expect(subjectsPassed).to.eql(exampleSubjectsArr);81 expect(funcsCalled).to.eql(["existent", "existent", "existent"]);82 expect(argsPassed).to.eql([undefined, undefined, undefined]);83 });84 it("should break the loop as soon as possible", function () {85 abortEach = true;86 isEach(exampleSubjectsArr).existent();87 expect(subjectsPassed).to.eql([A]);88 });89 it("should call on every item in exampleSubjectObj method #existent()", function () {90 abortEach = false;91 isEach(exampleSubjectsObj).existent();92 expect(subjectsPassed).to.eql(exampleSubjectsArr);93 expect(funcsCalled).to.eql(["existent", "existent", "existent"]);94 expect(argsPassed).to.eql([undefined, undefined, undefined]);95 });96 it("should break the loop as soon as possible", function () {97 abortEach = true;98 isEach(exampleSubjectsObj).existent();99 expect(subjectsPassed).to.eql([A]);100 });101 it("should throw a TypeError on non-collections", function () {102 abortEach = false;103 expect(function () {104 isEach(undefined).existent();105 }).to.throwException(checkForTypeError);106 expect(function () {107 isEach(null).existent();108 }).to.throwException(checkForTypeError);109 expect(function () {110 isEach(true).existent();111 }).to.throwException(checkForTypeError);112 expect(function () {113 isEach(2).existent();114 }).to.throwException(checkForTypeError);115 expect(function () {116 isEach("hello").existent();117 }).to.throwException(checkForTypeError);118 expect(function () {119 isEach(function () {}).existent();120 }).to.throwException(checkForTypeError);121 });122 });123 describe("#notExistent", function () {124 before(function () {125 exampleSubjectsArr = [null, undefined, null];126 exampleSubjectsObj = {A: null, B: undefined, C: null};127 });128 it("should call on every item in exampleSubjectArr method #notExistent()", function () {129 abortEach = false;130 isEach(exampleSubjectsArr).notExistent();131 expect(subjectsPassed).to.eql(exampleSubjectsArr);132 expect(funcsCalled).to.eql(["notExistent", "notExistent", "notExistent"]);133 expect(argsPassed).to.eql([undefined, undefined, undefined]);134 });135 it("should break the loop as soon as possible", function () {136 abortEach = true;137 isEach(exampleSubjectsArr).notExistent();138 expect(subjectsPassed).to.eql([null]);139 });140 it("should call on every item in exampleSubjectObj method #notExistent()", function () {141 abortEach = false;142 isEach(exampleSubjectsObj).notExistent();143 expect(subjectsPassed).to.eql(exampleSubjectsArr);144 expect(funcsCalled).to.eql(["notExistent", "notExistent", "notExistent"]);145 expect(argsPassed).to.eql([undefined, undefined, undefined]);146 });147 it("should break the loop as soon as possible", function () {148 abortEach = true;149 isEach(exampleSubjectsObj).notExistent();150 expect(subjectsPassed).to.eql([null]);151 });152 it("should return false on non-collections", function () {153 abortEach = false;154 expect(function () {155 isEach(undefined).notExistent();156 }).to.throwException(checkForTypeError);157 expect(function () {158 isEach(null).notExistent();159 }).to.throwException(checkForTypeError);160 expect(function () {161 isEach(true).notExistent();162 }).to.throwException(checkForTypeError);163 expect(function () {164 isEach(2).notExistent();165 }).to.throwException(checkForTypeError);166 expect(function () {167 isEach("hello").notExistent();168 }).to.throwException(checkForTypeError);169 expect(function () {170 isEach(function () {}).notExistent();171 }).to.throwException(checkForTypeError);172 });173 });174 describe("#instanceOf", function () {175 function A() {}176 function B() {}177 function C() {}178 function SuperClass() {}179 before(function () {180 A.prototype = SuperClass.prototype;181 B.prototype = SuperClass.prototype;182 C.prototype = SuperClass.prototype;183 exampleSubjectsArr = [new A(), new B(), new C()];184 exampleSubjectsObj = {A: new A(), B: new B(), C: new C()};185 });186 it("should call on every item in exampleSubjectArr method #instanceOf()", function () {187 abortEach = false;188 isEach(exampleSubjectsArr).instanceOf(SuperClass);189 expect(subjectsPassed).to.eql(exampleSubjectsArr);190 expect(funcsCalled).to.eql(["instanceOf", "instanceOf", "instanceOf"]);191 expect(argsPassed).to.eql([SuperClass, SuperClass, SuperClass]);192 });193 it("should break the loop as soon as possible", function () {194 abortEach = true;195 isEach(exampleSubjectsArr).instanceOf(SuperClass);196 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);197 });198 it("should call on every item in exampleSubjectObj method #instanceOf()", function () {199 abortEach = false;200 isEach(exampleSubjectsObj).instanceOf(SuperClass);201 expect(subjectsPassed).to.eql(exampleSubjectsArr);202 expect(funcsCalled).to.eql(["instanceOf", "instanceOf", "instanceOf"]);203 expect(argsPassed).to.eql([SuperClass, SuperClass, SuperClass]);204 });205 it("should break the loop as soon as possible", function () {206 abortEach = true;207 isEach(exampleSubjectsObj).instanceOf(SuperClass);208 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);209 });210 it("should return false on non-collections", function () {211 abortEach = false;212 expect(function () {213 isEach(undefined).instanceOf(SuperClass);214 }).to.throwException(checkForTypeError);215 expect(function () {216 isEach(null).instanceOf(SuperClass);217 }).to.throwException(checkForTypeError);218 expect(function () {219 isEach(true).instanceOf(SuperClass);220 }).to.throwException(checkForTypeError);221 expect(function () {222 isEach(2).instanceOf(SuperClass);223 }).to.throwException(checkForTypeError);224 expect(function () {225 isEach("hello").instanceOf(SuperClass);226 }).to.throwException(checkForTypeError);227 expect(function () {228 isEach(function () {}).instanceOf(SuperClass);229 }).to.throwException(checkForTypeError);230 });231 });232 describe("#notInstanceOf", function () {233 function A() {}234 function B() {}235 function C() {}236 function SuperClass() {}237 before(function () {238 exampleSubjectsArr = [new A(), new B(), new C()];239 exampleSubjectsObj = {A: new A(), B: new B(), C: new C()};240 });241 it("should call on every item in exampleSubjectArr method #notInstanceOf()", function () {242 abortEach = false;243 isEach(exampleSubjectsArr).notInstanceOf(SuperClass);244 expect(subjectsPassed).to.eql(exampleSubjectsArr);245 expect(funcsCalled).to.eql(["notInstanceOf", "notInstanceOf", "notInstanceOf"]);246 expect(argsPassed).to.eql([SuperClass, SuperClass, SuperClass]);247 });248 it("should break the loop as soon as possible", function () {249 abortEach = true;250 isEach(exampleSubjectsArr).notInstanceOf(SuperClass);251 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);252 });253 it("should call on every item in exampleSubjectObj method #notInstanceOf()", function () {254 abortEach = false;255 isEach(exampleSubjectsObj).notInstanceOf(SuperClass);256 expect(subjectsPassed).to.eql(exampleSubjectsArr);257 expect(funcsCalled).to.eql(["notInstanceOf", "notInstanceOf", "notInstanceOf"]);258 expect(argsPassed).to.eql([SuperClass, SuperClass, SuperClass]);259 });260 it("should break the loop as soon as possible", function () {261 abortEach = true;262 isEach(exampleSubjectsObj).notInstanceOf(SuperClass);263 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);264 });265 it("should return false on non-collections", function () {266 abortEach = false;267 expect(function () {268 isEach(undefined).notInstanceOf(SuperClass);269 }).to.throwException(checkForTypeError);270 expect(function () {271 isEach(null).notInstanceOf(SuperClass);272 }).to.throwException(checkForTypeError);273 expect(function () {274 isEach(true).notInstanceOf(SuperClass);275 }).to.throwException(checkForTypeError);276 expect(function () {277 isEach(2).notInstanceOf(SuperClass);278 }).to.throwException(checkForTypeError);279 expect(function () {280 isEach("hello").notInstanceOf(SuperClass);281 }).to.throwException(checkForTypeError);282 expect(function () {283 isEach(function () {}).notInstanceOf(SuperClass);284 }).to.throwException(checkForTypeError);285 });286 });287 describe("#implementing", function () {288 var Interface = {};289 function A() {}290 function B() {}291 function C() {}292 before(function () {293 A.Implements = [Interface];294 B.Implements = [Interface];295 C.Implements = [Interface];296 exampleSubjectsArr = [new A(), new B(), new C()];297 exampleSubjectsObj = {A: new A(), B: new B(), C: new C()};298 });299 it("should call on every item in exampleSubjectArr method #implementing()", function () {300 abortEach = false;301 isEach(exampleSubjectsArr).implementing(Interface);302 expect(subjectsPassed).to.eql(exampleSubjectsArr);303 expect(funcsCalled).to.eql(["implementing", "implementing", "implementing"]);304 expect(argsPassed).to.eql([Interface, Interface, Interface]);305 });306 it("should break the loop as soon as possible", function () {307 abortEach = true;308 isEach(exampleSubjectsArr).implementing(Interface);309 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);310 });311 it("should call on every item in exampleSubjectObj method #implementing()", function () {312 abortEach = false;313 isEach(exampleSubjectsObj).implementing(Interface);314 expect(subjectsPassed).to.eql(exampleSubjectsArr);315 expect(funcsCalled).to.eql(["implementing", "implementing", "implementing"]);316 expect(argsPassed).to.eql([Interface, Interface, Interface]);317 });318 it("should break the loop as soon as possible", function () {319 abortEach = true;320 isEach(exampleSubjectsObj).implementing(Interface);321 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);322 });323 it("should return false on non-collections", function () {324 abortEach = false;325 expect(function () {326 isEach(undefined).implementing(Interface);327 }).to.throwException(checkForTypeError);328 expect(function () {329 isEach(null).implementing(Interface);330 }).to.throwException(checkForTypeError);331 expect(function () {332 isEach(true).implementing(Interface);333 }).to.throwException(checkForTypeError);334 expect(function () {335 isEach(2).implementing(Interface);336 }).to.throwException(checkForTypeError);337 expect(function () {338 isEach("hello").implementing(Interface);339 }).to.throwException(checkForTypeError);340 expect(function () {341 isEach(function () {}).implementing(Interface);342 }).to.throwException(checkForTypeError);343 });344 });345 describe("#notImplementing", function () {346 var Interface = {},347 OtherInterface = {};348 function A() {}349 function B() {}350 function C() {}351 before(function () {352 A.Implements = [OtherInterface];353 B.Implements = [OtherInterface];354 C.Implements = [OtherInterface];355 exampleSubjectsArr = [new A(), new B(), new C()];356 exampleSubjectsObj = {A: new A(), B: new B(), C: new C()};357 });358 it("should call on every item in exampleSubjectArr method #notImplementing()", function () {359 abortEach = false;360 isEach(exampleSubjectsArr).notImplementing(Interface);361 expect(subjectsPassed).to.eql(exampleSubjectsArr);362 expect(funcsCalled).to.eql(["notImplementing", "notImplementing", "notImplementing"]);363 expect(argsPassed).to.eql([Interface, Interface, Interface]);364 });365 it("should break the loop as soon as possible", function () {366 abortEach = true;367 isEach(exampleSubjectsArr).notImplementing(Interface);368 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);369 });370 it("should call on every item in exampleSubjectObj method #notImplementing()", function () {371 abortEach = false;372 isEach(exampleSubjectsObj).notImplementing(Interface);373 expect(subjectsPassed).to.eql(exampleSubjectsArr);374 expect(funcsCalled).to.eql(["notImplementing", "notImplementing", "notImplementing"]);375 expect(argsPassed).to.eql([Interface, Interface, Interface]);376 });377 it("should break the loop as soon as possible", function () {378 abortEach = true;379 isEach(exampleSubjectsObj).notImplementing(Interface);380 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);381 });382 it("should return false on non-collections", function () {383 abortEach = false;384 expect(function () {385 isEach(undefined).notImplementing(Interface);386 }).to.throwException(checkForTypeError);387 expect(function () {388 isEach(null).notImplementing(Interface);389 }).to.throwException(checkForTypeError);390 expect(function () {391 isEach(true).notImplementing(Interface);392 }).to.throwException(checkForTypeError);393 expect(function () {394 isEach(2).notImplementing(Interface);395 }).to.throwException(checkForTypeError);396 expect(function () {397 isEach("hello").notImplementing(Interface);398 }).to.throwException(checkForTypeError);399 expect(function () {400 isEach(function () {}).notImplementing(Interface);401 }).to.throwException(checkForTypeError);402 });403 });404 describe("#nativeConstructor", function () {405 before(function () {406 exampleSubjectsArr = [Boolean, Number, String, Function, Array, Object, Date, RegExp];407 exampleSubjectsObj = {B: Boolean, N: Number, S: String, F: Function, A: Array, O: Object, D: Date, R: RegExp};408 });409 it("should call on every item in exampleSubjectArr method #nativeConstructor()", function () {410 abortEach = false;411 isEach(exampleSubjectsArr).nativeConstructor();412 expect(subjectsPassed).to.eql(exampleSubjectsArr);413 expect(funcsCalled).to.eql([414 "nativeConstructor", "nativeConstructor", "nativeConstructor",415 "nativeConstructor", "nativeConstructor", "nativeConstructor",416 "nativeConstructor", "nativeConstructor"]417 );418 expect(argsPassed).to.eql([419 undefined, undefined, undefined,420 undefined, undefined, undefined,421 undefined, undefined]422 );423 });424 it("should break the loop as soon as possible", function () {425 abortEach = true;426 isEach(exampleSubjectsArr).nativeConstructor();427 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);428 });429 it("should call on every item in exampleSubjectObj method #nativeConstructor()", function () {430 abortEach = false;431 isEach(exampleSubjectsObj).nativeConstructor();432 expect(subjectsPassed).to.eql(exampleSubjectsArr);433 expect(funcsCalled).to.eql([434 "nativeConstructor", "nativeConstructor", "nativeConstructor",435 "nativeConstructor", "nativeConstructor", "nativeConstructor",436 "nativeConstructor", "nativeConstructor"]437 );438 expect(argsPassed).to.eql([439 undefined, undefined, undefined,440 undefined, undefined, undefined,441 undefined, undefined]442 );443 });444 it("should break the loop as soon as possible", function () {445 abortEach = true;446 isEach(exampleSubjectsObj).nativeConstructor();447 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);448 });449 it("should return false on non-collections", function () {450 abortEach = false;451 expect(function () {452 isEach(undefined).nativeConstructor();453 }).to.throwException(checkForTypeError);454 expect(function () {455 isEach(null).nativeConstructor();456 }).to.throwException(checkForTypeError);457 expect(function () {458 isEach(true).nativeConstructor();459 }).to.throwException(checkForTypeError);460 expect(function () {461 isEach(2).nativeConstructor();462 }).to.throwException(checkForTypeError);463 expect(function () {464 isEach("hello").nativeConstructor();465 }).to.throwException(checkForTypeError);466 expect(function () {467 isEach(function () {}).nativeConstructor();468 }).to.throwException(checkForTypeError);469 });470 });471 describe("#notNativeConstructor", function () {472 function A() {}473 function B() {}474 function C() {}475 before(function () {476 exampleSubjectsArr = [A, B, C];477 exampleSubjectsObj = {A: A, B: B, C: C};478 });479 it("should call on every item in exampleSubjectArr method #notNativeConstructor()", function () {480 abortEach = false;481 isEach(exampleSubjectsArr).notNativeConstructor();482 expect(subjectsPassed).to.eql(exampleSubjectsArr);483 expect(funcsCalled).to.eql(["notNativeConstructor", "notNativeConstructor", "notNativeConstructor"]);484 expect(argsPassed).to.eql([undefined, undefined, undefined]);485 });486 it("should break the loop as soon as possible", function () {487 abortEach = true;488 isEach(exampleSubjectsArr).notNativeConstructor();489 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);490 });491 it("should call on every item in exampleSubjectObj method #notNativeConstructor()", function () {492 abortEach = false;493 isEach(exampleSubjectsObj).notNativeConstructor();494 expect(subjectsPassed).to.eql(exampleSubjectsArr);495 expect(funcsCalled).to.eql(["notNativeConstructor", "notNativeConstructor", "notNativeConstructor"]);496 expect(argsPassed).to.eql([undefined, undefined, undefined]);497 });498 it("should break the loop as soon as possible", function () {499 abortEach = true;500 isEach(exampleSubjectsObj).notNativeConstructor();501 expect(subjectsPassed).to.eql([exampleSubjectsArr[0]]);502 });503 it("should return false on non-collections", function () {504 abortEach = false;505 expect(function () {506 isEach(undefined).notNativeConstructor();507 }).to.throwException(checkForTypeError);508 expect(function () {509 isEach(null).notNativeConstructor();510 }).to.throwException(checkForTypeError);511 expect(function () {512 isEach(true).notNativeConstructor();513 }).to.throwException(checkForTypeError);514 expect(function () {515 isEach(2).notNativeConstructor();516 }).to.throwException(checkForTypeError);517 expect(function () {518 isEach("hello").notNativeConstructor();519 }).to.throwException(checkForTypeError);520 expect(function () {521 isEach(function () {}).notNativeConstructor();522 }).to.throwException(checkForTypeError);523 });524 });...
pathHelpers.test.js
Source:pathHelpers.test.js
1"use strict"; // run code in ES5 strict mode2var expect = require("expect.js"),3 pathHelpers = require("../../../lib/shared/helpers/pathHelpers.js"),4 checkError = require("../../testHelpers/checkError.js"),5 filters = pathHelpers.filters,6 checkForError = checkError(Error),7 checkForTypeError = checkError(TypeError);8describe("pathHelpers", function () {9 describe("#filters", function () {10 describe("##noClassFiles", function(){11 it("should filter all files expect class files", function () {12 expect(filters.noClassFiles("fileName.class.js")).to.be(false);13 expect(filters.noClassFiles("fileName.js")).to.be(true);14 expect(filters.noClassFiles("fileName.server.class.js")).to.be(false);15 });16 });17 describe("##onlyClassFiles", function(){18 it("should filter only class files", function () {19 expect(filters.onlyClassFiles("fileName.class.js")).to.be(true);20 expect(filters.onlyClassFiles("fileName.js")).to.be(false);21 expect(filters.onlyClassFiles("fileName.server.class.js")).to.be(true);22 });23 });24 describe("##noAlamidFiles", function(){25 it("should only return files, that don't contain to alamid", function () {26 expect(filters.noAlamidFiles("node_modules/alamid/fileName.js")).to.be(false);27 expect(filters.noAlamidFiles("fileName.js")).to.be(true);28 expect(filters.noAlamidFiles("/alamid/fileName.server.class.js")).to.be(true);29 });30 });31 describe("##noServerFiles", function(){32 it("should return everything except server-files", function () {33 expect(filters.noServerFiles("node_modules/alamid/fileName.server.js")).to.be(false);34 expect(filters.noServerFiles("fileName.js")).to.be(true);35 expect(filters.noServerFiles("/alamid/fileName.client.class.js")).to.be(true);36 });37 });38 describe("##onlyServerFiles", function(){39 it("should return only server-files", function () {40 expect(filters.onlyServerFiles("node_modules/alamid/fileName.server.js")).to.be(true);41 expect(filters.onlyServerFiles("fileName.js")).to.be(false);42 expect(filters.onlyServerFiles("/alamid/fileName.client.class.js")).to.be(false);43 });44 });45 describe("##onlyClientFiles", function(){46 it("should return only client-files", function () {47 expect(filters.onlyClientFiles("node_modules/alamid/fileName.server.js")).to.be(false);48 expect(filters.onlyClientFiles("fileName.js")).to.be(false);49 expect(filters.onlyClientFiles("/alamid/fileName.client.class.js")).to.be(true);50 });51 });52 describe("##noClientFiles", function(){53 it("should return everything except client-files", function () {54 expect(filters.noClientFiles("node_modules/alamid/fileName.server.js")).to.be(true);55 expect(filters.noClientFiles("fileName.js")).to.be(true);56 expect(filters.noClientFiles("/alamid/fileName.client.class.js")).to.be(false);57 });58 });59 });60 describe("#chain", function () {61 it("should chain appropriately", function () {62 var chain = pathHelpers.chain.filter(filterA);63 function filterA() {}64 function modifierA() {}65 expect(chain.filter(filterA)).to.be(chain);66 expect(chain.modifier(modifierA)).to.be(chain);67 chain = pathHelpers.chain.modifier(modifierA);68 expect(chain.filter(filterA)).to.be(chain);69 expect(chain.modifier(modifierA)).to.be(chain);70 });71 describe("##filter", function () {72 it("should apply all filters", function () {73 var myFilter = pathHelpers.chain.filter("noClassFiles", "noAlamidFiles").filter("noServiceURL");74 expect(myFilter("MyClass.class.js")).to.eql(false);75 expect(myFilter("node_modules/alamid/someModule.js")).to.eql(false);76 expect(myFilter("services/BlogService")).to.eql(false);77 });78 it("should apply all arbitrary filters", function () {79 var called = [],80 myFilter;81 function filterA() { called.push("a"); return true; }82 function filterB() { called.push("b"); return true; }83 function filterC() { called.push("c"); return true; }84 myFilter = pathHelpers.chain.filter(filterA, filterB).filter(filterC);85 expect(called).to.eql([]);86 myFilter();87 expect(called).to.eql(["a", "b", "c"]);88 });89 it("should stop after first filter that returned false", function () {90 var called = [],91 myFilter;92 function filterA() { called.push("a"); return true; }93 function filterB() { called.push("b"); return false; }94 function filterC() { called.push("c"); return true; }95 myFilter = pathHelpers.chain.filter(filterA, filterB, filterC);96 myFilter();97 expect(called).to.eql(["a", "b"]);98 });99 it("should call all filters with the path", function () {100 var called = [],101 myFilter;102 function filterA(path) { called.push(path); return true; }103 myFilter = pathHelpers.chain.filter(filterA, filterA, filterA);104 myFilter("a/b/c");105 expect(called).to.eql(["a/b/c", "a/b/c", "a/b/c"]);106 });107 it("should throw an exception if we don't pass a function", function () {108 expect(function () {109 pathHelpers.chain.filter();110 }).to.throwException(checkForError); // in this case we expect only an error because nothing has been passed111 expect(function () {112 pathHelpers.chain.filter(undefined);113 }).to.throwException(checkForTypeError);114 expect(function () {115 pathHelpers.chain.filter(null);116 }).to.throwException(checkForTypeError);117 expect(function () {118 pathHelpers.chain.filter(true);119 }).to.throwException(checkForTypeError);120 expect(function () {121 pathHelpers.chain.filter(2);122 }).to.throwException(checkForTypeError);123 expect(function () {124 pathHelpers.chain.filter([]);125 }).to.throwException(checkForTypeError);126 expect(function () {127 pathHelpers.chain.filter({});128 }).to.throwException(checkForTypeError);129 });130 it("should throw an exception if a filter doesn't return boolean results", function () {131 function returnsNothing() {}132 function returnsUndefined() { return undefined; }133 function returnsNull() { return null; }134 function returnsNumber() { return 2; }135 function returnsString() { return "hello"; }136 function returnsArray() { return []; }137 function returnsObject() { return {}; }138 expect(function () {139 pathHelpers.chain.filter(returnsNothing)();140 }).to.throwException(checkForTypeError);141 expect(function () {142 pathHelpers.chain.filter(returnsUndefined)();143 }).to.throwException(checkForTypeError);144 expect(function () {145 pathHelpers.chain.filter(returnsNull)();146 }).to.throwException(checkForTypeError);147 expect(function () {148 pathHelpers.chain.filter(returnsNumber)();149 }).to.throwException(checkForTypeError);150 expect(function () {151 pathHelpers.chain.filter(returnsString)();152 }).to.throwException(checkForTypeError);153 expect(function () {154 pathHelpers.chain.filter(returnsArray)();155 }).to.throwException(checkForTypeError);156 expect(function () {157 pathHelpers.chain.filter(returnsObject)();158 }).to.throwException(checkForTypeError);159 });160 // TODO finish remaining tests161 });162 });...
Param.class.test.js
Source:Param.class.test.js
1"use strict"; // run code in ES5 strict mode2var expect = require("expect.js"),3 checkError = require("./testHelpers/checkError.js"),4 is = require("../lib/helpers/is"),5 Param = require("../lib/Param.class.js");6describe("Param", function () {7 var instance,8 checkForTypeError = checkError(TypeError);9 beforeEach(function () {10 instance = new Param();11 });12 it("should return true", function () {13 expect(is(instance).instanceOf(Param)).to.be(true);14 });15 describe("#setName", function () {16 it("should return the instance", function () {17 expect(instance.setName("param1")).to.be(instance);18 });19 it("should throw an exception", function () {20 expect(function () {21 instance.setName(undefined);22 }).to.throwException(checkForTypeError);23 expect(function () {24 instance.setName(true);25 }).to.throwException(checkForTypeError);26 expect(function () {27 instance.setName(1);28 }).to.throwException(checkForTypeError);29 expect(function () {30 instance.setName([]);31 }).to.throwException(checkForTypeError);32 expect(function () {33 instance.setName({});34 }).to.throwException(checkForTypeError);35 expect(function () {36 instance.setName(function () {});37 }).to.throwException(checkForTypeError);38 });39 });40 describe("#getName", function () {41 it("should return 'param1'", function () {42 instance.setName("param1");43 expect(instance.getName()).to.be("param1");44 });45 it("should return null", function () {46 expect(instance.getName()).to.be(null);47 instance.setName("param1");48 instance.setName(null);49 expect(instance.getName()).to.be(null);50 });51 });52 describe("#setType", function () {53 it("should return the instance", function () {54 expect(instance.setType("String")).to.be(instance);55 });56 it("should throw an exception", function () {57 expect(function () {58 instance.setType(undefined);59 }).to.throwException(checkForTypeError);60 expect(function () {61 instance.setType(null);62 }).to.throwException(checkForTypeError);63 expect(function () {64 instance.setType(true);65 }).to.throwException(checkForTypeError);66 expect(function () {67 instance.setType(1);68 }).to.throwException(checkForTypeError);69 expect(function () {70 instance.setType([]);71 }).to.throwException(checkForTypeError);72 expect(function () {73 instance.setType({});74 }).to.throwException(checkForTypeError);75 expect(function () {76 instance.setType(function () {});77 }).to.throwException(checkForTypeError);78 });79 });80 describe("#getType", function () {81 it("should return 'String'", function () {82 instance.setType("String");83 expect(instance.getType()).to.be("String");84 });85 it("should return '*'", function () {86 expect(instance.getType()).to.be("*");87 });88 });89 describe("#setOptional", function () {90 it("should return the instance", function () {91 expect(instance.setOptional(true)).to.be(instance);92 });93 it("should throw an exception", function () {94 expect(function () {95 instance.setOptional(undefined);96 }).to.throwException(checkForTypeError);97 expect(function () {98 instance.setOptional("some string");99 }).to.throwException(checkForTypeError);100 expect(function () {101 instance.setOptional(1);102 }).to.throwException(checkForTypeError);103 expect(function () {104 instance.setOptionalType([]);105 }).to.throwException(checkForTypeError);106 expect(function () {107 instance.setOptional({});108 }).to.throwException(checkForTypeError);109 expect(function () {110 instance.setOptional(function () {});111 }).to.throwException(checkForTypeError);112 });113 });114 describe("#getOptional", function () {115 it("should return true", function () {116 instance.setOptional(true);117 expect(instance.getOptional()).to.be(true);118 });119 it("should return false", function () {120 expect(instance.getOptional()).to.be(false);121 instance.setOptional(true);122 instance.setOptional(false);123 expect(instance.getOptional()).to.be(false);124 });125 });...
Using AI Code Generation
1const rewire = require('rewire');2const test = rewire('./test.js');3const checkForTypeError = test.__get__('checkForTypeError');4describe('checkForTypeError', function() {5 it('should return true if type is not string', function() {6 const result = checkForTypeError(1);7 expect(result).to.be.true;8 });9 it('should return false if type is string', function() {10 const result = checkForTypeError('string');11 expect(result).to.be.false;12 });13});
Using AI Code Generation
1var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");2var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");3var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");4var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");5var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");6var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");7var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");8var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");9var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");10var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");11var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");12var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");13var checkForTypeError = require('rewire')('./app.js').__get__("checkForTypeError");
Using AI Code Generation
1const rewire = require('rewire');2const test = rewire('./test.js');3const checkForTypeError = test.__get__('checkForTypeError');4test('checkForTypeError', () => {5 expect(checkForTypeError(1, 'number')).toBe(null);6 expect(checkForTypeError('1', 'number')).toBe('Expected argument to be of type number but received string');7});8function checkForTypeError(arg, type) {9 if (typeof arg !== type) {10 return `Expected argument to be of type ${type} but received ${typeof arg}`;11 }12 return null;13}14module.exports = checkForTypeError;
Using AI Code Generation
1var rewire = require('rewire');2var assert = require('assert');3var test = rewire('./test.js');4var checkForTypeError = test.__get__('checkForTypeError');5assert(checkForTypeError(1) === undefined);6assert(checkForTypeError('1') === undefined);7assert(checkForTypeError(true) === undefined);8assert(checkForTypeError(false) === undefined);9assert(checkForTypeError(null) === undefined);10assert(checkForTypeError(undefined) === undefined);11assert(checkForTypeError([]) === undefined);12assert(checkForTypeError({}) === undefined);13assert(checkForTypeError(function(){}) === undefined);14assert(checkForTypeError(new Date()) instanceof TypeError);15assert(checkForTypeError(new Error()) instanceof TypeError);16assert(checkForTypeError(new RegExp()) instanceof TypeError);17assert(checkForTypeError(new String()) instanceof TypeError);18assert(checkForTypeError(new Number()) instanceof TypeError);19assert(checkForTypeError(new Boolean()) instanceof TypeError);20assert(checkForTypeError(new Array()) instanceof TypeError);21assert(checkForTypeError(new Object()) instanceof TypeError);22assert(checkForTypeError(new Function()) instanceof TypeError);23assert(checkForTypeError(new Date()) instanceof TypeError);24assert(checkForTypeError(new Error()) instanceof TypeError);25assert(checkForTypeError(new RegExp()) instanceof TypeError);26assert(checkForTypeError(new String()) instanceof TypeError);27assert(checkForTypeError(new Number()) instanceof TypeError);28assert(checkForTypeError(new Boolean()) instanceof TypeError);29assert(checkForTypeError(new Array()) instanceof TypeError);30assert(checkForTypeError(new Object()) instanceof TypeError);31assert(checkForTypeError(new Function()) instanceof TypeError);32assert(checkForTypeError(new Date()) instanceof TypeError);33assert(checkForTypeError(new Error()) instanceof TypeError);34assert(checkForTypeError(new RegExp()) instanceof TypeError);35assert(checkForTypeError(new String()) instanceof TypeError);36assert(checkForTypeError(new Number()) instanceof TypeError);37assert(checkForTypeError(new Boolean()) instanceof TypeError);38assert(checkForTypeError(new Array()) instanceof TypeError);39assert(checkForTypeError(new Object()) instanceof TypeError);40assert(checkForTypeError(new Function()) instanceof TypeError);41assert(checkForTypeError(new Date()) instanceof TypeError);42assert(checkForTypeError(new Error()) instanceof TypeError);43assert(checkForTypeError(new RegExp()) instanceof TypeError);44assert(checkForTypeError(new String()) instanceof TypeError);45assert(checkForTypeError(new Number()) instanceof TypeError);46assert(checkForTypeError(new Boolean()) instanceof TypeError);47assert(checkForTypeError(new Array()) instanceof TypeError);48assert(checkForTypeError(new Object()) instanceof TypeError);49assert(checkForTypeError(new Function()) instanceof TypeError);50assert(checkForTypeError(new Date()) instanceof TypeError);
Using AI Code Generation
1const rewire = require('rewire');2const test = rewire('./test.js');3const checkForTypeError = test.__get__('checkForTypeError');4test('checkForTypeError method', () => {5 expect(checkForTypeError('string')).toBe('string');6 expect(checkForTypeError(1)).toBe(1);7 expect(checkForTypeError(null)).toBe(null);8 expect(checkForTypeError(undefined)).toBe(undefined);9 expect(checkForTypeError({})).toBe({});10 expect(checkForTypeError([])).toBe([]);11 expect(checkForTypeError(true)).toBe(true);12 expect(checkForTypeError(false)).toBe(false);13});14const rewire = require('rewire');15const test = rewire('./test.js');16const checkForTypeError = test.__get__('checkForTypeError');17test('checkForTypeError method', () => {18 expect(checkForTypeError('string')).toBe('string');19 expect(checkForTypeError(1)).toBe(1);20 expect(checkForTypeError(null)).toBe(null);21 expect(checkForTypeError(undefined)).toBe(undefined);22 expect(checkForTypeError({})).toBe({});23 expect(checkForTypeError([])).toBe([]);24 expect(checkForTypeError(true)).toBe(true);25 expect(checkForTypeError(false)).toBe(false);26});27const rewire = require('rewire');28const test = rewire('./test.js');29const checkForTypeError = test.__get__('checkForTypeError');30test('checkForTypeError method', () => {31 expect(checkForTypeError('string')).toBe('string');32 expect(checkForTypeError(1)).toBe(1);33 expect(checkForTypeError(null)).toBe(null);34 expect(checkForTypeError(undefined)).toBe(undefined);35 expect(checkForTypeError({})).toBe({});36 expect(checkForTypeError([])).toBe([]);37 expect(checkForTypeError(true)).toBe(true);38 expect(checkForTypeError(false)).toBe(false);39});40const rewire = require('rewire');41const test = rewire('./test.js');42const checkForTypeError = test.__get__('checkForTypeError');43test('checkForTypeError method', () => {44 expect(checkForTypeError('string')).toBe('string');45 expect(checkForTypeError(1)).toBe(1);
Using AI Code Generation
1var rewire = require("rewire");2var test = rewire('./test');3var checkForTypeError = test.__get__("checkForTypeError");4var checkForTypeError = function (value) {5 if (typeof value !== 'number')6 throw new TypeError('value should be a number');7};8module.exports = {9};
Using AI Code Generation
1const rewire = require('rewire');2const test = rewire('./test.js');3test.__set__('checkForTypeError', function() {4 console.log('checkForTypeError has been called');5});6test('hello');7const rewire = require('rewire');8const test = rewire('./test.js');9test.__set__('checkForTypeError', function() {10 console.log('checkForTypeError has been called');11});12test('hello');13const rewire = require('rewire');14const test = rewire('./test.js');15test.__set__('checkForTypeError', function() {16 console.log('checkForTypeError has been called');17});18test('hello');19const rewire = require('rewire');20const test = rewire('./test.js');21test.__set__('checkForTypeError', function() {22 console.log('checkForTypeError has been called');23});24test('hello');25const rewire = require('rewire');26const test = rewire('./test.js');27test.__set__('checkForTypeError', function() {28 console.log('checkForTypeError has been called');29});30test('hello');31const rewire = require('rewire');32const test = rewire('./test.js');33test.__set__('checkForTypeError', function() {34 console.log('checkForTypeError has been called');35});36test('hello');37const rewire = require('rewire');38const test = rewire('./test.js');39test.__set__('checkForTypeError', function() {40 console.log('checkForTypeError has been called');41});42test('hello');43const rewire = require('rewire');44const test = rewire('./test.js');45test.__set__('checkForTypeError', function() {46 console.log('checkForTypeError has been called');47});48test('hello');
Using AI Code Generation
1var checkForTypeError = rewire("../src/checkForTypeError.js");2checkForTypeError.__set__("isType", function(type){3 return true;4});5var result = checkForTypeError("string");6assert.equal(result, "You provided a string type");7var checkForTypeError = rewire("../src/checkForTypeError.js");8var result = checkForTypeError("string");9assert.equal(result, "You provided a string type");
Using AI Code Generation
1var checkForTypeError = require('./checkForTypeError.js').checkForTypeError;2var assert = require('assert');3assert.equal(checkForTypeError(1), false, 'checkForTypeError(1) should return false');4assert.equal(checkForTypeError('1'), true, 'checkForTypeError(\'1\') should return true');5assert.equal(checkForTypeError('a'), true, 'checkForTypeError(\'a\') should return true');6assert.equal(checkForTypeError(undefined), true, 'checkForTypeError(undefined) should return true');7assert.equal(checkForTypeError(null), true, 'checkForTypeError(null) should return true');8assert.equal(checkForTypeError(true), false, 'checkForTypeError(true) should return false');9assert.equal(checkForTypeError(false), false, 'checkForTypeError(false) should return false');10assert.equal(checkForTypeError([]), false, 'checkForTypeError([]) should return false');11assert.equal(checkForTypeError({}), false, 'checkForTypeError({}) should return false');12assert.equal(checkForTypeError(function(){}), false, 'checkForTypeError(function(){}) should return false');13assert.equal(checkForTypeError(NaN), true, 'checkForTypeError(NaN) should return true');14assert.equal(checkForTypeError(Infinity), false, 'checkForTypeError(Infinity) should return false');15 ✓ checkForTypeError(1) should return false16 ✓ checkForTypeError('1') should return true17 ✓ checkForTypeError('a') should return true18 ✓ checkForTypeError(undefined) should return true19 ✓ checkForTypeError(null) should return true20 ✓ checkForTypeError(true) should return false21 ✓ checkForTypeError(false) should return false
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!