UNPKG

@tdb/util

Version:
323 lines (322 loc) 13.8 kB
"use strict"; var _this = this; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var chai_1 = require("chai"); var proxyquire = require("proxyquire"); var _1 = require("."); var mockData = {}; var storeMock = { init: function () { return { getItem: function (key) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2, mockData[key]]; }); }); }, setItem: function (key, value) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { mockData[key] = value; return [2]; }); }); }, removeItem: function (key) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { delete mockData[key]; return [2]; }); }); }, }; }, }; var prop = proxyquire('./prop', { './store.web': storeMock, }).prop; describe('local-storage', function () { beforeEach(function () { mockData = {}; }); it('returns a function', function () { var myProp = prop('MY_KEY'); chai_1.expect(myProp).to.be.an.instanceof(Function); }); it('stores the key on the function', function () { var myProp = prop('MY_KEY'); chai_1.expect(myProp.key).to.eql('MY_KEY'); }); it('sets the [isProp] flag on the property function', function () { var myProp = prop('MY_KEY'); chai_1.expect(myProp.isProp).to.eql(true); }); describe('default value', function () { it('has no default value', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: myProp = prop('MY_PROP'); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_b.sent()]).to.eql(undefined); return [2]; } }); }); }); it('has a curried default property', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: myProp = prop('MY_PROP', { default: 'My Default' }); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_b.sent()]).to.eql('My Default'); return [2]; } }); }); }); it('overrides the curried property value', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, result; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: myProp = prop('MY_PROP', { default: 'My Curried value' }); return [4, myProp(undefined, { default: 'Overridden' })]; case 1: result = _a.sent(); chai_1.expect(result).to.eql('Overridden'); return [2]; } }); }); }); }); describe('nothing (no value)', function () { it('undefined', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: myProp = prop('UNDEFINED_KEY'); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_b.sent()]).to.eql(undefined); return [2]; } }); }); }); it('null - reset the value', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a, _b, _c; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: myProp = prop('NULL_KEY'); _a = chai_1.expect; return [4, myProp(undefined, { default: null })]; case 1: _a.apply(void 0, [_d.sent()]).to.equal(null); myProp('myValue'); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_d.sent()]).to.equal('myValue'); myProp(null); _c = chai_1.expect; return [4, myProp()]; case 3: _c.apply(void 0, [_d.sent()]).to.equal(undefined); return [2]; } }); }); }); }); describe('data types', function () { it('boolean', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a, _b, _c, _d; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: myProp = prop('BOOL_KEY', { default: false }); _a = chai_1.expect; return [4, myProp(undefined)]; case 1: _a.apply(void 0, [_e.sent()]).to.equal(false); _b = chai_1.expect; return [4, myProp(undefined, { default: true })]; case 2: _b.apply(void 0, [_e.sent()]).to.equal(true); myProp(true); _c = chai_1.expect; return [4, myProp()]; case 3: _c.apply(void 0, [_e.sent()]).to.equal(true); myProp(false); _d = chai_1.expect; return [4, myProp()]; case 4: _d.apply(void 0, [_e.sent()]).to.equal(false); return [2]; } }); }); }); it('string', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: myProp = prop('STRING_KEY', { default: 'myDefault' }); _a = chai_1.expect; return [4, myProp(undefined)]; case 1: _a.apply(void 0, [_c.sent()]).to.equal('myDefault'); myProp('myValue'); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_c.sent()]).to.equal('myValue'); return [2]; } }); }); }); it('number', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: myProp = prop('NUMBER_KEY', { default: 321 }); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_c.sent()]).to.equal(321); myProp(1.23); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_c.sent()]).to.equal(1.23); return [2]; } }); }); }); it('object', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var myProp, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: myProp = prop('OBJECT_KEY', { default: { value: 123 } }); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_c.sent()]).to.eql({ value: 123 }); myProp({ root: { child: 88 } }); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_c.sent()]).to.eql({ root: { child: 88 } }); return [2]; } }); }); }); it('date', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var now, myProp, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: now = new Date(); myProp = prop('DATE_KEY', { default: now }); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_c.sent()]).to.eql(now); myProp(now); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_c.sent()]).to.eql(now); return [2]; } }); }); }); it('date - iso string when on object', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var now, myProp, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: now = new Date(); myProp = prop('DATE_KEY', { default: now }); _a = chai_1.expect; return [4, myProp()]; case 1: _a.apply(void 0, [_c.sent()]).to.eql(now); myProp({ now: now }); _b = chai_1.expect; return [4, myProp()]; case 2: _b.apply(void 0, [_c.sent()]).to.eql({ now: now.toISOString() }); return [2]; } }); }); }); describe('events$ (Observable)', function () { var myProp = prop('EVENT_PROP'); var count = 0; var event; _1.localStorage.changes$.forEach(function (e) { count += 1; event = e; }); beforeEach(function () { count = 0; event = undefined; }); it('fires write event', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, type, key, value; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: return [4, myProp(123)]; case 1: _b.sent(); chai_1.expect(count).to.equal(1); chai_1.expect(event).to.not.equal(undefined); if (event) { _a = event, type = _a.type, key = _a.key, value = _a.value; chai_1.expect(type).to.equal('number'); chai_1.expect(key).to.equal(myProp.key); chai_1.expect(value).to.equal(123); } return [2]; } }); }); }); it('fires write event (null)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, type, key, value; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: return [4, myProp(123)]; case 1: _b.sent(); return [4, myProp(null)]; case 2: _b.sent(); chai_1.expect(count).to.equal(2); chai_1.expect(event).to.not.equal(undefined); if (event) { _a = event, type = _a.type, key = _a.key, value = _a.value; chai_1.expect(type).to.equal('null'); chai_1.expect(key).to.equal(myProp.key); chai_1.expect(value).to.equal(null); } return [2]; } }); }); }); }); }); });