Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add tests for FE date calculations, fix niggles with site context (#4471)

Artur Pata51fba1c2cf89
diff --git a/assets/js/dashboard/site-context.test.tsx b/assets/js/dashboard/site-context.test.tsx
new file mode 100644
index 0000000000..46e755e408
--- /dev/null
+++ b/assets/js/dashboard/site-context.test.tsx
@@ -0,0 +1,92 @@
+/** @format */
+import React, { HTMLAttributes } from 'react'
+import { render, screen } from '@testing-library/react'
+import { parseSiteFromDataset, PlausibleSite } from './site-context'
+
+describe('parseSiteFromDataset', () => {
+ const StatsReactContainer = ({
+ ...attrs
+ }: HTMLAttributes<HTMLDivElement>) => (
+ <div
+ data-testid="stats-react-container"
+ data-domain="dummy.site/nice"
+ data-offset="20700"
+ data-has-goals="true"
+ data-conversions-opted-out="false"
+ data-funnels-opted-out="false"
+ data-props-opted-out="false"
+ data-funnels-available="true"
+ data-props-available="true"
+ data-revenue-goals='[{"currency":"USD","event_name":"Purchase"}]'
+ data-funnels='[{"id":1,"name":"From homepage to login","steps_count":3}]'
+ data-has-props="true"
+ data-logged-in="true"
+ data-stats-begin="2021-09-07"
+ data-native-stats-begin="2022-09-02"
+ data-embedded=""
+ data-is-dbip="false"
+ data-current-user-role="owner"
+ data-flags="{}"
+ data-valid-intervals-by-period='{"12mo":["day","week","month"],"30d":["day","week"],"6mo":["day","week","month"],"7d":["hour","day"],"all":["week","month"],"custom":["day","week","month"],"day":["minute","hour"],"month":["day","week"],"realtime":["minute"],"year":["day","week","month"]}'
+ {...attrs}
+ />
+ )
+ const expectedParsedSite: PlausibleSite = {
+ domain: 'dummy.site/nice',
+ offset: 20700,
+ hasGoals: true,
+ conversionsOptedOut: false,
+ funnelsOptedOut: false,
+ propsOptedOut: false,
+ funnelsAvailable: true,
+ propsAvailable: true,
+ revenueGoals: [{ currency: 'USD', event_name: 'Purchase' }],
+ funnels: [{ id: 1, name: 'From homepage to login', steps_count: 3 }],
+ hasProps: true,
+ statsBegin: '2021-09-07',
+ nativeStatsBegin: '2022-09-02',
+ embedded: false,
+ background: undefined,
+ isDbip: false,
+ flags: {},
+ validIntervalsByPeriod: {
+ '12mo': ['day', 'week', 'month'],
+ '30d': ['day', 'week'],
+ '6mo': ['day', 'week', 'month'],
+ '7d': ['hour', 'day'],
+ all: ['week', 'month'],
+ custom: ['day', 'week', 'month'],
+ day: ['minute', 'hour'],
+ month: ['day', 'week'],
+ realtime: ['minute'],
+ year: ['day', 'week', 'month']
+ },
+ shared: false
+ }
+
+ it('parses from dom string map correctly', () => {
+ render(<StatsReactContainer />)
+ expect(
+ parseSiteFromDataset(screen.getByTestId('stats-react-container').dataset)
+ ).toEqual(expectedParsedSite)
+ })
+
+ it('handles embedded', () => {
+ render(
+ <StatsReactContainer
+ data-embedded="true"
+ data-logged-in="false"
+ data-shared-link-auth="JO1Zc2Tg5gGJzOD2uae41"
+ data-background="#foofoo"
+ />
+ )
+ expect(
+ parseSiteFromDataset(screen.getByTestId('stats-react-container').dataset)
+ ).toEqual({
+ ...expectedParsedSite,
+ shared: true,
+ embedded: true,
+ background: '#foofoo'
+ })
+ })
+})
diff --git a/assets/js/dashboard/site-context.tsx b/assets/js/dashboard/site-context.tsx
index 2618c04d41..1c89386533 100644
--- a/assets/js/dashboard/site-context.tsx
+++ b/assets/js/dashboard/site-context.tsx
@@ -4,7 +4,7 @@ import React, { createContext, ReactNode, useContext } from 'react'
export function parseSiteFromDataset(dataset: DOMStringMap): PlausibleSite {
return {
domain: dataset.domain!,
- offset: dataset.offset!,
+ offset: parseInt(dataset.offset!, 10),
hasGoals: dataset.hasGoals === 'true',
hasProps: dataset.hasProps === 'true',
funnelsAvailable: dataset.funnelsAvailable === 'true',
@@ -16,8 +16,8 @@ export function parseSiteFromDataset(dataset: DOMStringMap): PlausibleSite {
funnels: JSON.parse(dataset.funnels!),
statsBegin: dataset.statsBegin!,
nativeStatsBegin: dataset.nativeStatsBegin!,
- embedded: dataset.embedded!,
- background: dataset.background!,
+ embedded: dataset.embedded === 'true',
+ background: dataset.background,
isDbip: dataset.isDbip === 'true',
flags: JSON.parse(dataset.flags!),
validIntervalsByPeriod: JSON.parse(dataset.validIntervalsByPeriod!),
@@ -27,7 +27,8 @@ export function parseSiteFromDataset(dataset: DOMStringMap): PlausibleSite {
const siteContextDefaultValue = {
domain: '',
- offset: '0',
+ /** offset in seconds from UTC at site load time, @example 7200 */
+ offset: 0,
hasGoals: false,
hasProps: false,
funnelsAvailable: false,
@@ -37,10 +38,12 @@ const siteContextDefaultValue = {
propsOptedOut: false,
revenueGoals: [] as { event_name: string; currency: 'USD' }[],
funnels: [] as { id: number; name: string; steps_count: number }[],
+ /** date in YYYY-MM-DD, @example "2023-01-01" */
statsBegin: '',
+ /** date in YYYY-MM-DD, @example "2023-04-01" */
nativeStatsBegin: '',
- embedded: '',
- background: '',
+ embedded: false,
+ background: undefined as string | undefined,
isDbip: false,
flags: {},
validIntervalsByPeriod: {} as Record<string, Array<string>>,
diff --git a/assets/js/dashboard/util/date.test.ts b/assets/js/dashboard/util/date.test.ts
new file mode 100644
index 0000000000..98624162ec
--- /dev/null
+++ b/assets/js/dashboard/util/date.test.ts
@@ -0,0 +1,89 @@
+/** @format */
+
+import { formatISO, nowForSite, shiftMonths, yesterday } from './date'
+
+jest.useFakeTimers()
+
+/* prettier-ignore */
+const dstChangeOverDayEstonia = [
+// system time today yesterday today-2mo today+2mo today-12mo offset
+ [ '2024-03-30T21:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', 7200 ],
+ [ '2024-03-30T22:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-30T23:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T00:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T01:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 10800 ], // <-- all European TZs change to DST at 1AM UTC
+ [ '2024-03-31T02:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 10800 ],
+ [ '2024-03-31T03:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 10800 ],
+ [ '2024-03-31T04:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 10800 ],
+ // ...
+ [ '2024-03-31T20:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 10800 ],
+ [ '2024-03-31T21:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', 10800 ],
+ [ '2024-03-31T22:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', 10800 ],
+ [ '2024-03-31T23:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', 10800 ]
+]
+
+/* prettier-ignore */
+const dstChangeOverDayGermany = [
+// system time today yesterday today-2mo today+2mo today-12mo offset
+ [ '2024-03-30T21:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', 3600 ],
+ [ '2024-03-30T22:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', 3600 ],
+ [ '2024-03-30T23:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 3600 ],
+ [ '2024-03-31T00:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 3600 ],
+ [ '2024-03-31T01:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ], // <-- all European TZs change to DST at 1AM UTC
+ [ '2024-03-31T02:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T03:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T04:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ // ...
+ [ '2024-03-31T20:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T21:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', 7200 ],
+ [ '2024-03-31T22:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', 7200 ],
+ [ '2024-03-31T23:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', 7200 ]
+]
+
+/* prettier-ignore */
+const dstChangeOverDayEgypt = [
+// system time today yesterday today-2mo today+2mo today-12mo offset
+ [ '2024-10-31T20:00:00.000Z', '2024-10-31', '2024-10-30', '2024-08-31', '2024-12-31', '2023-10-31', 10800 ],
+ [ '2024-10-31T20:59:59.999Z', '2024-10-31', '2024-10-30', '2024-08-31', '2024-12-31', '2023-10-31', 10800 ], // <-- changeover from DST is on Nov 1st 00:00 local time
+ [ '2024-10-31T21:00:00.000Z', '2024-10-31', '2024-10-30', '2024-08-31', '2024-12-31', '2023-10-31', 7200 ],
+ [ '2024-10-31T21:59:59.999Z', '2024-10-31', '2024-10-30', '2024-08-31', '2024-12-31', '2023-10-31', 7200 ],
+ [ '2024-10-31T22:00:00.000Z', '2024-11-01', '2024-10-31', '2024-09-01', '2025-01-01', '2023-11-01', 7200 ],
+]
+
+const sets = [
+ ['Europe/Tallinn', dstChangeOverDayEstonia],
+ ['Europe/Berlin', dstChangeOverDayGermany],
+ ['Africa/Cairo', dstChangeOverDayEgypt]
+] as const
+
+for (const [timezone, suite] of sets) {
+ describe(`in timezone of ${timezone}, given the correct offset`, () => {
+ it.each(suite)(
+ 'at system time %s, today is %s and yesterday is %s',
+ (
+ utcTime,
+ expectedToday,
+ expectedYesterday,
+ expectedTwoMonthsBack,
+ expectedTwoMonthsAhead,
+ expectedOneYearBack,
+ offset
+ ) => {
+ jest.setSystemTime(new Date(utcTime))
+ expect({
+ today: formatISO(nowForSite({ offset })),
+ yesterday: formatISO(yesterday({ offset })),
+ twoMonthsBack: formatISO(shiftMonths(nowForSite({ offset }), -2)),
+ twoMonthsAhead: formatISO(shiftMonths(nowForSite({ offset }), 2)),
+ oneYearBack: formatISO(shiftMonths(nowForSite({ offset }), -12))
+ }).toEqual({
+ today: expectedToday,
+ yesterday: expectedYesterday,
+ twoMonthsBack: expectedTwoMonthsBack,
+ twoMonthsAhead: expectedTwoMonthsAhead,
+ oneYearBack: expectedOneYearBack
+ })
+ }
+ )
+ })
+}
diff --git a/assets/test-utils/app-context-providers.tsx b/assets/test-utils/app-context-providers.tsx
index a0d38d2bb6..5d26d3f2f5 100644
--- a/assets/test-utils/app-context-providers.tsx
+++ b/assets/test-utils/app-context-providers.tsx
@@ -23,7 +23,7 @@ export const TestContextProviders = ({
}: TestContextProvidersProps) => {
const defaultSite: PlausibleSite = {
domain: 'plausible.io/unit',
- offset: '0',
+ offset: 0,
hasGoals: false,
hasProps: false,
funnelsAvailable: false,
@@ -35,7 +35,7 @@ export const TestContextProviders = ({
funnels: [],
statsBegin: '',
nativeStatsBegin: '',
- embedded: '',
+ embedded: false,
background: '',
isDbip: false,
flags: {},