Skip to content

Commit

Permalink
test: cookie() feat more
Browse files Browse the repository at this point in the history
  • Loading branch information
halldwang committed Aug 8, 2020
1 parent 8c61335 commit 05ac397
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions test/cookie/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe('bbo.cookie()', () => {
expect(bbo.cookie().remove).toBeInstanceOf(Function);
});

test('cookie().set()', function() {
test('bbo.cookie().set()', function() {
bbo.cookie().set('c', 'v');
expect(document.cookie).toBe('c=v');
bbo.cookie().set('c1', 'v');
expect(document.cookie).toBe(cookie);
});

test('cookie().set() a object', function() {
test('bbo.cookie().set() a object', function() {
bbo.cookie().set('name', { foo: 'bar' });
expect(document.cookie).toBe(`${cookie}; ${cookieName}`);
});
Expand All @@ -43,12 +43,12 @@ describe('bbo.cookie()', () => {
expect(document.cookie).toBe(`c=v1; c1=v; ${cookieName}`);
});

test('cookie().get()', function() {
test('bbo.cookie().get()', function() {
const c = bbo.cookie().get('c');
expect(c).toBe('v1');
});

test('cookie().get()', function() {
test('bbo.cookie().get()', function() {
const c = bbo.cookie().get('c');
const get = bbo.cookie().get();
const getJson = bbo.cookie().getJSON();
Expand All @@ -62,9 +62,59 @@ describe('bbo.cookie()', () => {
expect(getkeyJson).toEqual(objectJson.name);
});

test('cookie().remove()', function() {
test('bbo.cookie().remove()', function() {
bbo.cookie().remove('c');
const c = bbo.cookie().get('c');
expect(c).toBe(undefined);
});

test('bbo.cookie() add read', function() {
const wirte = bbo.cookie().set('c1', 'v1');
expect(wirte).toEqual('c1=v1; path=/');
const read = bbo.cookie().set('c2', 'v2');
expect(read).toEqual('c2=v2; path=/');
const readNew = bbo.cookie().set('c2', 'v2');
expect(readNew).toEqual('c2=v2; path=/');
});

test('bbo.cookie().set() more', function() {
bbo.cookie().set('c3', 'v', { expires: 7, path: '/', domain: '.github.com', secure: true });
expect(document.cookie).toBe(`c1=v1; ${cookieName}; c2=v2`);
});

test('bbo.cookie() extend write decoder', function() {
bbo
.cookie()
.withConverter({
write: function(value) {
let encoded = value.replace('a', 'A');
return bbo.cookie().withConverter(encoded);
}
})
.set('c', 'a;');
expect(document.cookie).toBe(`c1=v1; ${cookieName}; c2=v2`);
});

test('bbo.cookie() extend read decoder', function() {
const readConverter = function(value) {
return value.toUpperCase();
};
const api = bbo.cookie().withConverter({
read: readConverter
});

bbo
.cookie()
.withConverter(api)
.set('c2', 'a2;');

expect(document.cookie).toBe(`c1=v1; ${cookieName}; c2=a2%3B`);
});

test('cookie error', function() {
document.cookie = 'c1';
bbo.cookie().set('c', 'v2');

expect(document.cookie).toBe(`c1=v1; ${cookieName}; c2=a2%3B; c1; c=v2`);
});
});

0 comments on commit 05ac397

Please sign in to comment.