Skip to content

Commit 25465c8

Browse files
authored
[NOCSL] Request Queue - handle domlesss case (#368)
* add domlesss case * address comments
1 parent 80c917c commit 25465c8

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

spec/src/utils/request-queue.js

+76
Original file line numberDiff line numberDiff line change
@@ -850,5 +850,81 @@ describe('ConstructorIO - Utils - Request Queue', function utilsRequestQueue() {
850850
});
851851
});
852852
});
853+
854+
describe('domless', () => {
855+
let fetchSpy = null;
856+
857+
before(() => {
858+
helpers.clearStorage();
859+
});
860+
861+
beforeEach(() => {
862+
global.CLIENT_VERSION = 'cio-mocha';
863+
fetchSpy = sinon.spy(fetch);
864+
865+
requestQueueOptions = {
866+
fetch: fetchSpy,
867+
sendTrackingEvents: true,
868+
trackingSendDelay: 1,
869+
};
870+
});
871+
872+
afterEach(() => {
873+
requestQueueOptions = {};
874+
helpers.clearStorage();
875+
});
876+
877+
it('Should add url requests to the queue if the user is domless', async () => {
878+
const requests = new RequestQueue(requestQueueOptions);
879+
880+
requests.queue('https://ac.cnstrc.com/behavior?action=session_start');
881+
requests.queue('https://ac.cnstrc.com/behavior?action=focus');
882+
requests.queue('https://ac.cnstrc.com/behavior?action=magic_number_three');
883+
884+
expect(RequestQueue.get()).to.be.an('array').length(3);
885+
});
886+
887+
it('Should send requests from the queue if the user is domless', (done) => {
888+
const requests1 = new RequestQueue(requestQueueOptions);
889+
const requests2 = new RequestQueue(requestQueueOptions);
890+
const sendSpy1 = sinon.spy(requests1, 'send');
891+
const sendSpy2 = sinon.spy(requests2, 'send');
892+
893+
store.local.set(storageKey, [
894+
{
895+
url: 'https://ac.cnstrc.com/behavior?action=session_start',
896+
method: 'GET',
897+
},
898+
{
899+
url: 'https://ac.cnstrc.com/behavior?action=focus',
900+
method: 'GET',
901+
},
902+
{
903+
url: 'https://ac.cnstrc.com/behavior?action=magic_number_three',
904+
method: 'GET',
905+
},
906+
{
907+
url: 'https://ac.cnstrc.com/behavior?action=magic_number_four',
908+
method: 'GET',
909+
},
910+
{
911+
url: 'https://ac.cnstrc.com/behavior?action=magic_number_five',
912+
method: 'GET',
913+
},
914+
]);
915+
916+
requests1.send();
917+
requests2.send();
918+
919+
setTimeout(() => {
920+
expect(sendSpy1.callCount).to.be.at.least(2 + 1); // 2 min sent + 1 finally
921+
expect(sendSpy2.callCount).to.be.at.least(2 + 1); // 2 min sent + 1 finally
922+
expect(sendSpy1.callCount + sendSpy2.callCount).to.equal(5 + 2); // 5 sent + 2 finally
923+
expect(RequestQueue.get()).to.be.an('array').length(0);
924+
expect(store.local.get(storageKey)).to.be.null;
925+
done();
926+
}, waitInterval);
927+
});
928+
});
853929
}
854930
});

src/utils/request-queue.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class RequestQueue {
3131

3232
// Add request to queue to be dispatched
3333
queue(url, method = 'GET', body = {}, networkParameters = {}) {
34-
if (this.sendTrackingEvents && !this.humanity.isBot()) {
34+
// Consider user "human" if no DOM context is available
35+
if (this.sendTrackingEvents && (!helpers.canUseDOM() || !this.humanity.isBot())) {
3536
const queue = RequestQueue.get();
3637

3738
// PII Detection & Obfuscation

0 commit comments

Comments
 (0)