Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add is valid and revoke methods #65

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions src/components/CookieLaw.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
this.isOpen = true
}
},
mounted () {
if (this.isAccepted() === true) {
this.$emit('accept')
}
},
methods: {
setVisited () {
if (this.canUseLocalStorage) {
Expand All @@ -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()
Expand All @@ -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
Expand Down
75 changes: 74 additions & 1 deletion test/unit/specs/CookieLaw.spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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', () => {
Expand All @@ -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)

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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()
})
})