-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: correct the delete last one cookie value and format the cookie (#4
- Loading branch information
Showing
5 changed files
with
146 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const initCookieStoreValue = (...args) => { | ||
if (args.length > 2) { | ||
throw new TypeError(`Expect 1 or 2 arguments, you passed ${args.length}.`); | ||
} | ||
const defaultValue = { | ||
name: '', | ||
value: '', | ||
domain: null, | ||
path: '', | ||
partitioned: false, | ||
secure: false, | ||
sameSite: 'none', | ||
expires: null, | ||
}; | ||
if (typeof args[0] === 'string' && args.length === 2) { | ||
return { | ||
...defaultValue, | ||
name: args[0], | ||
value: args[1], | ||
}; | ||
} | ||
if (typeof args[0] === 'object') { | ||
return { | ||
...defaultValue, | ||
...args[0], | ||
}; | ||
} | ||
throw new TypeError('Cannot handle the cookieStore arguments, please check.'); | ||
}; | ||
|
||
class CookieChangeEvent extends Event { | ||
changed = []; | ||
deleted = []; | ||
|
||
constructor(type, data) { | ||
super(type); | ||
this.changed = data.changed; | ||
this.deleted = data.deleted; | ||
} | ||
} | ||
|
||
class CookieStore extends EventTarget { | ||
mockStoreValue = []; | ||
|
||
async getAll() { | ||
// Mock value | ||
return this.mockStoreValue; | ||
} | ||
|
||
async set(name, value) { | ||
return new Promise((resolve) => { | ||
const storeValue = initCookieStoreValue(name, value); | ||
this.mockStoreValue.push(storeValue); | ||
this.dispatchEvent( | ||
new CookieChangeEvent('change', { | ||
changed: [storeValue], | ||
deleted: [], | ||
}), | ||
); | ||
resolve(); | ||
}); | ||
} | ||
|
||
async delete(name) { | ||
const result = []; | ||
const deleted = []; | ||
this.mockStoreValue.forEach((item) => { | ||
if (item.name === name) { | ||
deleted.push(item); | ||
} else { | ||
result.push(item); | ||
} | ||
}); | ||
if (deleted.length === 0) return; | ||
this.mockStoreValue = result; | ||
this.dispatchEvent( | ||
new CookieChangeEvent('change', { | ||
changed: [], | ||
deleted, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
window.cookieStore = new CookieStore(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import { StoragePlugin } from 'src/plugins/storage'; | ||
|
||
const trigger = jest.fn(); | ||
// @ts-ignore | ||
jest.spyOn(StoragePlugin, 'sendStorageItem').mockImplementation(trigger); | ||
const trigger = jest.spyOn(StoragePlugin, 'sendStorageItem'); | ||
|
||
beforeEach(() => { | ||
localStorage.clear(); | ||
sessionStorage.clear(); | ||
document.cookie = ''; | ||
}); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('Storage plugin', () => { | ||
it('Storage action', () => { | ||
localStorage.setItem('1', '1'); | ||
sessionStorage.setItem('2', '2'); | ||
document.cookie = '3=3'; | ||
it('cookieStore / localStorage /sessionStorage', async () => { | ||
new StoragePlugin().onCreated(); | ||
await cookieStore.set('1', '1'); | ||
await cookieStore.set('2', '2'); | ||
expect(trigger).toHaveBeenCalledTimes(3); | ||
|
||
localStorage.removeItem('1'); | ||
sessionStorage.removeItem('1'); | ||
await cookieStore.delete('1'); | ||
await cookieStore.delete('2'); | ||
expect(trigger).toHaveBeenCalledTimes(5); | ||
|
||
localStorage.setItem('1', '1'); | ||
sessionStorage.setItem('2', '2'); | ||
expect(trigger).toHaveBeenCalledTimes(7); | ||
localStorage.removeItem('1'); | ||
sessionStorage.removeItem('2'); | ||
expect(trigger).toHaveBeenCalledTimes(9); | ||
localStorage.clear(); | ||
sessionStorage.clear(); | ||
expect(trigger).toHaveBeenCalledTimes(7); | ||
expect(trigger).toHaveBeenCalledTimes(11); | ||
}); | ||
}); |
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