Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
52940961
Commit
52940961
authored
Oct 18, 2021
by
Axel García
Committed by
Savas Vedova
Oct 18, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split tests for tracking related frontend files
parent
8a68dab4
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
299 additions
and
147 deletions
+299
-147
spec/frontend/tracking/tracking_initialization_spec.js
spec/frontend/tracking/tracking_initialization_spec.js
+140
-0
spec/frontend/tracking/tracking_spec.js
spec/frontend/tracking/tracking_spec.js
+60
-147
spec/frontend/tracking/utils_spec.js
spec/frontend/tracking/utils_spec.js
+99
-0
No files found.
spec/frontend/tracking/tracking_initialization_spec.js
0 → 100644
View file @
52940961
import
{
TRACKING_CONTEXT_SCHEMA
}
from
'
~/experimentation/constants
'
;
import
{
getExperimentData
,
getAllExperimentContexts
}
from
'
~/experimentation/utils
'
;
import
Tracking
,
{
initUserTracking
,
initDefaultTrackers
}
from
'
~/tracking
'
;
import
getStandardContext
from
'
~/tracking/get_standard_context
'
;
jest
.
mock
(
'
~/experimentation/utils
'
,
()
=>
({
getExperimentData
:
jest
.
fn
(),
getAllExperimentContexts
:
jest
.
fn
(),
}));
describe
(
'
Tracking
'
,
()
=>
{
let
standardContext
;
let
snowplowSpy
;
let
bindDocumentSpy
;
let
trackLoadEventsSpy
;
let
enableFormTracking
;
let
setAnonymousUrlsSpy
;
beforeAll
(()
=>
{
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
snowplowStandardContext
=
{
schema
:
'
iglu:com.gitlab/gitlab_standard
'
,
data
:
{
environment
:
'
testing
'
,
source
:
'
unknown
'
,
extra
:
{},
},
};
standardContext
=
getStandardContext
();
});
beforeEach
(()
=>
{
getExperimentData
.
mockReturnValue
(
undefined
);
getAllExperimentContexts
.
mockReturnValue
([]);
window
.
snowplow
=
window
.
snowplow
||
(()
=>
{});
window
.
snowplowOptions
=
{
namespace
:
'
gl_test
'
,
hostname
:
'
app.test.com
'
,
cookieDomain
:
'
.test.com
'
,
};
snowplowSpy
=
jest
.
spyOn
(
window
,
'
snowplow
'
);
});
describe
(
'
initUserTracking
'
,
()
=>
{
it
(
'
calls through to get a new tracker with the expected options
'
,
()
=>
{
initUserTracking
();
expect
(
snowplowSpy
).
toHaveBeenCalledWith
(
'
newTracker
'
,
'
gl_test
'
,
'
app.test.com
'
,
{
namespace
:
'
gl_test
'
,
hostname
:
'
app.test.com
'
,
cookieDomain
:
'
.test.com
'
,
appId
:
''
,
userFingerprint
:
false
,
respectDoNotTrack
:
true
,
forceSecureTracker
:
true
,
eventMethod
:
'
post
'
,
contexts
:
{
webPage
:
true
,
performanceTiming
:
true
},
formTracking
:
false
,
linkClickTracking
:
false
,
pageUnloadTimer
:
10
,
formTrackingConfig
:
{
fields
:
{
allow
:
[]
},
forms
:
{
allow
:
[]
},
},
});
});
});
describe
(
'
initDefaultTrackers
'
,
()
=>
{
beforeEach
(()
=>
{
bindDocumentSpy
=
jest
.
spyOn
(
Tracking
,
'
bindDocument
'
).
mockImplementation
(()
=>
null
);
trackLoadEventsSpy
=
jest
.
spyOn
(
Tracking
,
'
trackLoadEvents
'
).
mockImplementation
(()
=>
null
);
enableFormTracking
=
jest
.
spyOn
(
Tracking
,
'
enableFormTracking
'
)
.
mockImplementation
(()
=>
null
);
setAnonymousUrlsSpy
=
jest
.
spyOn
(
Tracking
,
'
setAnonymousUrls
'
).
mockImplementation
(()
=>
null
);
});
it
(
'
should activate features based on what has been enabled
'
,
()
=>
{
initDefaultTrackers
();
expect
(
snowplowSpy
).
toHaveBeenCalledWith
(
'
enableActivityTracking
'
,
30
,
30
);
expect
(
snowplowSpy
).
toHaveBeenCalledWith
(
'
trackPageView
'
,
null
,
[
standardContext
]);
expect
(
snowplowSpy
).
not
.
toHaveBeenCalledWith
(
'
enableFormTracking
'
);
expect
(
snowplowSpy
).
not
.
toHaveBeenCalledWith
(
'
enableLinkClickTracking
'
);
window
.
snowplowOptions
=
{
...
window
.
snowplowOptions
,
formTracking
:
true
,
linkClickTracking
:
true
,
formTrackingConfig
:
{
forms
:
{
whitelist
:
[
'
foo
'
]
},
fields
:
{
whitelist
:
[
'
bar
'
]
}
},
};
initDefaultTrackers
();
expect
(
enableFormTracking
).
toHaveBeenCalledWith
(
window
.
snowplowOptions
.
formTrackingConfig
);
expect
(
snowplowSpy
).
toHaveBeenCalledWith
(
'
enableLinkClickTracking
'
);
});
it
(
'
binds the document event handling
'
,
()
=>
{
initDefaultTrackers
();
expect
(
bindDocumentSpy
).
toHaveBeenCalled
();
});
it
(
'
tracks page loaded events
'
,
()
=>
{
initDefaultTrackers
();
expect
(
trackLoadEventsSpy
).
toHaveBeenCalled
();
});
it
(
'
calls the anonymized URLs method
'
,
()
=>
{
initDefaultTrackers
();
expect
(
setAnonymousUrlsSpy
).
toHaveBeenCalled
();
});
describe
(
'
when there are experiment contexts
'
,
()
=>
{
const
experimentContexts
=
[
{
schema
:
TRACKING_CONTEXT_SCHEMA
,
data
:
{
experiment
:
'
experiment1
'
,
variant
:
'
control
'
},
},
{
schema
:
TRACKING_CONTEXT_SCHEMA
,
data
:
{
experiment
:
'
experiment_two
'
,
variant
:
'
candidate
'
},
},
];
beforeEach
(()
=>
{
getAllExperimentContexts
.
mockReturnValue
(
experimentContexts
);
});
it
(
'
includes those contexts alongside the standard context
'
,
()
=>
{
initDefaultTrackers
();
expect
(
snowplowSpy
).
toHaveBeenCalledWith
(
'
trackPageView
'
,
null
,
[
standardContext
,
...
experimentContexts
,
]);
});
});
});
});
spec/frontend/tracking_spec.js
→
spec/frontend/tracking
/tracking
_spec.js
View file @
52940961
This diff is collapsed.
Click to expand it.
spec/frontend/tracking/utils_spec.js
0 → 100644
View file @
52940961
import
{
renameKey
,
getReferrersCache
,
addExperimentContext
,
addReferrersCacheEntry
,
filterOldReferrersCacheEntries
,
}
from
'
~/tracking/utils
'
;
import
{
TRACKING_CONTEXT_SCHEMA
}
from
'
~/experimentation/constants
'
;
import
{
REFERRER_TTL
,
URLS_CACHE_STORAGE_KEY
}
from
'
~/tracking/constants
'
;
import
{
TEST_HOST
}
from
'
helpers/test_constants
'
;
jest
.
mock
(
'
~/experimentation/utils
'
,
()
=>
({
getExperimentData
:
jest
.
fn
().
mockReturnValue
({}),
}));
describe
(
'
~/tracking/utils
'
,
()
=>
{
beforeEach
(()
=>
{
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
snowplowStandardContext
=
{};
});
describe
(
'
addExperimentContext
'
,
()
=>
{
const
options
=
{
category
:
'
root:index
'
,
action
:
'
generic
'
,
};
it
(
'
returns same options if no experiment is provided
'
,
()
=>
{
expect
(
addExperimentContext
({
options
})).
toStrictEqual
({
options
});
});
it
(
'
adds experiment if provided
'
,
()
=>
{
const
experiment
=
'
TEST_EXPERIMENT_NAME
'
;
expect
(
addExperimentContext
({
experiment
,
...
options
})).
toStrictEqual
({
...
options
,
context
:
{
data
:
{},
schema
:
TRACKING_CONTEXT_SCHEMA
},
});
});
});
describe
(
'
renameKey
'
,
()
=>
{
it
(
'
renames a given key
'
,
()
=>
{
expect
(
renameKey
({
allow
:
[]
},
'
allow
'
,
'
permit
'
)).
toStrictEqual
({
permit
:
[]
});
});
});
describe
(
'
referrers cache
'
,
()
=>
{
describe
(
'
filterOldReferrersCacheEntries
'
,
()
=>
{
it
(
'
removes entries with old or no timestamp
'
,
()
=>
{
const
now
=
Date
.
now
();
const
cache
=
[{
timestamp
:
now
},
{
timestamp
:
now
-
REFERRER_TTL
},
{
referrer
:
''
}];
expect
(
filterOldReferrersCacheEntries
(
cache
)).
toStrictEqual
([{
timestamp
:
now
}]);
});
});
describe
(
'
getReferrersCache
'
,
()
=>
{
beforeEach
(()
=>
{
localStorage
.
removeItem
(
URLS_CACHE_STORAGE_KEY
);
});
it
(
'
returns an empty array if cache is not found
'
,
()
=>
{
expect
(
getReferrersCache
()).
toHaveLength
(
0
);
});
it
(
'
returns an empty array if cache is invalid
'
,
()
=>
{
localStorage
.
setItem
(
URLS_CACHE_STORAGE_KEY
,
'
Invalid JSON
'
);
expect
(
getReferrersCache
()).
toHaveLength
(
0
);
});
it
(
'
returns parsed entries if valid
'
,
()
=>
{
localStorage
.
setItem
(
URLS_CACHE_STORAGE_KEY
,
JSON
.
stringify
([{
referrer
:
''
,
timestamp
:
Date
.
now
()
}]),
);
expect
(
getReferrersCache
()).
toHaveLength
(
1
);
});
});
describe
(
'
addReferrersCacheEntry
'
,
()
=>
{
it
(
'
unshifts entry and adds timestamp
'
,
()
=>
{
const
now
=
Date
.
now
();
addReferrersCacheEntry
([{
referrer
:
''
,
originalUrl
:
TEST_HOST
,
timestamp
:
now
}],
{
referrer
:
TEST_HOST
,
});
const
cache
=
getReferrersCache
();
expect
(
cache
).
toHaveLength
(
2
);
expect
(
cache
[
0
].
referrer
).
toBe
(
TEST_HOST
);
expect
(
cache
[
0
].
timestamp
).
toBeDefined
();
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment