Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/roktManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export default class RoktManager {
const enrichedOptions = {
...options,
attributes: enrichedAttributes,
devPassedAttributes: attributes,
};

return this.kit.selectPlacements(enrichedOptions);
Expand Down
197 changes: 187 additions & 10 deletions test/jest/roktManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ describe('RoktManager', () => {
attributes: {
mapped_key: 'test_value', // This key should be mapped
other_attr: 'other_value' // This key should remain unchanged
},
devPassedAttributes: {
original_key: 'test_value',
other_attr: 'other_value'
}
};

Expand Down Expand Up @@ -645,7 +649,10 @@ describe('RoktManager', () => {
} as IRoktSelectPlacementsOptions;

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {},
devPassedAttributes: {}
});
});

it('should call kit.selectPlacements with passed in attributes', async () => {
Expand Down Expand Up @@ -677,7 +684,22 @@ describe('RoktManager', () => {
};

await roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
age: 25,
score: 100.5,
isSubscribed: true,
isActive: false,
interests: 'sports,music,books'
},
devPassedAttributes: {
age: 25,
score: 100.5,
isSubscribed: true,
isActive: false,
interests: 'sports,music,books'
}
});
});

it('should queue the selectPlacements method if no launcher or kit is attached', () => {
Expand Down Expand Up @@ -739,7 +761,10 @@ describe('RoktManager', () => {

expect(roktManager['kit']).not.toBeNull();
expect(roktManager['messageQueue'].size).toBe(0);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {},
devPassedAttributes: {}
});
expect(result).toEqual(expectedResult);
});

Expand Down Expand Up @@ -779,9 +804,26 @@ describe('RoktManager', () => {
}
};

const expectedOptions = {
attributes: {
age: 25,
score: 100.5,
isSubscribed: true,
isActive: false,
interests: 'sports,music,books'
},
devPassedAttributes: {
age: 25,
score: 100.5,
isSubscribed: true,
isActive: false,
interests: 'sports,music,books'
}
};

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.launcher.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(expectedOptions);
expect(kit.launcher.selectPlacements).toHaveBeenCalledWith(expectedOptions);
});

it('should pass sandbox flag as an attribute through to kit.selectPlacements', () => {
Expand Down Expand Up @@ -811,7 +853,17 @@ describe('RoktManager', () => {
};

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
customAttr: 'value',
sandbox: true
},
identifier: 'test-identifier',
devPassedAttributes: {
customAttr: 'value',
sandbox: true
}
});
});

it('should NOT override global sandbox in placement attributes when initialized as true', () => {
Expand Down Expand Up @@ -843,7 +895,16 @@ describe('RoktManager', () => {
roktManager.selectPlacements(options);

expect(roktManager['sandbox']).toBeTruthy();
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
customAttr: 'value',
sandbox: false
},
devPassedAttributes: {
customAttr: 'value',
sandbox: false
}
});
});

it('should set sandbox in placement attributes when not initialized globally', () => {
Expand Down Expand Up @@ -874,7 +935,17 @@ describe('RoktManager', () => {
};

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
customAttr: 'value',
sandbox: true
},
identifier: 'test-identifier',
devPassedAttributes: {
customAttr: 'value',
sandbox: true
}
});
});

it('should pass mapped attributes to kit.launcher.selectPlacements', () => {
Expand Down Expand Up @@ -919,6 +990,11 @@ describe('RoktManager', () => {
firstname: 'John',
lastname: 'Doe',
score: 42,
},
devPassedAttributes: {
'f.name': 'John',
'last_name': 'Doe',
'score': 42,
}
};

Expand Down Expand Up @@ -952,7 +1028,101 @@ describe('RoktManager', () => {
};

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
'f.name': 'John',
'last_name': 'Doe',
'score': 42,
'age': 25,
},
devPassedAttributes: {
'f.name': 'John',
'last_name': 'Doe',
'score': 42,
'age': 25,
}
});
});

it('should pass devPassedAttributes to kit for event logging', () => {
const kit: Partial<IRoktKit> = {
launcher: {
selectPlacements: jest.fn(),
hashAttributes: jest.fn(),
use: jest.fn(),
},
hashAttributes: jest.fn(),
selectPlacements: jest.fn(),
setExtensionData: jest.fn(),
use: jest.fn(),
};

roktManager.kit = kit as IRoktKit;
roktManager['placementAttributesMapping'] = [];

const options: IRoktSelectPlacementsOptions = {
attributes: {
'customAttr': 'value',
}
};

roktManager.selectPlacements(options);

expect(kit.selectPlacements).toHaveBeenCalledWith(
expect.objectContaining({
devPassedAttributes: {
'customAttr': 'value',
}
})
);
});

it('should include original attributes in devPassedAttributes even after mapping', () => {
const kit: Partial<IRoktKit> = {
launcher: {
selectPlacements: jest.fn(),
hashAttributes: jest.fn(),
use: jest.fn(),
},
hashAttributes: jest.fn(),
selectPlacements: jest.fn(),
setExtensionData: jest.fn(),
use: jest.fn(),
};

roktManager.kit = kit as IRoktKit;
roktManager['placementAttributesMapping'] = [
{
jsmap: null,
map: 'f.name',
maptype: 'UserAttributeClass.Name',
value: 'firstname'
}
];

const options: IRoktSelectPlacementsOptions = {
attributes: {
'f.name': 'John',
'userId': 'user123',
}
};

roktManager.selectPlacements(options);

// devPassedAttributes should contain the ORIGINAL unmapped attributes
expect(kit.selectPlacements).toHaveBeenCalledWith(
expect.objectContaining({
devPassedAttributes: {
'f.name': 'John',
'userId': 'user123',
},
// attributes should be mapped
attributes: {
'firstname': 'John',
'userId': 'user123',
}
})
);
});

it('should set the mapped attributes on the current user via setUserAttributes', async () => {
Expand Down Expand Up @@ -1031,7 +1201,14 @@ describe('RoktManager', () => {
};

roktManager.selectPlacements(options);
expect(kit.selectPlacements).toHaveBeenCalledWith(options);
expect(kit.selectPlacements).toHaveBeenCalledWith({
attributes: {
'sandbox': true
},
devPassedAttributes: {
'sandbox': true
}
});
expect(roktManager['currentUser'].setUserAttributes).not.toHaveBeenCalledWith({
sandbox: true
});
Expand Down
Loading