Repository: incubator-senssoft-useralejs Updated Branches: refs/heads/master f25b9cb00 -> fbdbcb9c1
Added tests for sendLogs. Project: http://git-wip-us.apache.org/repos/asf/incubator-senssoft-useralejs/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-senssoft-useralejs/commit/c610177f Tree: http://git-wip-us.apache.org/repos/asf/incubator-senssoft-useralejs/tree/c610177f Diff: http://git-wip-us.apache.org/repos/asf/incubator-senssoft-useralejs/diff/c610177f Branch: refs/heads/master Commit: c610177f156a9d2f71c32cc7991ae7aa4b24d846 Parents: f25b9cb Author: Rob Foley <rob.foley...@gmail.com> Authored: Wed Sep 21 17:03:02 2016 -0400 Committer: Rob Foley <rob.foley...@gmail.com> Committed: Wed Sep 21 17:03:02 2016 -0400 ---------------------------------------------------------------------- test/sendLogs_spec.js | 97 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-senssoft-useralejs/blob/c610177f/test/sendLogs_spec.js ---------------------------------------------------------------------- diff --git a/test/sendLogs_spec.js b/test/sendLogs_spec.js index 477147e..5da5157 100644 --- a/test/sendLogs_spec.js +++ b/test/sendLogs_spec.js @@ -16,9 +16,100 @@ */ import { expect } from 'chai'; import jsdom from 'jsdom'; -import fs from 'fs'; +import sinon from 'sinon'; +import { sendOnInterval, sendOnClose } from '../src/sendLogs'; describe('sendLogs', () => { - it('sends logs on an interval'); - it('sends logs on page exit'); + it('sends logs on an interval', (done) => { + let requests = 0; + const originalXMLHttpRequest = global.XMLHttpRequest; + const conf = { transmitInterval: 500, url: 'test', logCountThreshold: 2 }; + const logs = []; + const clock = sinon.useFakeTimers(); + const xhr = sinon.useFakeXMLHttpRequest(); + global.XMLHttpRequest = xhr; + xhr.onCreate = () => { requests++; }; + + sendOnInterval(logs, conf); + + clock.tick(conf.transmitInterval * 2); + // Make sure it doesn't make requests for no raisin + expect(requests).to.equal(0); + + // Make sure it respects the logCountThreshold + logs.push({ foo: 'bar1' }); + clock.tick(conf.transmitInterval); + expect(logs.length).to.equal(1); + + // Make sure it sends the logs and clears the array + logs.push({ foo: 'bar2' }); + clock.tick(conf.transmitInterval); + expect(logs.length).to.equal(0); + expect(requests).to.equal(1); + + xhr.restore(); + clock.restore(); + global.XMLHttpRequest = originalXMLHttpRequest; + done(); + }); + it('sends logs on page exit with navigator', (done) => { + const html = `<html><head></head><body></body></html>`; + jsdom.env({ + html, + done: (err, window) => { + const originalNavigator = global.navigator; + const originalWindow = global.window; + let called = false; + global.window = window; + global.navigator = { + sendBeacon: () => { + called = true; + }, + }; + + const evt = window.document.createEvent('CustomEvent'); + evt.initEvent('unload', true, true); + sendOnClose([{ foo: 'bar' }], { url: 'test' }); + + window.dispatchEvent(evt); + window.close(); + + expect(called).to.equal(true); + global.window = originalWindow; + global.navigator = originalNavigator; + done(); + } + }); + }); + it('sends logs on page exit without navigator', (done) => { + const html = `<html><head></head><body></body></html>`; + jsdom.env({ + html, + done: (err, window) => { + const originalNavigator = global.navigator; + const originalXMLHttpRequest = global.XMLHttpRequest; + const originalWindow = global.window; + let requests = 0; + const xhr = sinon.useFakeXMLHttpRequest(); + global.XMLHttpRequest = xhr; + global.window = window; + global.XMLHttpRequest = xhr; + global.navigator = { sendBeacon: false, }; + xhr.onCreate = () => { requests++; }; + + const evt = window.document.createEvent('CustomEvent'); + evt.initEvent('beforeunload', true, true); + sendOnClose([{ foo: 'bar' }], { url: 'test' }); + + window.dispatchEvent(evt); + window.close(); + + expect(requests).to.equal(1); + global.window = originalWindow; + global.navigator = originalNavigator; + global.XMLHttpRequest = originalXMLHttpRequest; + done(); + } + }); + }); });