From 8947e732905b45b7be124699fb88e33485228039 Mon Sep 17 00:00:00 2001 From: wolframkriesing Date: Sat, 14 Oct 2023 14:57:17 +0200 Subject: [PATCH] Update --- katas/es6/language/class/creation.js | 90 +++++++++++++++++----------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/katas/es6/language/class/creation.js b/katas/es6/language/class/creation.js index e77fff40..52d0c346 100644 --- a/katas/es6/language/class/creation.js +++ b/katas/es6/language/class/creation.js @@ -2,43 +2,65 @@ // To do: make all tests pass, leave the assert lines unchanged! // Follow the hints of the failure messages! -describe('Class creation', () => { - it('is as simple as `class XXX {}`', function() { - let TestClass; - const instance = new TestClass(); +describe('Creating classes has a special syntax, using the `class` keyword', () => { + it('WHEN creating a class `XXX` THEN writing `class XXX {}` is the most common way', function() { + let MyClass; + const instance = new MyClass(); assert.equal(typeof instance, 'object'); }); - it('a class is block scoped', () => { - class Inside {} - {class Inside {}} - assert.equal(typeof Inside, 'undefined'); + it('WHEN assigning `class {}` to a variable THEN this an anonymous class', () => { + //: {"jskatas": {"terms": ["anonymous class", "assigning", "variable"]}} + const classType = {}; + assert.equal(classType.toString(), 'class {}'); }); - it('the `constructor` is a special method', function() { - class User { - constructor(id) {} - } - const user = new User(42); - assert.equal(user.id, 42); + describe('constructor, methods and other class fields', () => { + it('WHEN instantiating a class THEN the method named `constructor` is executed', function() { + //: {"jskatas": {"terms": ["instantiating", "constructor", "method"]}} + class User { + constructor() { this.id = 23; } + } + const user = new User(); + assert.equal(user.id, 42); + }); + it('WHEN instantiating a class and passing arguments THEN the constructor receives them', () => { + class User { + // constructor() { + constructor(name, id) { + this.name = name; + this.id = id; + } + } + const user = new User('The answer', 42); + assert.equal(user.name, 'The answer'); + assert.equal(user.id, 42); + }); + it('WHEN defining a custom method THEN this can be done like the `constructor`, using a custom name', function() { + class User { + + } + const notATester = new User(); + assert.equal(notATester.writesTests(), false); + }); + it('WHEN defining multiple methods THEN these can be written in any order without any special separation characters', function() { + class User { + constructor() { this.everWroteATest = false; } + wroteATest() { this.everWroteATest = true; } + isLazy() { return false; } + } + const tester = new User(); + assert.equal(tester.isLazy(), true); + tester.wroteATest(); + assert.equal(tester.isLazy(), false); + }); }); - it('defining a method by writing it inside the class body', function() { - class User { - - } - const notATester = new User(); - assert.equal(notATester.writesTests(), false); - }); - it('multiple methods need no commas (opposed to object notation)', function() { - class User { - wroteATest() { this.everWroteATest = true; } - isLazy() { } - } - const tester = new User(); - assert.equal(tester.isLazy(), true); - tester.wroteATest(); - assert.equal(tester.isLazy(), false); - }); - it('anonymous class', () => { - const classType = typeof {}; - assert.equal(classType, 'function'); + describe('GIVEN one creates a class using a class expression', () => { + it('WHEN using a class expression without a name THEN the name is derived from the variable name', function() { + const classType = typeof {}; + assert.equal(classType.name, 'classType'); + }); + it('WHEN using a class expression with a name THEN the name is that name', function() { + const classType = class {}; + assert.equal(classType.name, 'ThatName'); + }); }); });