Skip to content

Commit

Permalink
[TEST] add common helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Oct 18, 2023
1 parent b73b7e2 commit 989af04
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 166 deletions.
23 changes: 23 additions & 0 deletions common/test-utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
BrowserTracker,
SharedState,
TrackerConfiguration,
addTracker as addTrackerReal,
} from '../../libraries/browser-tracker-core';

const DEFAULT_CONFIG: Partial<TrackerConfiguration> = { connectionTimeout: 0 };
export function createTracker(configuration?: TrackerConfiguration) {
let id = 'sp-' + Math.random();
return addTracker(id, id, '', '', new SharedState(), { ...DEFAULT_CONFIG, ...configuration });
}

export function addTracker(
trackerId: string,
namespace: string,
version: string,
endpoint: string,
sharedState: SharedState,
configuration?: TrackerConfiguration
): BrowserTracker | null {
return addTrackerReal(trackerId, namespace, version, endpoint, sharedState, { ...DEFAULT_CONFIG, ...configuration });
}
38 changes: 0 additions & 38 deletions libraries/browser-tracker-core/test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
/*
* Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { TrackerConfiguration, addTracker, SharedState } from '../../src';

// Default Domain alias is snowplow-js-tracker.local & configCookiepath is /
const DEFAULT_DOMAIN_HASH = '7f01';

Expand Down Expand Up @@ -74,9 +42,3 @@ export function createTestSessionIdCookie(params?: CreateTestSessionIdCookie) {
const domainHash = DEFAULT_DOMAIN_HASH || params?.domainHash;
return `_sp_ses.${domainHash}=*; Expires=; Path=/; SameSite=None; Secure;`;
}

const DEFAULT_CONFIG: Partial<TrackerConfiguration> = { connectionTimeout: 0 };
export function createTracker(configuration?: TrackerConfiguration) {
let id = 'sp-' + Math.random();
return addTracker(id, id, '', '', new SharedState(), { ...DEFAULT_CONFIG, ...configuration });
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ jest.mock('uuid');
const MOCK_UUID = '123456789';
jest.spyOn(uuid, 'v4').mockReturnValue(MOCK_UUID);

import { createTestIdCookie, createTestSessionIdCookie, createTracker as baseCreateTracker } from '../helpers';
import { TrackerConfiguration } from '../../src/tracker/types';

const createTracker = (config?: TrackerConfiguration) => baseCreateTracker({ ...config, connectionTimeout: 1 });
import { createTestIdCookie, createTestSessionIdCookie } from '../helpers';
import { createTracker } from '../../../../common/test-utils/utils';

jest.useFakeTimers('modern');

Expand Down
32 changes: 2 additions & 30 deletions plugins/browser-plugin-consent/test/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
/*
* Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';

import { addTracker, SharedState } from '@snowplow/browser-tracker-core';
import F from 'lodash/fp';
import { ConsentPlugin, trackConsentWithdrawn, trackConsentGranted, enableGdprContext } from '../src';

Expand Down
4 changes: 3 additions & 1 deletion plugins/browser-plugin-enhanced-consent/test/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { addTracker, SharedState } from '@snowplow/browser-tracker-core';
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';

import {
EnhancedConsentPlugin,
trackCmpVisible,
Expand Down
32 changes: 2 additions & 30 deletions plugins/browser-plugin-enhanced-ecommerce/test/contexts.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
/*
* Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';

import { addTracker, SharedState } from '@snowplow/browser-tracker-core';
import F from 'lodash/fp';
import {
EnhancedEcommercePlugin,
Expand Down
33 changes: 2 additions & 31 deletions plugins/browser-plugin-error-tracking/test/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
/*
* Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { addTracker, SharedState } from '@snowplow/browser-tracker-core';
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';
import F from 'lodash/fp';
import { ErrorTrackingPlugin, trackError } from '../src';

Expand Down
3 changes: 2 additions & 1 deletion plugins/browser-plugin-media/test/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addTracker, SharedState } from '@snowplow/browser-tracker-core';
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';
import { PayloadBuilder, SelfDescribingJson } from '@snowplow/tracker-core';
import {
endMediaTracking,
Expand Down
34 changes: 3 additions & 31 deletions trackers/browser-tracker/test/tracker.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
/*
* Copyright (c) 2022 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { SharedState, addTracker } from '@snowplow/browser-tracker-core';
import { SharedState } from '@snowplow/browser-tracker-core';
import { addTracker } from '../../../common/test-utils/utils';

import F from 'lodash/fp';

jest.useFakeTimers('modern');
Expand Down

0 comments on commit 989af04

Please sign in to comment.