diff --git a/README.md b/README.md index 6de2231..ebee347 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ For a more complex layout use the **scoped slot** | accept | Closes the cookie disclaimer and saves to localStorage | | close | Only closes the cookie disclaimer. The disclaimer will reappear on the next page load. | | open | Show disclaimer if user ignored him | +| revoke | Revoke previous user decision | +| isAccepted | To check anytime if cookies has been accepted | ## Props | prop | default | type | description diff --git a/src/components/CookieLaw.vue b/src/components/CookieLaw.vue index e43aeae..47b96b5 100644 --- a/src/components/CookieLaw.vue +++ b/src/components/CookieLaw.vue @@ -145,6 +145,11 @@ this.isOpen = true } }, + mounted () { + if (this.isAccepted() === true) { + this.$emit('accept') + } + }, methods: { setVisited () { if (this.canUseLocalStorage) { @@ -168,11 +173,32 @@ } }, getVisited () { + let visited = false + if (this.canUseLocalStorage) { + visited = localStorage.getItem(this.storageName) + } else { + visited = Cookie.get(this.storageName) + } + + if (typeof visited === 'string') { + visited = JSON.parse(visited) + } + + return visited + }, + isAccepted () { + let accepted = false if (this.canUseLocalStorage) { - return localStorage.getItem(this.storageName) + accepted = localStorage.getItem('cookie:all') } else { - return Cookie.get(this.storageName) + accepted = Cookie.get('cookie:all') + } + + if (typeof accepted === 'string') { + accepted = JSON.parse(accepted) } + + return accepted }, accept () { this.setVisited() @@ -190,6 +216,15 @@ this.isOpen = false this.$emit('decline') }, + revoke () { + if (this.canUseLocalStorage) { + localStorage.removeItem(this.storageName) + } else { + Cookie.remove(this.storageName) + } + this.isOpen = true + this.$emit('revoke') + }, open () { if (!this.getVisited() === true) { this.isOpen = true diff --git a/test/unit/specs/CookieLaw.spec.js b/test/unit/specs/CookieLaw.spec.js index 00df681..7ddc1b5 100644 --- a/test/unit/specs/CookieLaw.spec.js +++ b/test/unit/specs/CookieLaw.spec.js @@ -1,8 +1,9 @@ -import { mount } from "vue-test-utils"; +import { mount } from 'vue-test-utils' import CookieLaw from '@/components/CookieLaw' let mockGetCookie = jest.fn(); let mockSetCookie = jest.fn(); +let mockRemoveCookie = jest.fn(); jest.mock('tiny-cookie', () => ({ __esModule: true, // mock the exports @@ -12,6 +13,9 @@ jest.mock('tiny-cookie', () => ({ get: jest.fn().mockImplementation((...args) => { mockGetCookie(...args); }), + remove: jest.fn().mockImplementation((...args) => { + mockRemoveCookie(...args); + }), })); describe('CookieLaw.vue', () => { @@ -21,6 +25,17 @@ describe('CookieLaw.vue', () => { .toEqual('This website uses cookies to ensure you get the best experience on our website.') }) + it('should call "isAccepted" method when mount ', async () => { + const mockFn = jest.fn(); + mount(CookieLaw, { + methods: { + isAccepted: mockFn + } + }) + + expect(mockFn).toHaveBeenCalled() + }) + it('should set localstorage when clicking confirm button', () => { const wrapper = mount(CookieLaw) @@ -55,6 +70,7 @@ describe('CookieLaw.vue', () => { wrapper.find('.Cookie__button').trigger('click') expect(mockSetCookie).toHaveBeenCalledWith('cookie:accepted', true, {"expires": "1Y"}); + }) it('should set cookie when domain prop set', () => { @@ -116,4 +132,61 @@ describe('CookieLaw.vue', () => { expect(wrapper.html()).toBe(undefined) }); + + it('should trigger "accept" event when mounted if cookie has been already acccepted ', async () => { + localStorage.setItem('cookie:all', 'true') + + const wrapper = mount(CookieLaw) + + expect(wrapper.emitted()).toHaveProperty('accept') + + localStorage.clear() + }) + + it('should NOT trigger "accept" event when mounted if cookie has not been already accepted ', async () => { + const wrapper = mount(CookieLaw) + + console.log(wrapper.emitted()) + + expect(wrapper.emitted()).not.toHaveProperty('accept') + + localStorage.clear() + }) + + it('should trigger "revoke" event and remove previous user choice if revoke() method is called', async () => { + const storageName = 'cookie:test' + localStorage.setItem(storageName, 'true') + + const wrapper = mount(CookieLaw, { + propsData: { + storageName: storageName + } + }) + + wrapper.vm.revoke() + + expect(wrapper.emitted()).toHaveProperty('revoke') + expect(localStorage.getItem(storageName)).toBeNull() + + localStorage.clear() + }) + + it('should trigger "revoke" event and remove previous user choice if revoke() method is called (using COOKIE)', async () => { + const storageName = 'cookie:test' + localStorage.setItem(storageName, 'true') + + const wrapper = mount(CookieLaw, { + propsData: { + storageName: storageName, + storageType: 'cookies' + } + }) + + wrapper.vm.revoke() + + expect(wrapper.emitted()).toHaveProperty('revoke') + expect(mockRemoveCookie).toHaveBeenCalled(); + + localStorage.clear() + }) })