1- import { validatePageViewEvent , validateGaEvent , gaPageView , gaEvent } from "./analytics" ;
1+ import { validateGtag , validatePageViewEvent , validateGaEvent , gaPageView , gaEvent } from "./analytics" ;
22
3- const consoleSpy = jest . spyOn ( console , "error" ) ;
3+ const consoleError = jest . spyOn ( console , "error" ) ;
4+ const consoleWarn = jest . spyOn ( console , "warn" ) ;
45const GA_MEASUREMENT_ID = "G-123456789" ;
56const mockGaEvent = {
67 action : "TEST_ACTION" ,
@@ -12,18 +13,28 @@ const mockGaEvent = {
1213beforeEach ( ( ) => {
1314 // eslint-disable-next-line @typescript-eslint/no-empty-function
1415 jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
16+ // eslint-disable-next-line @typescript-eslint/no-empty-function
17+ jest . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
1518} ) ;
1619
1720afterEach ( ( ) => {
1821 jest . clearAllMocks ( ) ;
1922} ) ;
2023
24+ describe ( "validateGtag" , ( ) => {
25+ it ( "warns if gtag is not initialised" , ( ) => {
26+ validateGtag ( ) ;
27+ expect ( consoleWarn ) . toBeCalledTimes ( 1 ) ;
28+ expect ( consoleWarn ) . toHaveBeenCalledWith ( "gtag is not initialised" ) ;
29+ } ) ;
30+ } ) ;
31+
2132describe ( "validateGaPageView" , ( ) => {
22- it ( "throws if action is missing" , ( ) => {
33+ it ( "errors if action is missing" , ( ) => {
2334 // @ts -expect-error we expect this error to be thrown
2435 validatePageViewEvent ( { } ) ;
25- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
26- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Action is required" ) ;
36+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
37+ expect ( consoleError ) . toHaveBeenCalledWith ( "Action is required" ) ;
2738 } ) ;
2839} ) ;
2940
@@ -50,39 +61,39 @@ describe("gaPageView", () => {
5061 } ) ;
5162 } ) ;
5263
53- it ( "throws if there is a validation error" , ( ) => {
64+ it ( "errors if there is a validation error" , ( ) => {
5465 const mockGaEventError = { action : 123 } ;
5566 // @ts -expect-error the mock does not match the signature
5667 gaPageView ( mockGaEventError ) ;
57- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
58- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Action must be a string" ) ;
68+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
69+ expect ( consoleError ) . toHaveBeenCalledWith ( "Action must be a string" ) ;
5970 } ) ;
6071} ) ;
6172
6273describe ( "validateGaEvent" , ( ) => {
63- it ( "throws if category is missing" , ( ) => {
74+ it ( "errors if category is missing" , ( ) => {
6475 // @ts -expect-error we expect this error to be thrown
6576 validateGaEvent ( {
6677 action : "foobar_start" ,
6778 } ) ;
68- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
69- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Category is required" ) ;
79+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
80+ expect ( consoleError ) . toHaveBeenCalledWith ( "Category is required" ) ;
7081 } ) ;
7182
72- it ( "throws if action is missing" , ( ) => {
83+ it ( "errors if action is missing" , ( ) => {
7384 // @ts -expect-error we expect this error to be thrown
7485 validateGaEvent ( {
7586 category : "foobar" ,
7687 } ) ;
77- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
78- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Action is required" ) ;
88+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
89+ expect ( consoleError ) . toHaveBeenCalledWith ( "Action is required" ) ;
7990 } ) ;
8091
81- it ( "throws if value is not number" , ( ) => {
92+ it ( "errors if value is not number" , ( ) => {
8293 // @ts -expect-error we expect this error to be thrown
8394 validateGaEvent ( { category : "foobar" , action : "foobar_start" , value : "STRING" } ) ;
84- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
85- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Value must be a number" ) ;
95+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
96+ expect ( consoleError ) . toHaveBeenCalledWith ( "Value must be a number" ) ;
8697 } ) ;
8798
8899 it ( "passes for minimum values" , ( ) => {
@@ -135,7 +146,7 @@ describe("gaEvent", () => {
135146 const mockGaEventError = { ...mockGaEvent , value : "STRING" } ;
136147 // @ts -expect-error the mock does not match the signature
137148 gaEvent ( mockGaEventError ) ;
138- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
139- expect ( consoleSpy ) . toHaveBeenCalledWith ( "Value must be a number" ) ;
149+ expect ( consoleError ) . toBeCalledTimes ( 1 ) ;
150+ expect ( consoleError ) . toHaveBeenCalledWith ( "Value must be a number" ) ;
140151 } ) ;
141152} ) ;
0 commit comments