Issues (204)

test/ClassBuilder/CallTest.js (1 issue)

Severity
1
/* Javascript Object Inheritance Implementation                ______  ________
2
 * (c) 2016 <[email protected]>                             __ / / __ \/  _/  _/
3
 * Licensed under MIT.                                    / // / /_/ // /_/ /
4
 * ------------------------------------------------------ \___/\____/___/__*/
5
var JOII = require('../../dist/joii').JOII;
6
7
/**
8
* Tests the functionality of __call.
9
*/
10
test('ClassBuilder:CallTest', function(assert) {
11
12
    var C1 = JOII.ClassBuilder({
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
13
        __call: function() { return true; }
14
    });
15
    assert.strictEqual(C1(), true, '__call function OK.');
16
17
    // __call may not return "this", because it references to the static
18
    // definition of the class body.
19
    assert.throws(function() {
20
        var a = JOII.ClassBuilder({ 'public function foo' : function() {}, __call: function() { return this; }}); a();
21
    }, function(err) { return err === '__call cannot return itself.'; }, '__call cannot return itself.');
22
23
    // Test the context of a static call.
24
    var C2 = JOII.ClassBuilder({
25
        a: 1,
26
27
        __call: function(val) {
28
            if (!val) {
29
                return this.a;
30
            }
31
            this.a = val;
32
        }
33
    });
34
35
    var c2 = new C2();
36
    assert.strictEqual(c2.getA(), 1, 'c2.getA() returns this.a (1)');
37
    assert.strictEqual(C2(), 1, '__call returns this.a (1)');
38
39
    // Update the value, THEN create another instance and check again...
40
    C2(2);
41
    var c2a = new C2();
42
    assert.strictEqual(C2(), 2, '__call returns this.a (2)');
43
    assert.strictEqual(c2.getA(), 1, 'c2.getA() returns this.a (1)');
44
    assert.strictEqual(c2a.getA(), 1, 'c2a.getA() returns this.a (1)');
45
46
47
    // 3.1.0: Custom callable method names.
48
49
    var C3 = JOII.ClassBuilder({'<>': function() { return 1; } });
50
    assert.strictEqual(C3(), 1, 'New default call method "<>" used.');
51
52
    JOII.Config.addCallable('execute');
53
    var C4 = JOII.ClassBuilder({'execute': function() { return 2; } });
54
    assert.strictEqual(C4(), 2, 'Custom call method "execute" used.');
55
56
});
57