|
1 | | -import { generateCombinedBundle, CONTEXT_RESOLUTION_HELPERS } from '@datadog/browser-sdk-endpoint' |
| 1 | +import { generateCombinedBundle } from '@datadog/browser-sdk-endpoint' |
2 | 2 | import { test, expect } from '@playwright/test' |
3 | | -import { createTest, DEFAULT_RUM_CONFIGURATION, html, basePage, createCrossOriginScriptUrls } from '../../lib/framework' |
4 | | -import type { SetupOptions, Servers } from '../../lib/framework' |
5 | | - |
6 | | -export interface ContextItem { |
7 | | - key: string |
8 | | - value: { rcSerializedType: string; value?: string; strategy?: string; name?: string; path?: string; selector?: string; attribute?: string; extractor?: unknown } |
9 | | -} |
10 | | - |
11 | | -/** |
12 | | - * Creates a SetupFactory that generates an HTML page mimicking the embedded config bundle |
13 | | - * produced by generateCombinedBundle(), with context resolution for user and globalContext. |
14 | | - * Uses CONTEXT_RESOLUTION_HELPERS verbatim so tests exercise the same code as production. |
15 | | - * |
16 | | - * @param extraConfig - optional user[] / context[] arrays and optional pre-SDK script |
17 | | - * @param extraConfig.user - optional user context items |
18 | | - * @param extraConfig.context - optional global context items |
19 | | - * @param extraConfig.preSDKScript - extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie) |
20 | | - */ |
21 | | -export function createEmbeddedConfigSetup(extraConfig: { |
22 | | - user?: ContextItem[] |
23 | | - context?: ContextItem[] |
24 | | - /** Extra inline JS injected before the SDK script tag (e.g. to pre-set a cookie). */ |
25 | | - preSDKScript?: string |
26 | | -}) { |
27 | | - return (options: SetupOptions, servers: Servers): string => { |
28 | | - const { rumScriptUrl } = createCrossOriginScriptUrls(servers, options) |
29 | | - |
30 | | - const embeddedConfig = { |
31 | | - ...DEFAULT_RUM_CONFIGURATION, |
32 | | - sessionSampleRate: 100, |
33 | | - proxy: servers.intake.origin, |
34 | | - ...extraConfig, |
35 | | - } |
36 | | - const configJson = JSON.stringify(embeddedConfig) |
37 | | - const testContextJson = JSON.stringify(options.context) |
38 | | - |
39 | | - const header = html` |
40 | | - ${extraConfig.preSDKScript ? `<script type="text/javascript">${extraConfig.preSDKScript}</script>` : ''} |
41 | | - <script type="text/javascript" src="${rumScriptUrl}"></script> |
42 | | - <script type="text/javascript"> |
43 | | - (function () { |
44 | | - 'use strict'; |
45 | | - var __DATADOG_REMOTE_CONFIG__ = ${configJson}; |
46 | | - var __dd_user = {}; |
47 | | - (__DATADOG_REMOTE_CONFIG__.user || []).forEach(function (item) { |
48 | | - __dd_user[item.key] = __dd_resolveContextValue(item.value); |
49 | | - }); |
50 | | - var __dd_globalContext = {}; |
51 | | - (__DATADOG_REMOTE_CONFIG__.context || []).forEach(function (item) { |
52 | | - __dd_globalContext[item.key] = __dd_resolveContextValue(item.value); |
53 | | - }); |
54 | | - var hasUser = Object.keys(__dd_user).length > 0; |
55 | | - var hasGlobalContext = Object.keys(__dd_globalContext).length > 0; |
56 | | - window.DD_RUM.init(Object.assign({}, __DATADOG_REMOTE_CONFIG__, { |
57 | | - user: hasUser ? __dd_user : undefined, |
58 | | - context: undefined, |
59 | | - globalContext: hasGlobalContext ? __dd_globalContext : undefined |
60 | | - })); |
61 | | - // Re-apply test isolation context after init so it is not overwritten by the |
62 | | - // globalContext init option (setContext replaces; we add test properties back). |
63 | | - var __dd_testContext = ${testContextJson}; |
64 | | - Object.keys(__dd_testContext).forEach(function(key) { |
65 | | - window.DD_RUM.setGlobalContextProperty(key, __dd_testContext[key]); |
66 | | - }); |
67 | | -
|
68 | | - ${CONTEXT_RESOLUTION_HELPERS.trim()} |
69 | | - })(); |
70 | | - </script> |
71 | | - ` |
72 | | - return basePage({ header }) |
73 | | - } |
74 | | -} |
| 3 | +import { createTest } from '../../lib/framework' |
| 4 | +import { createEmbeddedConfigSetup } from '../../lib/helpers/embeddedConfigSetup' |
75 | 5 |
|
76 | 6 | test.describe('embedded configuration', () => { |
77 | 7 | createTest('should load SDK with embedded config and expose getInitConfiguration') |
|
0 commit comments