diff --git a/tests/koans.spec.js b/tests/koans.spec.js index 5ea5963..b9917dc 100644 --- a/tests/koans.spec.js +++ b/tests/koans.spec.js @@ -26,18 +26,18 @@ describe('the JavaScript language', () => { it('surprises me, NaN is not comparable with NaN', () => { expect(5 / 'a').toEqual(5 / 'a'); - // expect(typeof NaN).toEqual(); + expect(typeof NaN).toEqual("number"); expect(isNaN(5 / 'a')).toBeTruthy(); }); it('considers an empty string to be falsy', () => { - //expect("" == false).toBe......();// Truthy or Falsy - //expect("" === false).toBe.....();// Truthy or Falsy + expect("" == false).toBeTruthy();// Truthy or Falsy + expect("" === false).toBeFalsy();// Truthy or Falsy }); it('considers zero to be falsy', () => { - //expect(0 == false).toBe......();// Truthy or Falsy - //expect(0 === false).toBe.....();// Truthy or Falsy + expect(0 == false).toBeTruthy();// Truthy or Falsy + expect(0 === false).toBeFalsy();// Truthy or Falsy }); it('considers nulls to be falsy', () => { @@ -50,9 +50,9 @@ describe('the JavaScript language', () => { result = false; } - //expect(result == false).toBe......();// Truthy or Falsy - //expect(null === false).toBe.....();// Truthy or Falsy - //expect(null == false).toBe....();// Truthy or Falsy + expect(result == false).toBeTruthy();// Truthy or Falsy + expect(null === false).toBeFalsy();// Truthy or Falsy + expect(null == false).toBeFalsy();// Truthy or Falsy }); it('knows the type of a function', () => { @@ -61,7 +61,7 @@ describe('the JavaScript language', () => { } expect(typeof x).toBe('function'); - //expect(typeof(xxx)).toBe('...'); + expect(typeof (xxx)).toBe('undefined'); }); it('has arrays and they can contain anything inside', () => { @@ -75,13 +75,11 @@ describe('the JavaScript language', () => { ['g', 7, 8] ]; - /* - expect(arr[1]).toEqual(); - expect(arr[4]).toEqual(); - expect(arr[6]).toEqual(); - expect(arr[9]).toEqual(); - expect(matrix[0][2]).toEqual(); - */ + expect(arr[1]).toEqual(2); + expect(arr[4]).toEqual(5); + expect(arr[6]).toEqual(undefined); + expect(arr[9]).toEqual(6); + expect(matrix[0][2]).toEqual('c'); }); it('may contain functions inside arrays', () => { @@ -93,39 +91,39 @@ describe('the JavaScript language', () => { } ]; - //expect(arr[2](1)).toEqual(); + expect(arr[2](1)).toEqual(4); }); it('concatenate arrays - well, kind of', () => { const a = [1, 2, 3]; const b = [4, 5, 6]; - //expect(a + b).toEqual(); + expect(a + b).toEqual("1,2,34,5,6"); }); it('joins arrays and strings', () => { const a = [1, 2, 3]; - //expect ("1" + a).toEqual(); - //expect(a + "1").toEqual(); + expect("1" + a).toEqual("11,2,3"); + expect(a + "1").toEqual("1,2,31"); }); it('joins arrays and other things', () => { const a = [1, 2, 3]; const b = ['x', 'y', 'z']; - //expect(1 + a).toEqual(); - //expect(a + 1).toEqual(); - //expect(1 + b).toEqual(); - //expect(true + a).toEqual(); + expect(1 + a).toEqual("11,2,3"); + expect(a + 1).toEqual("1,2,31"); + expect(1 + b).toEqual("1x,y,z"); + expect(true + a).toEqual("true1,2,3"); }); it("can't compare arrays", () => { const a = [1, 2, 3]; const b = [1, 2, 3]; - //expect(a == b).toBe.....(); // Truthy or Falsy - //expect(a === b).toBe.....(); // Truthy or Falsy + expect(a == b).toBeFalsy(); // Truthy or Falsy + expect(a === b).toBeFalsy(); // Truthy or Falsy }); it('is not the same to compare by value than by reference ', () => { @@ -143,7 +141,7 @@ describe('the JavaScript language', () => { return 'some example'; } - //expect(example()).toEqual(); + expect(example()).toEqual('some example'); }); it('can declare anonymous functions', () => { @@ -151,8 +149,8 @@ describe('the JavaScript language', () => { return a + b; }; - //expect(typeof(someVar)).toBe(); - //expect(someVar(1,1)).toBe(); + expect(typeof (someVar)).toBe("function"); + expect(someVar(1, 1)).toBe(2); }); it('may return anything', () => { @@ -162,15 +160,17 @@ describe('the JavaScript language', () => { const result = example(2); - //expect(result[1]).toEqual(); + expect(result[1]).toEqual(4); }); it('may return arrays that contains functions and so on', () => { function example() { - // write the missing code here + return [(val) => { + return [val, val * 10] + }] } - //expect(example()[0](1)[1]).toEqual(10); + expect(example()[0](1)[1]).toEqual(10); }); it("doesn't care about the declaration order when they are named", () => { @@ -178,7 +178,7 @@ describe('the JavaScript language', () => { return exampleB(1); } - //expect(exampleA()).toEqual(); + expect(exampleA()).toEqual(1); function exampleB(arg1) { return arg1; @@ -186,15 +186,15 @@ describe('the JavaScript language', () => { }); it('matters, the declaration order when they are anonymous', () => { + const exampleB = function (arg1) { + return arg1; + }; + const exampleA = function () { return exampleB(1); }; - //expect(exampleA()).toEqual(1); - - const exampleB = function (arg1) { - return arg1; - }; + expect(exampleA()).toEqual(1); }); it('can use optional parameters', () => { @@ -205,16 +205,16 @@ describe('the JavaScript language', () => { return a + b; } - //expect(example(1,1,1)).toBe(); - //expect(example(1,1)).toBe(); + expect(example(1, 1, 1)).toBe(3); + expect(example(1, 1)).toBe(2); }); it('anonymous functions are anonymous', () => { const x = function z() { return 1; }; - //expect(typeof(z)).toEqual(); - //expect(x()).toEqual(); + expect(typeof (z)).toEqual('undefined'); + expect(x()).toEqual(1); }); it('can create closures with free variables', () => { @@ -228,7 +228,7 @@ describe('the JavaScript language', () => { return internal(); } - //expect(external()).toBe(); + expect(external()).toBe(2); }); it('can create closures with several free variables', () => { @@ -240,9 +240,11 @@ describe('the JavaScript language', () => { const c = 3; return a + b + c; } + + return internal() } - //expect(external()).toBe(6); + expect(external()).toBe(6); }); it('defines a pure function when there are no free variables', () => { @@ -258,17 +260,19 @@ describe('the JavaScript language', () => { return internal(4, 4); } - //expect(external()).toBe(); + expect(external()).toBe(9); }); it('may return arrays that contains closures and so on', () => { function example() { - // write the missing code here + return [(val) => { + return [val, val + 9] + }] } - //expect(example()[0](1)[1]).toEqual(10); - //expect(example()[0](2)[1]).toEqual(11); - //expect(example()[0](3)[1]).toEqual(12); + expect(example()[0](1)[1]).toEqual(10); + expect(example()[0](2)[1]).toEqual(11); + expect(example()[0](3)[1]).toEqual(12); }); it('passes primitive types as values (a copy) to functions', () => { @@ -281,13 +285,13 @@ describe('the JavaScript language', () => { const z = true; example(x); - //expect(x).toEqual(); + expect(x).toEqual(1); example(y); - //expect(y).toEqual(); + expect(y).toEqual('example'); example(z); - //expect(z).toEqual(); + expect(z).toEqual(true); }); it('passes arrays by reference', () => { @@ -298,7 +302,7 @@ describe('the JavaScript language', () => { const x = [1, 2, 3]; example(x); - //expect(x).toEqual(); + expect(x).toEqual([100, 2, 3]); }); it('passes objects by reference', () => { @@ -309,7 +313,7 @@ describe('the JavaScript language', () => { const x = { property: 'cool!' }; example(x); - //expect(x).toEqual(); + expect(x).toEqual({ "property": "test" }); }); it('may return a function as the result of invoking a function', () => { @@ -321,9 +325,9 @@ describe('the JavaScript language', () => { return add; } - //expect(example()(1,2)).toEqual(); + expect(example()(1, 2)).toEqual(3); const f = example(); - //expect(f(2,2)).toEqual(); + expect(f(2, 2)).toEqual(4); }); it('can return closures as a function result', () => { @@ -335,7 +339,7 @@ describe('the JavaScript language', () => { const f = plus(5); - //expect(f(3)).toBe(); + expect(f(3)).toBe(8); }); it('can have functions that receive other functions as arguments', () => { @@ -347,7 +351,7 @@ describe('the JavaScript language', () => { return arg(2, 2) + 1; } - //expect(example(add)).toEqual(); + expect(example(add)).toEqual(5); }); it('may have functions as the input and the output', () => { @@ -361,7 +365,7 @@ describe('the JavaScript language', () => { return 1; }); - //expect(f(2)).toBe(); + expect(f(2)).toBe(3); }); it("can invoke functions indirectly using the special 'call'", () => { @@ -369,7 +373,7 @@ describe('the JavaScript language', () => { return a + b; } - //expect(f.call(f,1,1)).toEqual(); + expect(f.call(f, 1, 1)).toEqual(2); }); it("can invoke functions indirectly using the special 'apply'", () => { @@ -377,7 +381,7 @@ describe('the JavaScript language', () => { return a + b; } - //expect(f.apply(f, [1,1])).toEqual(); + expect(f.apply(f, [1, 1])).toEqual(2); }); it("doesn't have a private scope inside blocks", () => { @@ -386,8 +390,7 @@ describe('the JavaScript language', () => { j += i; } - //expect(i).toEqual(); - //expect(j).toEqual(); + expect(j).toEqual(10); }); }); @@ -400,7 +403,7 @@ describe('the JavaScript language', () => { } }; - //expect(obj.theName()).toBe(); + expect(obj.theName()).toBe('bob'); }); it('can create properties dynamically', () => { @@ -410,17 +413,24 @@ describe('the JavaScript language', () => { }; obj.address = 'palm tree'; - //expect(obj.address).toEqual(); - //expect(obj['address']).toEqual(); - //expect(obj['name']).toEqual(); + expect(obj.address).toEqual('palm tree'); + expect(obj['address']).toEqual('palm tree'); + expect(obj['name']).toEqual('bob'); }); it('may define complex objects', () => { - let user; + let user = { + address: { + street: "sesame" + }, + friends: [{ + name: "triki" + }] + } // write the contents of the obj to make the satisfy the expectations: - //expect(user.address.street).toEqual('sesame'); - //expect(user.friends[0].name).toEqual('triki'); + expect(user.address.street).toEqual('sesame'); + expect(user.friends[0].name).toEqual('triki'); }); it('has a pattern called, the Module Pattern', () => { @@ -440,21 +450,29 @@ describe('the JavaScript language', () => { const obj = createObject(); obj.addPoint(); - //expect(obj.score()).toEqual(); - //expect(typeof(obj.points)).toEqual(); + expect(obj.score()).toEqual(1); + expect(typeof (obj.points)).toEqual('undefined'); }); it('may create objects also with the module pattern', () => { - function createObject(initialScore) { - // write the code here + function createObject(initialScore, color) { + let score = initialScore + + return { + color: color, + incrementScoreIn: (val) => { + score += val + }, + points: () => { + return score + } + } } - /* const obj = createObject(5, 'red'); obj.incrementScoreIn(5); expect(obj.color).toEqual('red'); expect(obj.points()).toEqual(10); - */ }); it('can define constructors', () => { @@ -467,7 +485,7 @@ describe('the JavaScript language', () => { } const obj = new Obj(); - //expect(obj.theName()).toBe(); + expect(obj.theName()).toBe('bob'); }); it("may contain 'static' methods", () => { @@ -483,7 +501,7 @@ describe('the JavaScript language', () => { return 22; }; - //expect(Obj.someStaticMethod()).toBe(); + expect(Obj.someStaticMethod()).toBe(22); }); it('can have have methods in the prototype', () => { @@ -496,8 +514,8 @@ describe('the JavaScript language', () => { }; const obj = new Obj(); - //expect(obj.theName()).toEqual(); - //expect(obj.theName).toBe(new Obj().theName); + expect(obj.theName()).toEqual(undefined); + expect(obj.theName).toBe(new Obj().theName); }); it('can define a factory', () => { @@ -513,8 +531,8 @@ describe('the JavaScript language', () => { } const instance = obj(); - //expect(instance.theName()).toBe(); - //expect(instance.theName).not.toBe(obj().theName); + expect(instance.theName()).toBe('bob'); + expect(instance.theName).not.toBe(obj().theName); }); it('can create methods dynamically on an object instance', () => { @@ -527,7 +545,7 @@ describe('the JavaScript language', () => { }; } - //expect(obj.meow()).toEqual(); + expect(obj.meow()).toEqual('it works'); }); describe('the polymorphism', () => { @@ -546,8 +564,8 @@ describe('the JavaScript language', () => { Child.prototype = Object.create(Parent.prototype); // prototype chaining const child = new Child(); - //expect(child.someMethod()).toEqual(); - //expect(child.name).toEqual(); + expect(child.someMethod()).toEqual(10); + expect(child.name).toEqual('child'); }); it('may use the functional inheritance', () => { @@ -567,7 +585,7 @@ describe('the JavaScript language', () => { } const instance = child(); - //expect(instance.someMethod()).toBe(); + expect(instance.someMethod()).toBe(10); }); }); }); @@ -591,7 +609,7 @@ describe('the JavaScript language', () => { myNamespace.addOne(); myNamespace.addOne(); - //expect(myNamespace.giveMeTheCount()).toBe(); + expect(myNamespace.giveMeTheCount()).toBe(2); }); it("hoists variables the way you probably don't expect", () => { @@ -605,8 +623,8 @@ describe('the JavaScript language', () => { return functions; } - //expect(generate()[0]()).toEqual(); - //expect(generate()[1]()).toEqual(); + expect(generate()[0]()).toEqual(0); + expect(generate()[1]()).toEqual(1); }); }); @@ -664,7 +682,7 @@ describe('the JavaScript language', () => { cat.feed(); cat.feed(); - //expect(cat.kilos).toEqual(); + expect(cat.kilos).toEqual(3); }); it('works different on detached functions', () => { @@ -673,15 +691,15 @@ describe('the JavaScript language', () => { feed(); - //expect(window.kilos).toEqual(); - //expect(cat.kilos).toEqual(); + expect(window.kilos).toEqual(11); + expect(cat.kilos).toEqual(1); }); it('can be bound explicitly with CALL and APPLY', () => { const feed = cat.feed; feed.apply(cat); - //expect(cat.kilos).toEqual(); + expect(cat.kilos).toEqual(2); }); it('can be bound in modern browsers with BIND', () => { @@ -690,7 +708,7 @@ describe('the JavaScript language', () => { bound(); - //expect(cat.kilos).toEqual(); + expect(cat.kilos).toEqual(2); }); it('works different when function is attached to other object', () => { @@ -699,8 +717,8 @@ describe('the JavaScript language', () => { otherCat.feed = cat.feed; otherCat.feed(); - //expect(otherCat.kilos).toEqual(); - //expect(cat.kilos).toEqual(); + expect(otherCat.kilos).toEqual(11); + expect(cat.kilos).toEqual(1); }); it('can be handled using the SELF trick', () => { @@ -709,7 +727,7 @@ describe('the JavaScript language', () => { lion.hunt(); - //expect(lion.energy).toEqual(); + expect(lion.energy).toEqual(185); }); it('interprets the THIS when the function is executed', () => { @@ -721,7 +739,7 @@ describe('the JavaScript language', () => { }; lion.hunt(); - //expect(lion.energy).toEqual(); + expect(lion.energy).toEqual(4000); }); }); });