Created
January 23, 2017 17:21
-
-
Save rmtsrc/8bd2b5eb033751a97b19c72b631d2e73 to your computer and use it in GitHub Desktop.
Get a Browser Cookie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @see http://stackoverflow.com/a/15724300 | |
export default (cookieName) => { | |
const value = `; ${document.cookie}`; | |
const parts = value.split(`; ${cookieName}=`); | |
if (parts.length === 2) { | |
return parts.pop().split(';').shift(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai'; | |
import getBrowserCookie from './'; | |
describe('getBrowserCookie', () => { | |
beforeEach(() => { | |
document.cookie = 'some-token=Some.Token1; tags=id:123456$_sn:60$_ss:0$_st:1234567$ses_id:123456%3Bexp-session$_pn:12%3Bexp-session; WTF=id=123abc:ab=123:cd=1234567'; | |
}); | |
it('will return undefined if it cannot find the cookie', () =>{ | |
expect(getBrowserCookie('something-that-does-exist')).to.equal(undefined); | |
}); | |
it('should return the correct value of a cookie', () => { | |
expect(getBrowserCookie('some-token')).to.equal('Some.Token1'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment