From 317679c9ade793d293d877a3bf6396ef2e105d6c Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Fri, 15 Dec 2023 20:48:12 -0500 Subject: [PATCH 1/6] [chore] updated github actions Signed-off-by: Giuseppe Macri --- .github/workflows/release.yml | 2 +- .github/workflows/test.yml | 4 ++-- .husky/pre-commit | 6 ++++++ .husky/pre-push | 5 +++++ package.json | 13 +------------ 5 files changed, 15 insertions(+), 15 deletions(-) create mode 100755 .husky/pre-commit create mode 100755 .husky/pre-push diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0637134..e5a1606 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,7 +3,7 @@ name: Version 🔖 on: push: branches: - - main + - master concurrency: ${{ github.workflow }}-${{ github.ref }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3b6b113..cc87cf5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,9 +5,9 @@ name: Node.js CI on: push: - branches: ['main'] + branches: ['master'] pull_request: - branches: ['main'] + branches: ['master'] jobs: build: runs-on: ubuntu-latest diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..e6ca61e --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,6 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +# yarn lint and added to git +yarn format:fix +git add . diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100755 index 0000000..ea4ace8 --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +# make sure to run yarn test before pushing +yarn test diff --git a/package.json b/package.json index 82830ff..21e5c4b 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ ], "scripts": { "dev": "vite", + "typecheck": "tsc --noEmit", "build": "tsc && vite build", "preview": "vite preview", "test": "vitest run", @@ -49,18 +50,6 @@ "vite-plugin-dts": "^3.6.4", "vitest": "^1.0.1" }, - "husky": { - "hooks": { - "pre-commit": "lint-staged", - "pre-push": "yarn test" - } - }, - "lint-staged": { - "*.{ts,json,md}": [ - "prettier --write", - "git add" - ] - }, "dependencies": { "@types/react-router": "^5.1.20", "md5": "^2.3.0", From a89cd97b7546fc45e6f5e3b1a1299c4bba3ec9e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Fri, 15 Dec 2023 22:41:14 -0500 Subject: [PATCH 2/6] fixed location tests Signed-off-by: Giuseppe Macri --- src/storage/location.test.ts | 61 +++++++++++++++++++++++++++++++----- src/storage/location.ts | 14 ++++----- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/storage/location.test.ts b/src/storage/location.test.ts index f69c035..9820663 100644 --- a/src/storage/location.test.ts +++ b/src/storage/location.test.ts @@ -11,15 +11,21 @@ import { setPageviewConfig, uaaValuesFromUrl, } from '../storage/location'; -import { describe, test, expect, beforeEach } from 'vitest'; +import { describe, test, expect, beforeEach, afterEach } from 'vitest'; import { DEFAULT_LOCATION } from '../storage/location'; const resetState = () => { - const location = getLocation(); - location.breadcrumbs = []; - location.initialUAAData = {}; - location.pagePath = ''; - location.prevPagePath = ''; + setLocation({ + breadcrumbs: [], + initialUAAData: {}, + pagePath: '', + prevPagePath: '', + pageviewConfig: { + isEnabled: false, + blacklistRegex: [], + }, + history: undefined, + }); }; describe('location', () => { @@ -35,6 +41,15 @@ describe('location', () => { }); }); + test('should return the same value when called multiple times', () => { + expect(getLocation()).toBe(getLocation()); + const location = getLocation(); + setLocation({ breadcrumbs: [{ label: 'test', href: 'test' }] }); + expect(location).toBe(getLocation()); + setLocation(DEFAULT_LOCATION); + expect(location).toBe(getLocation()); + }); + test('should init with right values', () => { const location = createLocation(); expect(location).toEqual({ @@ -71,6 +86,7 @@ describe('location', () => { test('should set breadcrumbs with custom values', () => { const location = getLocation(); + expect(location).toEqual(DEFAULT_LOCATION); setBreadcrumbs([ { label: 'test', @@ -105,6 +121,10 @@ describe('location', () => { }); describe('getDocumentReferrer()', () => { + beforeEach(() => { + resetState(); + }); + test('should return string when document.referrer is undefined', () => { expect(getDocumentReferrer()).toBe(''); }); @@ -122,6 +142,10 @@ describe('location', () => { }); describe('getReferrerData()', () => { + beforeEach(() => { + setLocation(DEFAULT_LOCATION); + }); + test('should return an empty object when document.referrer is an empty string', () => { expect(getReferrerData()).toEqual({}); }); @@ -146,6 +170,10 @@ describe('location', () => { }); describe('getUrlHostname()', () => { + beforeEach(() => { + resetState(); + }); + test('should return string when global.location is undefined', () => { expect(getUrlHostname()).toBe(''); }); @@ -160,6 +188,10 @@ describe('location', () => { }); describe('getUrlParams()', () => { + beforeEach(() => { + resetState(); + }); + test('should return empty string when global.location is undefined', () => { expect(getUrlParams()).toBe(''); }); @@ -173,6 +205,13 @@ describe('location', () => { }); describe('uaaValuesFromUrl', () => { + beforeEach(() => { + setLocation(DEFAULT_LOCATION); + }); + + afterEach(() => { + setLocation(DEFAULT_LOCATION); + }); test('returns UAA values', () => { Object.defineProperty(window, 'location', { value: { search: '?&utm_term=some-new-value' }, @@ -183,7 +222,10 @@ describe('location', () => { describe('pageviewConfig', () => { beforeEach(() => { - resetState(); + setLocation(DEFAULT_LOCATION); + }); + + afterEach(() => { setLocation(DEFAULT_LOCATION); }); @@ -214,7 +256,10 @@ describe('location', () => { }); }); - test.only('modifying pageViewConfig should not touch location properties', () => { + test('modifying pageViewConfig should not touch location properties', () => { + let location = getLocation(); + + expect(location).toEqual(DEFAULT_LOCATION); setLocation({ breadcrumbs: [{ label: 'test', href: 'test' }], initialUAAData: { utm_term: 'test' }, diff --git a/src/storage/location.ts b/src/storage/location.ts index ac441ff..942fab6 100644 --- a/src/storage/location.ts +++ b/src/storage/location.ts @@ -43,18 +43,18 @@ const UAA_QUERIES: AnalyticsQueries[] = [ ]; export function setBreadcrumbs(breadcrumbs: Breadcrumb[]) { - const location = getLocation(); - Object.assign(location, { breadcrumbs }); + setLocation({ breadcrumbs }); } export function setPageviewConfig(pageviewConfig: PageviewConfig) { - const location = getLocation(); - Object.assign(location, { pageviewConfig }); + setLocation({ pageviewConfig }); } export function setLocation(data: SetLocation) { const location = getLocation(); - Object.assign(location, data); + // console.log('setLocation before', location, data); + Object.assign(location, structuredClone(data)); + // console.log('setLocation after', location); } export const getUrlParams = (): string => { @@ -213,7 +213,5 @@ export const setLocationTracking = (options: LocationHistoryTracking) => { // TODO: we could pass the history object as an input export const createLocation = (): Location => { - return { - ...DEFAULT_LOCATION, - }; + return structuredClone(DEFAULT_LOCATION); }; From e0ac95a1aede410004536add037be43e92835239 Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Fri, 15 Dec 2023 22:47:34 -0500 Subject: [PATCH 3/6] added empty changeset Signed-off-by: Giuseppe Macri --- .changeset/sixty-pets-hope.md | 2 ++ package.json | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .changeset/sixty-pets-hope.md diff --git a/.changeset/sixty-pets-hope.md b/.changeset/sixty-pets-hope.md new file mode 100644 index 0000000..a845151 --- /dev/null +++ b/.changeset/sixty-pets-hope.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/package.json b/package.json index 21e5c4b..8d355e2 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "format:fix": "prettier . --write", "release:check": "changeset status --verbose --since=origin/master", "release:publish": "yarn install && yarn build && changeset publish", - "release:version": "changeset version && yarn install --lockfile-only" + "release:version": "changeset version && yarn install --lockfile-only", + "release:add": "changeset add" }, "devDependencies": { "@changesets/changelog-github": "^0.4.8", From 0e9b2cc3bfc78df42565aa76c7a616871bf77356 Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Fri, 15 Dec 2023 22:58:42 -0500 Subject: [PATCH 4/6] fixed metric payload Signed-off-by: Giuseppe Macri --- src/storage/networkLayer.test.ts | 20 ++------------------ src/storage/networkLayer.ts | 24 +++++++----------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/src/storage/networkLayer.test.ts b/src/storage/networkLayer.test.ts index 23ecba3..960a655 100644 --- a/src/storage/networkLayer.test.ts +++ b/src/storage/networkLayer.test.ts @@ -104,23 +104,7 @@ describe('networkLayer', () => { .mockImplementation(() => 'apiFetch'); }); - test('should call apiFetch when skipScheduler is set to true', async () => { - setConfig({ - platform: 'unknown', - projectName: 'testProjectName', - apiEndpoint: 'https://cca-lite.coinbase.com', - }); - sendMetrics(metrics, true); - expect(apiFetchSpy).toHaveBeenCalledTimes(1); - expect(configOnErrorSpy).toHaveBeenCalledTimes(0); - expect(apiFetchSpy).toHaveBeenCalledWith({ - url: 'https://cca-lite.coinbase.com/metrics', - data: { metricData: JSON.stringify(metrics) }, - onError: config.onError, - }); - }); - - test('should call apiFetch when skipScheduler is false', async () => { + test('should call apiFetch', async () => { setConfig({ platform: 'unknown', projectName: 'testProjectName', @@ -131,7 +115,7 @@ describe('networkLayer', () => { expect(configOnErrorSpy).toHaveBeenCalledTimes(0); expect(apiFetchSpy).toHaveBeenCalledWith({ url: 'https://cca-lite.coinbase.com/metrics', - data: { metricData: JSON.stringify(metrics) }, + data: { metrics: metrics }, onError: config.onError, }); }); diff --git a/src/storage/networkLayer.ts b/src/storage/networkLayer.ts index ebafd16..42b44b4 100644 --- a/src/storage/networkLayer.ts +++ b/src/storage/networkLayer.ts @@ -93,29 +93,19 @@ export const sendEvents = (events: Event[]) => { }); }; -export const sendMetrics = (metrics: Metric[], skipScheduler = false) => { +export const sendMetrics = (metrics: Metric[]) => { const { apiEndpoint, metricPath, onError } = getConfig(); const metricEndpoint = `${apiEndpoint}${metricPath}`; const data = { - metricData: JSON.stringify(metricNameEnhancer(metrics)), + metrics: metricNameEnhancer(metrics), }; - if (skipScheduler) { - apiFetch({ - url: metricEndpoint, - data, - onError: onError, - }); - } else { - scheduleEvent(() => { - apiFetch({ - url: metricEndpoint, - data, - onError: onError, - }); - }); - } + apiFetch({ + url: metricEndpoint, + data, + onError: onError, + }); }; // TODO: network layer should be generic as the scheduler From 2a5398877e06534771f6470e1bb738f2c54faeab Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Fri, 15 Dec 2023 23:01:52 -0500 Subject: [PATCH 5/6] added release info Signed-off-by: Giuseppe Macri --- .changeset/tidy-cobras-judge.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tidy-cobras-judge.md diff --git a/.changeset/tidy-cobras-judge.md b/.changeset/tidy-cobras-judge.md new file mode 100644 index 0000000..16a6daf --- /dev/null +++ b/.changeset/tidy-cobras-judge.md @@ -0,0 +1,5 @@ +--- +'client-analytics': patch +--- + +Fixed metric payload From 6820320805faf63a4a274d47ddc458903483222d Mon Sep 17 00:00:00 2001 From: Giuseppe Macri Date: Mon, 18 Dec 2023 09:30:23 -0500 Subject: [PATCH 6/6] updated release workflow Signed-off-by: Giuseppe Macri --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e5a1606..6e005bd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,9 +25,9 @@ jobs: - name: "Setup" uses: ./.github/actions/setup - - name: Build + - name: Install shell: bash - run: yarn build + run: yarn - name: Set deployment token run: npm config set '//registry.npmjs.org/:_authToken' "${{ secrets.NPM_TOKEN }}"