Skip to content

Commit 7e20b6e

Browse files
committed
test: add unit tests for getGCLID function to validate query parameter handling
1 parent 88cc948 commit 7e20b6e

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

test/unit/utm.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { describe, it, expect } from 'vitest'
2+
import type { LocationQuery } from 'vue-router'
3+
import { getGCLID } from '../../src/runtime/utm'
4+
import type { GCLIDParams } from 'nuxt-utm'
5+
6+
describe('getGCLID', () => {
7+
it('should return GCLID and gad_source when both are present in query', () => {
8+
const query: LocationQuery = {
9+
gclid: 'testGclid123',
10+
gad_source: 'testGadSourceABC',
11+
}
12+
const expected: GCLIDParams = {
13+
gclid: 'testGclid123',
14+
gad_source: 'testGadSourceABC',
15+
}
16+
expect(getGCLID(query)).toEqual(expected)
17+
})
18+
19+
it('should return only gclid when gad_source is not present', () => {
20+
const query: LocationQuery = {
21+
gclid: 'testGclid123',
22+
}
23+
const expected: GCLIDParams = {
24+
gclid: 'testGclid123',
25+
gad_source: undefined,
26+
}
27+
expect(getGCLID(query)).toEqual(expected)
28+
})
29+
30+
it('should return only gad_source when gclid is not present', () => {
31+
const query: LocationQuery = {
32+
gad_source: 'testGadSourceABC',
33+
}
34+
const expected: GCLIDParams = {
35+
gclid: undefined,
36+
gad_source: 'testGadSourceABC',
37+
}
38+
expect(getGCLID(query)).toEqual(expected)
39+
})
40+
41+
it('should return undefined for both when neither gclid nor gad_source is present', () => {
42+
const query: LocationQuery = {}
43+
const expected: GCLIDParams = {
44+
gclid: undefined,
45+
gad_source: undefined,
46+
}
47+
expect(getGCLID(query)).toEqual(expected)
48+
})
49+
50+
it('should return empty strings if query parameters are empty strings', () => {
51+
const query: LocationQuery = {
52+
gclid: '',
53+
gad_source: '',
54+
}
55+
const expected: GCLIDParams = {
56+
gclid: '',
57+
gad_source: '',
58+
}
59+
expect(getGCLID(query)).toEqual(expected)
60+
})
61+
62+
it('should handle gclid present and gad_source as empty string', () => {
63+
const query: LocationQuery = {
64+
gclid: 'testGclid123',
65+
gad_source: '',
66+
}
67+
const expected: GCLIDParams = {
68+
gclid: 'testGclid123',
69+
gad_source: '',
70+
}
71+
expect(getGCLID(query)).toEqual(expected)
72+
})
73+
74+
it('should handle gad_source present and gclid as empty string', () => {
75+
const query: LocationQuery = {
76+
gclid: '',
77+
gad_source: 'testGadSourceABC',
78+
}
79+
const expected: GCLIDParams = {
80+
gclid: '',
81+
gad_source: 'testGadSourceABC',
82+
}
83+
expect(getGCLID(query)).toEqual(expected)
84+
})
85+
86+
it('should handle query parameters with special characters', () => {
87+
const query: LocationQuery = {
88+
gclid: 'gclid-123_abc!@#',
89+
gad_source: 'gad_source-XYZ*%^',
90+
}
91+
const expected: GCLIDParams = {
92+
gclid: 'gclid-123_abc!@#',
93+
gad_source: 'gad_source-XYZ*%^',
94+
}
95+
expect(getGCLID(query)).toEqual(expected)
96+
})
97+
98+
it('should handle query parameters that are numbers (as strings)', () => {
99+
const query: LocationQuery = {
100+
gclid: '12345',
101+
gad_source: '67890',
102+
}
103+
const expected: GCLIDParams = {
104+
gclid: '12345',
105+
gad_source: '67890',
106+
}
107+
expect(getGCLID(query)).toEqual(expected)
108+
})
109+
110+
it('should handle null values for query parameters', () => {
111+
const query: LocationQuery = {
112+
gclid: null,
113+
gad_source: null,
114+
}
115+
const expected: GCLIDParams = {
116+
gclid: undefined,
117+
gad_source: undefined,
118+
}
119+
expect(getGCLID(query)).toEqual(expected)
120+
})
121+
122+
it('should handle one param as null and other present', () => {
123+
const query: LocationQuery = {
124+
gclid: 'testGclid123',
125+
gad_source: null,
126+
}
127+
const expected: GCLIDParams = {
128+
gclid: 'testGclid123',
129+
gad_source: undefined,
130+
}
131+
expect(getGCLID(query)).toEqual(expected)
132+
})
133+
134+
it('should convert array query parameters to comma-separated strings', () => {
135+
const query: LocationQuery = {
136+
gclid: ['testGclid123', 'anotherGclid'],
137+
gad_source: ['testGadSourceABC', 'anotherGadSource'],
138+
}
139+
const expected: GCLIDParams = {
140+
gclid: 'testGclid123,anotherGclid',
141+
gad_source: 'testGadSourceABC,anotherGadSource',
142+
}
143+
expect(getGCLID(query)).toEqual(expected)
144+
})
145+
146+
it('should handle mixed types of query parameters (string and array)', () => {
147+
const query: LocationQuery = {
148+
gclid: 'testGclid123',
149+
gad_source: ['testGadSourceABC', 'anotherGadSource'],
150+
}
151+
const expected: GCLIDParams = {
152+
gclid: 'testGclid123',
153+
gad_source: 'testGadSourceABC,anotherGadSource',
154+
}
155+
expect(getGCLID(query)).toEqual(expected)
156+
})
157+
})

0 commit comments

Comments
 (0)