-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AG-20095 Fix 'trusted-set-cookie-reload' — website is not reloaded if…
… '$now$' value is used. #291 Squashed commit of the following: commit 67987c5 Author: Slava Leleka <v.leleka@adguard.com> Date: Fri Apr 28 17:21:53 2023 +0300 fix changelog link commit c9679ed Author: Adam Wróblewski <adam@adguard.com> Date: Fri Apr 28 16:10:29 2023 +0200 Remove unnecessary tests Use toBeTruthy() instead of toBe(true) in tests Add better comment about path in clearCookie function Move fix to unreleased section in changelog commit 91abfd2 Author: Adam Wróblewski <adam@adguard.com> Date: Fri Apr 28 15:05:31 2023 +0200 Add information about jest to readme Mock console.trace to fix errors in jest tests commit c5c53db Author: Maxim Topciu <mtopciu@adguard.com> Date: Fri Apr 28 15:34:54 2023 +0300 AG-20095 rename test to spec commit c39dbc2 Author: Maxim Topciu <mtopciu@adguard.com> Date: Fri Apr 28 14:14:24 2023 +0300 Add yarn test to package.json commit ecc65f6 Merge: 085a374 402972c Author: Adam Wróblewski <adam@adguard.com> Date: Fri Apr 28 13:13:22 2023 +0200 Merge branch 'master' into fix/AG-20095 commit 085a374 Author: Adam Wróblewski <adam@adguard.com> Date: Thu Apr 27 20:37:50 2023 +0200 Use jest test for trusted-set-cookie-reload Use better approach to check cookie commit 6ac14db Author: Adam Wróblewski <adam@adguard.com> Date: Mon Apr 3 12:01:23 2023 +0200 Fix 'trusted-set-cookie-reload' — website is not reloaded if '$now$' value is used
- Loading branch information
Showing
8 changed files
with
1,871 additions
and
28 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
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
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,114 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable no-underscore-dangle */ | ||
|
||
import { trustedSetCookieReload } from '../../src/scriptlets/trusted-set-cookie-reload'; | ||
import { parseCookieString } from '../../src/helpers'; | ||
import { | ||
clearGlobalProps, | ||
clearCookie, | ||
} from '../helpers'; | ||
|
||
beforeEach(() => { | ||
window.__debug = () => { | ||
window.hit = 'FIRED'; | ||
}; | ||
Object.defineProperty(window, 'location', { | ||
configurable: true, | ||
value: { | ||
reload: | ||
jest.fn(), | ||
}, | ||
}); | ||
|
||
// Mocking console.trace() because | ||
// it causes errors in tests using jest | ||
window.console.trace = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
clearGlobalProps('hit', '__debug'); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Test trusted-set-cookie-reload scriptlet', () => { | ||
const sourceParams = { | ||
name: 'trusted-set-cookie-reload', | ||
verbose: true, | ||
}; | ||
|
||
test('Set cookie with current time value', () => { | ||
const cName = '__test-cookie_current_time'; | ||
const cValue = '$now$'; | ||
const expiresSec = ''; | ||
const cPath = '/'; | ||
|
||
trustedSetCookieReload(sourceParams, cName, cValue, expiresSec, cPath); | ||
|
||
// Some time will pass between calling scriptlet | ||
// and jest running test | ||
const tolerance = 125; | ||
const cookieValue = parseCookieString(document.cookie)[cName]; | ||
const currentTime = new Date().getTime(); | ||
const timeDiff = currentTime - cookieValue; | ||
|
||
expect(timeDiff).toBeLessThan(tolerance); | ||
expect(document.cookie.includes(cName)).toBeTruthy(); | ||
expect(window.location.reload).toHaveBeenCalledTimes(1); | ||
expect(window.hit).toBe('FIRED'); | ||
clearCookie(cName); | ||
}); | ||
|
||
test('Set cookie with current date value', () => { | ||
const cName = '__test-cookie_current_date'; | ||
const cValue = '$currentDate$'; | ||
const expiresSec = ''; | ||
const cPath = '/'; | ||
|
||
trustedSetCookieReload(sourceParams, cName, cValue, expiresSec, cPath); | ||
|
||
// Some time will pass between calling scriptlet | ||
// and jest running test | ||
const cookieValue = parseCookieString(document.cookie)[cName]; | ||
// Check only day, month and year | ||
const currentDate = encodeURIComponent(Date().split(' ', 4).join(' ')); | ||
const dateDiff = cookieValue.split(' ', 4).join(' '); | ||
|
||
expect(dateDiff.startsWith(currentDate)).toBeTruthy(); | ||
expect(document.cookie.includes(cName)).toBeTruthy(); | ||
expect(window.location.reload).toHaveBeenCalledTimes(1); | ||
expect(window.hit).toBe('FIRED'); | ||
clearCookie(cName); | ||
}); | ||
|
||
test('Set cookie string', () => { | ||
const cName = '__test-cookie_OK'; | ||
const cValue = 'OK'; | ||
const expiresSec = ''; | ||
const cPath = '/'; | ||
|
||
trustedSetCookieReload(sourceParams, cName, cValue, expiresSec, cPath); | ||
|
||
expect(document.cookie.includes(cName)).toBeTruthy(); | ||
expect(document.cookie.includes(cValue)).toBeTruthy(); | ||
expect(window.location.reload).toHaveBeenCalledTimes(1); | ||
expect(window.hit).toBe('FIRED'); | ||
clearCookie(cName); | ||
}); | ||
|
||
test('Cookie already set, should not reload', () => { | ||
const cName = '__test-cookie_set'; | ||
const cValue = 'test'; | ||
const expiresSec = ''; | ||
const cPath = '/'; | ||
|
||
document.cookie = `${cName}=${cValue};`; | ||
|
||
trustedSetCookieReload(sourceParams, cName, cValue, expiresSec, cPath); | ||
|
||
expect(window.location.reload).toHaveBeenCalledTimes(0); | ||
expect(window.hit).toBe(undefined); | ||
clearCookie(cName); | ||
}); | ||
}); |
Oops, something went wrong.