From 05ac3976df9db9d842c3ef5f74ceffd73a0b50b0 Mon Sep 17 00:00:00 2001 From: hrout Date: Sun, 9 Aug 2020 01:43:09 +0800 Subject: [PATCH] test: cookie() feat more --- test/cookie/cookie.js | 60 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/test/cookie/cookie.js b/test/cookie/cookie.js index fbfc0fb..0a17301 100644 --- a/test/cookie/cookie.js +++ b/test/cookie/cookie.js @@ -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}`); }); @@ -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(); @@ -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`); + }); });