Skip to content

Commit

Permalink
feat: ✨ add spaPageview support
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmannZ committed Feb 20, 2024
1 parent 5c2ea5a commit 6a95ff2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/helpers/get-zaraz.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type QueueItem = {
method: 'track' | 'set' | 'ecommerce';
method: 'track' | 'set' | 'ecommerce' | 'spaPageview';
arguments: unknown;
};

Expand Down Expand Up @@ -29,6 +29,9 @@ export function getZaraz() {
ecommerce: (...args: unknown[]) => {
queue.push({ method: 'ecommerce', arguments: args });
},
spaPageview: (...args: unknown[]) => {
queue.push({ method: 'spaPageview', arguments: args });

Check warning on line 33 in src/helpers/get-zaraz.ts

View check run for this annotation

Codecov / codecov/patch

src/helpers/get-zaraz.ts#L32-L33

Added lines #L32 - L33 were not covered by tests
},
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { ecommerce } from './ecommerce';
import { set } from './set';
import { spaPageview } from './spa-pageview';
import { track } from './track';

declare global {
Expand All @@ -15,9 +16,11 @@ export const zaraz = {
track,
set,
ecommerce,
spaPageview,
};

// Export typing etc.
export * from './ecommerce';
export * from './set';
export * from './spa-pageview';
export * from './track';
38 changes: 38 additions & 0 deletions src/spa-pageview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { spaPageview } from './spa-pageview';

const trackMock = jest.fn();
const setMock = jest.fn();
const ecommerceMock = jest.fn();
const spaPageviewMock = jest.fn();

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('set()', () => {
it('should call zaraz spaPageview when called', () => {
window.zaraz = {
track: trackMock,
set: setMock,
ecommerce: ecommerceMock,
spaPageview: spaPageviewMock,
};

spaPageview();

expect(spaPageviewMock).toHaveBeenCalledWith();
});
});
6 changes: 6 additions & 0 deletions src/spa-pageview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getZaraz } from './helpers/get-zaraz';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function spaPageview(): void {
getZaraz().spaPageview();
}

0 comments on commit 6a95ff2

Please sign in to comment.