Skip to content

Commit 70bf370

Browse files
Paul Boocockpaulboocock
authored andcommitted
Fix removeGlobalContexts not removing expected context (close snowplow#1006)
1 parent 9e3e034 commit 70bf370

6 files changed

Lines changed: 88 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix removeGlobalContexts not removing expected context (#1006)",
5+
"type": "none",
6+
"packageName": "@snowplow/browser-tracker-core"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix removeGlobalContexts not removing expected context (#1006)",
5+
"type": "none",
6+
"packageName": "@snowplow/tracker-core"
7+
}
8+
],
9+
"packageName": "@snowplow/tracker-core",
10+
"email": "paul@snowplowanalytics.com"
11+
}

libraries/browser-tracker-core/CHANGELOG.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"comments": {
99
"none": [
1010
{
11-
"comment": "Fix linkDecorationHandler targeting (closes #1002)"
11+
"comment": "Fix linkDecorationHandler targeting (#1002)"
1212
}
1313
]
1414
}

libraries/browser-tracker-core/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Tue, 21 Sep 2021 14:59:36 GMT
77

88
### Updates
99

10-
- Fix linkDecorationHandler targeting (closes #1002)
10+
- Fix linkDecorationHandler targeting (#1002)
1111

1212
## 3.1.3
1313
Mon, 23 Aug 2021 10:13:18 GMT

libraries/tracker-core/src/contexts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ export function globalContexts(): GlobalContexts {
193193
for (const context of contexts) {
194194
if (isConditionalContextProvider(context)) {
195195
conditionalProviders = conditionalProviders.filter(
196-
(item) => JSON.stringify(item) === JSON.stringify(context)
196+
(item) => JSON.stringify(item) !== JSON.stringify(context)
197197
);
198198
} else if (isContextPrimitive(context)) {
199-
globalPrimitives = globalPrimitives.filter((item) => JSON.stringify(item) === JSON.stringify(context));
199+
globalPrimitives = globalPrimitives.filter((item) => JSON.stringify(item) !== JSON.stringify(context));
200200
}
201201
}
202202
},

libraries/tracker-core/test/contexts.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,68 @@ test('Add global contexts', (t) => {
215215
t.is(globalContexts.getConditionalProviders().length, 2, 'Correct number of conditional providers added');
216216
});
217217

218+
test('Remove one of two global context primitives', (t) => {
219+
const geolocationContext = {
220+
schema: 'iglu:com.snowplowanalytics.snowplow/geolocation_context/jsonschema/1-1-0',
221+
data: {
222+
latitude: 40.0,
223+
longitude: 55.1,
224+
},
225+
};
226+
227+
const webPageContext = {
228+
schema: 'iglu:org.schema/WebPage/jsonschema/1-0-0',
229+
data: {
230+
genre: 'test',
231+
},
232+
};
233+
234+
const globalContexts = contexts.globalContexts();
235+
globalContexts.addGlobalContexts([geolocationContext, webPageContext]);
236+
globalContexts.removeGlobalContexts([geolocationContext]);
237+
t.deepEqual(globalContexts.getGlobalPrimitives(), [webPageContext]);
238+
});
239+
240+
test('Remove one of two global context conditional providers', (t) => {
241+
const geolocationContext = {
242+
schema: 'iglu:com.snowplowanalytics.snowplow/geolocation_context/jsonschema/1-1-0',
243+
data: {
244+
latitude: 40.0,
245+
longitude: 55.1,
246+
},
247+
};
248+
249+
function eventTypeContextGenerator(args?: contexts.ContextEvent) {
250+
const context: SelfDescribingJson = {
251+
schema: 'iglu:com.snowplowanalytics.snowplow/mobile_context/jsonschema/1-0-1',
252+
data: {
253+
osType: 'ubuntu',
254+
osVersion: '2018.04',
255+
deviceManufacturer: 'ASUS',
256+
deviceModel: args ? String(args['eventType']) : '',
257+
},
258+
};
259+
return context;
260+
}
261+
262+
const bothRuleSet = {
263+
accept: ['iglu:com.snowplowanalytics.snowplow/*/jsonschema/*-*-*'],
264+
reject: ['iglu:com.snowplowanalytics.snowplow/*/jsonschema/*-*-*'],
265+
};
266+
267+
const filterFunction = function (args?: contexts.ContextEvent) {
268+
return args?.eventType === 'ue';
269+
};
270+
271+
const filterProvider: contexts.FilterProvider = [filterFunction, [geolocationContext, eventTypeContextGenerator]];
272+
const ruleSetProvider: contexts.RuleSetProvider = [bothRuleSet, [geolocationContext, eventTypeContextGenerator]];
273+
const globalContexts = contexts.globalContexts();
274+
275+
globalContexts.addGlobalContexts([filterProvider, ruleSetProvider]);
276+
globalContexts.removeGlobalContexts([filterProvider]);
277+
t.deepEqual(globalContexts.getConditionalProviders(), [ruleSetProvider]);
278+
});
279+
218280
test('Remove global contexts', (t) => {
219281
const geolocationContext = {
220282
schema: 'iglu:com.snowplowanalytics.snowplow/geolocation_context/jsonschema/1-1-0',

0 commit comments

Comments
 (0)