@tdb/util
Version:
Shared helpers and utilities.
103 lines • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var _1 = require(".");
describe('cookie', function () {
describe('read cookie', function () {
var obj = { text: 'MyText', number: 1234, flag: true };
var cookies = {
TEXT: 'Hello',
OBJECT: JSON.stringify(obj),
};
var ctx = {
req: { cookies: cookies },
res: {
setHeader: function (key, value) {
return;
},
},
};
it('stores the key on the property function', function () {
var prop = _1.cookie.prop('MY_KEY');
chai_1.expect(prop.key).to.eql('MY_KEY');
});
it('store options on the property function', function () {
var prop = _1.cookie.prop('MY_KEY', { path: '/foo' });
chai_1.expect(prop.options).to.eql({ path: '/foo' });
});
it('sets the [isProp] flag on the property function', function () {
var prop = _1.cookie.prop('MY_KEY');
chai_1.expect(prop.isProp).to.eql(true);
});
it('reads string', function () {
var prop = _1.cookie.prop('TEXT');
var result = prop(undefined, { ctx: ctx });
chai_1.expect(result).to.eql('Hello');
});
it('reads object', function () {
var prop = _1.cookie.prop('OBJECT');
var result = prop(undefined, { ctx: ctx });
chai_1.expect(result).to.eql(obj);
chai_1.expect(result && result.text).to.eql('MyText');
chai_1.expect(result && result.number).to.eql(1234);
});
it('has no default value', function () {
var prop = _1.cookie.prop('FOO');
chai_1.expect(prop()).to.eql(undefined);
});
it('has a default value from prop [options]', function () {
var prop = _1.cookie.prop('FOO');
chai_1.expect(prop(undefined, { default: 'hello' })).to.eql('hello');
});
it('has a default value from the curried [defaultOptions]', function () {
var prop = _1.cookie.prop('FOO', { default: 'Yo' });
chai_1.expect(prop()).to.eql('Yo');
});
it('overrides the curried [defaultOptions] with prop [options]', function () {
var prop = _1.cookie.prop('FOO', { default: 'Yo' });
chai_1.expect(prop(undefined, { default: 'hello' })).to.eql('hello');
});
});
describe('ServerCookie', function () {
var myCookies = {
foo: _1.cookie.prop('FOO'),
bar: _1.cookie.prop('BAR'),
baz: _1.cookie.prop('BAZ', { expires: 30 }),
};
it('has no cookies by default', function () {
var server = new _1.ServerCookie();
chai_1.expect(server.cookies).to.eql([]);
});
it('adds cookies', function () {
var server = new _1.ServerCookie();
server
.add(myCookies.foo, 'one')
.add(myCookies.bar, { bar: 123 })
.add(myCookies.baz, 'three');
chai_1.expect(server.cookies.length).to.eql(3);
chai_1.expect(server.cookies[0].key).to.eql('FOO');
chai_1.expect(server.cookies[0].value).to.eql('one');
chai_1.expect(server.cookies[0].options).to.eql(undefined);
chai_1.expect(server.cookies[1].key).to.eql('BAR');
chai_1.expect(server.cookies[1].value).to.eql({ bar: 123 });
chai_1.expect(server.cookies[1].options).to.eql(undefined);
chai_1.expect(server.cookies[2].key).to.eql('BAZ');
chai_1.expect(server.cookies[2].value).to.eql('three');
chai_1.expect(server.cookies[2].options).to.eql({ expires: 30 });
});
it('clears all cookies', function () {
var server = new _1.ServerCookie();
server.add(myCookies.foo, 'one').add(myCookies.bar, { bar: 123 });
chai_1.expect(server.cookies.length).to.eql(2);
server.clear();
chai_1.expect(server.cookies.length).to.eql(0);
});
it('constructs "Set-Cookie" header', function () {
var server = new _1.ServerCookie();
server.add(myCookies.foo, 'my-value');
var header = server.header;
chai_1.expect(header).to.eql('__TDB_TEMP__=%7B%22FOO%22%3A%7B%22key%22%3A%22FOO%22%2C%22value%22%3A%22my-value%22%7D%7D');
});
});
});
//# sourceMappingURL=prop.test.js.map