Skip to content

Commit

Permalink
feat: Add prop to set cookie options (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenIndirecto authored and apertureless committed Oct 29, 2019
1 parent 093cd4a commit a42cc23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ For a more complex layout use the **scoped slot**
| transitionName | 'slideFromBottom' | String | Enter and leave transitions. Currently supported `slideFromBottom`, `slideFromTop`, `fade`
| storageName | 'localStorage' | String | Name for the localStorage / cookie name. Defaults to `cookie:accepted`
| storageType | 'localStorage' | String | Type of storage, where to store 'cookies:accept': true. Can be `localStorage` (default) or `cookies`. If LocalStorage is unsupported, then used Cookies.
| cookieOptions | {} | Object | (Optional) The cookieOptions parameter is an object. And its property can be a valid cookie option, such as `path`, `domain`, `expires` / `max-age`, `samesite` or `secure`. See [tiny-cookie docs](https://github.com/Alex1990/tiny-cookie#setkey-value-options) for details.

## Events

Expand Down
7 changes: 6 additions & 1 deletion src/components/CookieLaw.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
storageType: {
type: String,
default: STORAGE_TYPES.local
},
cookieOptions: {
type: Object,
default: () => {},
required: false
}
},
data () {
Expand Down Expand Up @@ -132,7 +137,7 @@
if (this.canUseLocalStorage) {
localStorage.setItem(this.storageName, true)
} else {
Cookie.set(this.storageName, true)
Cookie.set(this.storageName, true, this.cookieOptions)
}
},
getVisited () {
Expand Down
28 changes: 27 additions & 1 deletion test/unit/specs/CookieLaw.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vue from 'vue'
import CookieLaw from '@/components/CookieLaw'
// import * as Cookie from 'tiny-cookie'
import * as Cookie from 'tiny-cookie'

describe('CookieLaw.vue', () => {
it('should render correct contents', () => {
Expand Down Expand Up @@ -31,6 +31,32 @@ describe('CookieLaw.vue', () => {
expect(vm.$el.querySelector('.Cookie__buttons > a').getAttribute('target'))
.to.equal('_blank')
})
it('should set cookie when domain prop is not set', () => {
const Constructor = Vue.extend(CookieLaw)
const vm = new Constructor({ propsData: { storageType: 'cookies' } }).$mount()

expect(Cookie.get('cookie:accepted')).to.equal(null)

vm.$el.querySelector('.Cookie__button').click()

expect(Cookie.get('cookie:accepted')).to.equal('true')

Cookie.remove('cookie:accepted')
})
it('should set cookie when domain prop set', () => {
const Constructor = Vue.extend(CookieLaw)
const vm = new Constructor({
propsData: { storageType: 'cookies', cookieOptions: { domain: 'localhost' } }
}).$mount()

expect(Cookie.get('cookie:accepted')).to.equal(null)

vm.$el.querySelector('.Cookie__button').click()

expect(Cookie.get('cookie:accepted')).to.equal('true')

Cookie.remove('cookie:accepted', { domain: 'localhost' })
})
// it('should set a cookie when localstorage is not available', () => {
// const Constructor = Vue.extend(CookieLaw)
// const vm = new Constructor().$mount()
Expand Down

0 comments on commit a42cc23

Please sign in to comment.