@@ -7,19 +7,19 @@ describe('Utility Functions', () => {
7
7
describe ( 'getLibraryVersion()' , ( ) => {
8
8
it ( 'should return a valid semver version string' , ( ) => {
9
9
const version = getLibraryVersion ( ) ;
10
-
10
+
11
11
expect ( version ) . toBeDefined ( ) ;
12
12
expect ( typeof version ) . toBe ( 'string' ) ;
13
13
expect ( version . length ) . toBeGreaterThan ( 0 ) ;
14
-
14
+
15
15
// Check if it matches semver pattern (major.minor.patch)
16
16
const semverPattern = / ^ \d + \. \d + \. \d + (?: - [ a - z A - Z 0 - 9 . - ] + ) ? $ / ;
17
17
expect ( version ) . toMatch ( semverPattern ) ;
18
18
} ) ;
19
19
20
20
it ( 'should return the version from package.json' , ( ) => {
21
21
const version = getLibraryVersion ( ) ;
22
-
22
+
23
23
// The version should match whatever is in package.json
24
24
// We don't hardcode the expected version to avoid breaking on version updates
25
25
expect ( version ) . toMatch ( / ^ \d + \. \d + \. \d + (?: - [ a - z A - Z 0 - 9 . - ] + ) ? $ / ) ;
@@ -28,47 +28,67 @@ describe('Utility Functions', () => {
28
28
} ) ;
29
29
30
30
describe ( 'getUserAgent()' , ( ) => {
31
+ const originalEnv = process . env ;
32
+
33
+ beforeEach ( ( ) => {
34
+ jest . resetModules ( ) ; // Important to clear cached modules
35
+ process . env = { ...originalEnv } ;
36
+ } ) ;
37
+
38
+ afterEach ( ( ) => {
39
+ process . env = originalEnv ; // Restore original environment
40
+ } ) ;
41
+
31
42
it ( 'should return a properly formatted User-Agent string' , ( ) => {
32
43
const userAgent = getUserAgent ( ) ;
33
-
44
+
34
45
expect ( userAgent ) . toBeDefined ( ) ;
35
46
expect ( typeof userAgent ) . toBe ( 'string' ) ;
36
47
expect ( userAgent . length ) . toBeGreaterThan ( 0 ) ;
37
48
} ) ;
38
49
39
50
it ( 'should follow the expected User-Agent format' , ( ) => {
40
51
const userAgent = getUserAgent ( ) ;
41
-
52
+
42
53
// Should match: nutrient-dws-client-typescript/VERSION
43
54
const expectedPattern = / ^ n u t r i e n t - d w s - c l i e n t - t y p e s c r i p t \/ \d + \. \d + \. \d + (?: - [ a - z A - Z 0 - 9 . - ] + ) ? $ / ;
44
55
expect ( userAgent ) . toMatch ( expectedPattern ) ;
45
56
} ) ;
46
57
58
+ it ( 'should follow the expected User-Agent format when in development' , ( ) => {
59
+ process . env = { ...originalEnv , NODE_ENV : 'development' } ;
60
+ const userAgent = getUserAgent ( ) ;
61
+
62
+ // Should match: nutrient-dws-client-typescript/VERSION-dev
63
+ const expectedPattern = / ^ n u t r i e n t - d w s - c l i e n t - t y p e s c r i p t \/ 0 \. 0 \. 0 - d e v $ / ;
64
+ expect ( userAgent ) . toMatch ( expectedPattern ) ;
65
+ } ) ;
66
+
47
67
it ( 'should include the correct library name' , ( ) => {
48
68
const userAgent = getUserAgent ( ) ;
49
-
69
+
50
70
expect ( userAgent ) . toContain ( 'nutrient-dws-client-typescript' ) ;
51
71
} ) ;
52
72
53
73
it ( 'should include the current library version' , ( ) => {
54
74
const userAgent = getUserAgent ( ) ;
55
75
const version = getLibraryVersion ( ) ;
56
-
76
+
57
77
expect ( userAgent ) . toContain ( version ) ;
58
78
} ) ;
59
79
60
80
it ( 'should have consistent format across multiple calls' , ( ) => {
61
81
const userAgent1 = getUserAgent ( ) ;
62
82
const userAgent2 = getUserAgent ( ) ;
63
-
83
+
64
84
expect ( userAgent1 ) . toBe ( userAgent2 ) ;
65
85
} ) ;
66
86
67
87
it ( 'should return the expected User-Agent format with current version' , ( ) => {
68
88
const userAgent = getUserAgent ( ) ;
69
89
const version = getLibraryVersion ( ) ;
70
-
90
+
71
91
expect ( userAgent ) . toBe ( `nutrient-dws-client-typescript/${ version } ` ) ;
72
92
} ) ;
73
93
} ) ;
74
- } ) ;
94
+ } ) ;
0 commit comments