Skip to content

Commit

Permalink
Merge branch 'main' into ab/refactor-comment-state
Browse files Browse the repository at this point in the history
  • Loading branch information
abeddow91 authored Jan 24, 2024
2 parents 855126e + 0b3623e commit 6f04d1b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dotcom-rendering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@guardian/eslint-plugin-source-react-components": "21.0.1",
"@guardian/identity-auth": "1.1.0",
"@guardian/identity-auth-frontend": "1.0.0",
"@guardian/libs": "16.0.0",
"@guardian/libs": "16.0.1",
"@guardian/shimport": "1.0.2",
"@guardian/source-foundations": "14.1.2",
"@guardian/source-react-components": "18.0.0",
Expand Down
48 changes: 44 additions & 4 deletions dotcom-rendering/src/lib/contributions.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { setCookie } from '@guardian/libs';
import { setCookie, storage } from '@guardian/libs';
import MockDate from 'mockdate';
import {
getLastOneOffContributionTimestamp,
isRecentOneOffContributor,
NO_RR_BANNER_KEY,
ONE_OFF_CONTRIBUTION_DATE_COOKIE,
recentlyClosedBanner,
setLocalNoBannerCachePeriod,
SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
withinLocalNoBannerCachePeriod,
} from './contributions';

const clearAllCookies = () => {
Expand Down Expand Up @@ -81,6 +85,9 @@ describe('getLastOneOffContributionDate', () => {

describe('isRecentOneOffContributor', () => {
beforeEach(clearAllCookies);
afterEach(() => {
MockDate.reset();
});

it('returns false if there is no one-off contribution cookie', () => {
expect(isRecentOneOffContributor()).toBe(false);
Expand All @@ -91,7 +98,8 @@ describe('isRecentOneOffContributor', () => {
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
});
global.Date.now = jest.fn(() => Date.parse('2018-08-07T10:50:34'));

MockDate.set(Date.parse('2018-08-07T10:50:34'));
expect(isRecentOneOffContributor()).toBe(true);
});

Expand All @@ -100,7 +108,7 @@ describe('isRecentOneOffContributor', () => {
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
});
global.Date.now = jest.fn(() => Date.parse('2018-08-01T13:00:30'));
MockDate.set(Date.parse('2018-08-01T13:00:30'));
expect(isRecentOneOffContributor()).toBe(true);
});

Expand All @@ -109,7 +117,7 @@ describe('isRecentOneOffContributor', () => {
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
});
global.Date.now = jest.fn(() => Date.parse('2019-08-01T13:00:30'));
MockDate.set(Date.parse('2019-08-01T13:00:30'));
expect(isRecentOneOffContributor()).toBe(false);
});
});
Expand Down Expand Up @@ -167,3 +175,35 @@ describe('recentlyClosedBanner', () => {
expect(recentlyClosedBanner(lastClosedAt, now)).toEqual(false);
});
});

describe('withinLocalNoBannerCachePeriod', () => {
beforeEach(() => {
storage.local.remove(NO_RR_BANNER_KEY);
});

it('returns true if not expired', () => {
setLocalNoBannerCachePeriod();
expect(withinLocalNoBannerCachePeriod()).toEqual(true);
});

it('returns false if expired', () => {
const past = new Date('2020-01-01').getTime();
setLocalNoBannerCachePeriod(past);
expect(withinLocalNoBannerCachePeriod()).toEqual(false);
});

it('returns false if no local storage item', () => {
expect(withinLocalNoBannerCachePeriod()).toEqual(false);
});

// The following 2 tests relate to a temporary issue with invalid expiry values - see https://github.com/guardian/csnx/pull/1099
it('returns true if expiry is number and not expired', () => {
storage.local.set(NO_RR_BANNER_KEY, true, Date.now() + 10000);
expect(withinLocalNoBannerCachePeriod()).toEqual(true);
});

it('returns false if expiry is number and expired', () => {
storage.local.set(NO_RR_BANNER_KEY, true, Date.now() - 10000);
expect(withinLocalNoBannerCachePeriod()).toEqual(false);
});
});
6 changes: 3 additions & 3 deletions dotcom-rendering/src/lib/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE =
// Local storage keys
const DAILY_ARTICLE_COUNT_KEY = 'gu.history.dailyArticleCount';
const WEEKLY_ARTICLE_COUNT_KEY = 'gu.history.weeklyArticleCount';
const NO_RR_BANNER_KEY = 'gu.noRRBanner';
export const NO_RR_BANNER_KEY = 'gu.noRRBanner';

// See https://github.com/guardian/support-dotcom-components/blob/main/module-versions.md
export const MODULES_VERSION = 'v3';
Expand Down Expand Up @@ -219,8 +219,8 @@ const twentyMins = 20 * 60000;
export const withinLocalNoBannerCachePeriod = (): boolean =>
!!storage.local.get(NO_RR_BANNER_KEY);

export const setLocalNoBannerCachePeriod = (): void =>
storage.local.set(NO_RR_BANNER_KEY, true, Date.now() + twentyMins);
export const setLocalNoBannerCachePeriod = (now: number = Date.now()): void =>
storage.local.set(NO_RR_BANNER_KEY, true, now + twentyMins);

// Returns true if banner was closed in the last hour
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
Expand Down
117 changes: 89 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f04d1b

Please sign in to comment.