4 const assert
= require('assert');
5 const { lazy
} = require('../index');
7 describe('lazy', function () {
8 let o
, initializer
, called
;
10 beforeEach(function () {
19 it('does not initialize if not accessed', function () {
20 lazy(o
, 'p', initializer
);
21 assert
.strictEqual(called
, false);
22 assert(Object
.keys(o
).includes('p'));
25 it('wants a callable initializer', function () {
27 lazy(o
, 'p', undefined);
28 assert
.fail('should reject un-callable initializer');
30 assert(e
instanceof TypeError
, `expected 'TypeError', got '${e.name}'`);
34 it('initializes if accessed', function () {
35 lazy(o
, 'p', initializer
);
37 assert
.strictEqual(called
, true);
38 assert
.strictEqual(v
, 'value');
41 it('handles symbolic properties', function () {
42 const s
= Symbol('s');
43 lazy(o
, s
, initializer
);
45 assert
.strictEqual(called
, true);
46 assert
.strictEqual(v
, 'value');
49 it('can be overwritten before being read', function () {
50 const expected
= 'foobar';
51 lazy(o
, 'p', initializer
);
54 assert
.strictEqual(v
, expected
);
55 assert
.strictEqual(called
, false);