Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframkriesing committed Oct 14, 2023
1 parent 9eb25a9 commit 8947e73
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions katas/es6/language/class/creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit 8947e73

Please sign in to comment.