Skip to content

Commit db5ca11

Browse files
Jaissica HoraJaissica Hora
authored andcommitted
test: added tests for use method
1 parent cbaff08 commit db5ca11

1 file changed

Lines changed: 173 additions & 0 deletions

File tree

test/src/tests.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,179 @@ describe('Rokt Forwarder', () => {
13911391
});
13921392
});
13931393

1394+
describe('#use', () => {
1395+
beforeEach(() => {
1396+
window.Rokt = new MockRoktForwarder();
1397+
window.mParticle.Rokt = window.Rokt;
1398+
window.mParticle.Rokt.attachKitCalled = false;
1399+
window.mParticle.Rokt.attachKit = async (kit) => {
1400+
window.mParticle.Rokt.attachKitCalled = true;
1401+
window.mParticle.Rokt.kit = kit;
1402+
Promise.resolve();
1403+
};
1404+
});
1405+
1406+
it('should call launcher.use with the provided extension name when fully initialized', async () => {
1407+
window.mParticle.forwarder.isInitialized = true;
1408+
window.mParticle.forwarder.launcher = {
1409+
use: function (name) {
1410+
window.Rokt.useCalled = true;
1411+
window.Rokt.useName = name;
1412+
return Promise.resolve({});
1413+
},
1414+
};
1415+
1416+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1417+
1418+
window.Rokt.useCalled.should.equal(true);
1419+
window.Rokt.useName.should.equal('ThankYouPageJourney');
1420+
});
1421+
1422+
it('should reject when called before initialization', async () => {
1423+
window.mParticle.forwarder.isInitialized = false;
1424+
window.mParticle.forwarder.launcher = {
1425+
use: function () {},
1426+
};
1427+
1428+
let caught = null;
1429+
try {
1430+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1431+
} catch (e) {
1432+
caught = e;
1433+
}
1434+
1435+
(!!caught).should.equal(true);
1436+
caught.message.should.equal('Rokt Kit: Not initialized');
1437+
});
1438+
1439+
it('should log an error when called before initialization', async () => {
1440+
let errorLogged = false;
1441+
let errorMessage = null;
1442+
window.console.error = function (message) {
1443+
errorLogged = true;
1444+
errorMessage = message;
1445+
};
1446+
1447+
window.mParticle.forwarder.isInitialized = false;
1448+
window.mParticle.forwarder.launcher = null;
1449+
1450+
try {
1451+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1452+
} catch (e) {}
1453+
1454+
errorLogged.should.equal(true);
1455+
errorMessage.should.equal('Rokt Kit: Not initialized');
1456+
});
1457+
1458+
it('should reject when extension name is invalid', async () => {
1459+
window.mParticle.forwarder.isInitialized = true;
1460+
window.mParticle.forwarder.launcher = {
1461+
use: function () {
1462+
return Promise.resolve({});
1463+
},
1464+
};
1465+
1466+
let caught = null;
1467+
try {
1468+
await window.mParticle.forwarder.use(123);
1469+
} catch (e) {
1470+
caught = e;
1471+
}
1472+
1473+
(!!caught).should.equal(true);
1474+
caught.message.should.equal('Rokt Kit: Invalid extension name');
1475+
});
1476+
1477+
it('should reject when kit is initialized but launcher is missing', async () => {
1478+
window.mParticle.forwarder.isInitialized = true;
1479+
window.mParticle.forwarder.launcher = null;
1480+
1481+
let caught = null;
1482+
try {
1483+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1484+
} catch (e) {
1485+
caught = e;
1486+
}
1487+
1488+
(!!caught).should.equal(true);
1489+
caught.message.should.equal('Rokt Kit: Not initialized');
1490+
});
1491+
1492+
it('should log an error when kit is initialized but launcher is missing', async () => {
1493+
let errorLogged = false;
1494+
let errorMessage = null;
1495+
window.console.error = function (message) {
1496+
errorLogged = true;
1497+
errorMessage = message;
1498+
};
1499+
1500+
window.mParticle.forwarder.isInitialized = true;
1501+
window.mParticle.forwarder.launcher = null;
1502+
1503+
try {
1504+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1505+
} catch (e) {}
1506+
1507+
errorLogged.should.equal(true);
1508+
errorMessage.should.equal('Rokt Kit: Not initialized');
1509+
});
1510+
1511+
it('should call launcher.use after init (test mode) and attach', async () => {
1512+
window.mParticle.Rokt.attachKitCalled = false;
1513+
window.mParticle.Rokt.attachKit = async (kit) => {
1514+
window.mParticle.Rokt.attachKitCalled = true;
1515+
window.mParticle.Rokt.kit = kit;
1516+
Promise.resolve();
1517+
};
1518+
1519+
window.Rokt.createLauncher = async function () {
1520+
return Promise.resolve({
1521+
use: function (name) {
1522+
window.Rokt.useCalled = true;
1523+
window.Rokt.useName = name;
1524+
return Promise.resolve({});
1525+
},
1526+
});
1527+
};
1528+
1529+
await window.mParticle.forwarder.init(
1530+
{
1531+
accountId: '123456',
1532+
},
1533+
reportService.cb,
1534+
true,
1535+
null,
1536+
{}
1537+
);
1538+
1539+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
1540+
1541+
await window.mParticle.forwarder.use('ThankYouPageJourney');
1542+
1543+
window.Rokt.useCalled.should.equal(true);
1544+
window.Rokt.useName.should.equal('ThankYouPageJourney');
1545+
});
1546+
1547+
it('should reject errors from launcher.use', async () => {
1548+
window.mParticle.forwarder.isInitialized = true;
1549+
window.mParticle.forwarder.launcher = {
1550+
use: function () {
1551+
return Promise.reject(new Error('Unknown extension'));
1552+
},
1553+
};
1554+
1555+
let caught = null;
1556+
try {
1557+
await window.mParticle.forwarder.use('UnknownExtension');
1558+
} catch (e) {
1559+
caught = e;
1560+
}
1561+
1562+
(!!caught).should.equal(true);
1563+
caught.message.should.equal('Unknown extension');
1564+
});
1565+
});
1566+
13941567
describe('#setUserAttribute', () => {
13951568
it('should set the user attribute', async () => {
13961569
window.mParticle.forwarder.setUserAttribute(

0 commit comments

Comments
 (0)