UNPKG

@vivianhealth/braze-segment-debounce

Version:

Segment utilities to debounce data before it reaches the Braze destination

515 lines (457 loc) 20 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _server = require("./server"); function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } describe('debouncePayload', function () { var storage; var persistPayload; var fetchPayload; beforeEach(function () { storage = {}; fetchPayload = /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(k) { return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: return _context.abrupt("return", storage[k] || null); case 1: case "end": return _context.stop(); } } }, _callee); })); return function fetchPayload(_x) { return _ref.apply(this, arguments); }; }(); persistPayload = /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(k, i) { return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: storage[k] = i; case 1: case "end": return _context2.stop(); } } }, _callee2); })); return function persistPayload(_x2, _x3) { return _ref2.apply(this, arguments); }; }(); }); afterEach(function () { storage = null; }); test('debounces payload', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() { var userId, key, payload, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload = { userId: userId, traits: { firstName: 'Test', lastName: 'User' } }; _context3.next = 5; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 5: debouncedPayload1 = _context3.sent; _context3.next = 8; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 8: debouncedPayload2 = _context3.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload); expect(debouncedPayload1).toEqual(payload); expect(debouncedPayload2).toBe(null); case 13: case "end": return _context3.stop(); } } }, _callee3); }))); test('debounces payload with anonymousId', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() { var anonymousId, key, payload, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: anonymousId = 1; key = "".concat(anonymousId, "-previousSegmentPayload"); payload = { anonymousId: anonymousId, traits: { firstName: 'Test', lastName: 'User' } }; _context4.next = 5; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 5: debouncedPayload1 = _context4.sent; _context4.next = 8; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 8: debouncedPayload2 = _context4.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload); expect(debouncedPayload1).toEqual(payload); expect(debouncedPayload2).toBe(null); case 13: case "end": return _context4.stop(); } } }, _callee4); }))); test('debounces payload with traits', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5() { var key, payload, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: key = "no-id-previousSegmentPayload"; payload = { traits: { firstName: 'Test', lastName: 'User' } }; _context5.next = 4; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 4: debouncedPayload1 = _context5.sent; _context5.next = 7; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 7: debouncedPayload2 = _context5.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload); expect(debouncedPayload1).toEqual(payload); expect(debouncedPayload2).toBe(null); case 12: case "end": return _context5.stop(); } } }, _callee5); }))); test('debounces payload with new anonymousId', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6() { var userId, key, payload1, payload2, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload1 = { userId: userId, traits: { firstName: 'Test', lastName: 'User' } }; payload2 = { userId: userId, anonymousId: 2, traits: { firstName: 'Test', lastName: 'User' } }; _context6.next = 6; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 6: debouncedPayload1 = _context6.sent; _context6.next = 9; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 9: debouncedPayload2 = _context6.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload2); expect(debouncedPayload1).toEqual(payload1); expect(debouncedPayload2).toEqual(payload2); case 14: case "end": return _context6.stop(); } } }, _callee6); }))); test('debounces payload with new traits', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7() { var userId, key, payload1, payload2, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload1 = { userId: userId, traits: { firstName: 'Test', lastName: 'User', emails: ['test@user.com', 'user@test.com'] } }; payload2 = { userId: userId, traits: { phoneNumber: '123-45678', address: '1 Main St' } }; _context7.next = 6; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 6: debouncedPayload1 = _context7.sent; _context7.next = 9; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 9: debouncedPayload2 = _context7.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(_objectSpread(_objectSpread({}, payload2), {}, { traits: _objectSpread(_objectSpread({}, payload2.traits), {}, { firstName: 'Test', lastName: 'User', emails: ['test@user.com', 'user@test.com'] }) })); expect(debouncedPayload1).toEqual(payload1); expect(debouncedPayload2).toEqual(payload2); case 14: case "end": return _context7.stop(); } } }, _callee7); }))); test('debounces payload with different traits', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8() { var userId, key, payload1, payload2, payload3, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload1 = { userId: userId, traits: { firstName: 'Test', lastName: 'User' } }; payload2 = { userId: userId, traits: { firstName: 'Test', lastName: 'Tester' } }; payload3 = { userId: userId, traits: { lastName: 'Tester' } }; _context8.next = 7; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 7: debouncedPayload1 = _context8.sent; _context8.next = 10; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 10: debouncedPayload2 = _context8.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload2); expect(debouncedPayload1).toEqual(payload1); expect(debouncedPayload2).toEqual(payload3); case 15: case "end": return _context8.stop(); } } }, _callee8); }))); test('debounces payload with different array traits', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9() { var userId, key, payload1, payload2, debouncedPayload1, debouncedPayload2; return _regenerator.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload1 = { userId: userId, traits: { colors: ['blue', 'orange', 'red'] } }; payload2 = { userId: userId, traits: { colors: ['blue', 'orange', 'red', 'green'] } }; _context9.next = 6; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 6: debouncedPayload1 = _context9.sent; _context9.next = 9; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 9: debouncedPayload2 = _context9.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload2); expect(debouncedPayload1).toEqual(payload1); expect(debouncedPayload2).toEqual(payload2); case 14: case "end": return _context9.stop(); } } }, _callee9); }))); it('debounces payload with multiple calls', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10() { var userId, key, payloads, debouncedPayloads, _iterator, _step, payload, debouncedPayload; return _regenerator.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payloads = (0, _toConsumableArray2.default)(Array(4)).map(function (_, i) { return { userId: userId, traits: { firstName: 'Test', lastName: 'Tester', uniqueId: i } }; }); debouncedPayloads = []; _iterator = _createForOfIteratorHelper(payloads); _context10.prev = 5; _iterator.s(); case 7: if ((_step = _iterator.n()).done) { _context10.next = 15; break; } payload = _step.value; _context10.next = 11; return (0, _server.debouncePayload)(payload, fetchPayload, persistPayload); case 11: debouncedPayload = _context10.sent; debouncedPayloads.push(debouncedPayload); case 13: _context10.next = 7; break; case 15: _context10.next = 20; break; case 17: _context10.prev = 17; _context10.t0 = _context10["catch"](5); _iterator.e(_context10.t0); case 20: _context10.prev = 20; _iterator.f(); return _context10.finish(20); case 23: expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payloads[3]); expect(debouncedPayloads[0]).toEqual({ userId: userId, traits: { firstName: 'Test', lastName: 'Tester', uniqueId: 0 } }); (0, _toConsumableArray2.default)(Array(3)).forEach(function (_, i) { return expect(debouncedPayloads[i + 1]).toEqual({ userId: userId, traits: { uniqueId: i + 1 } }); }); case 27: case "end": return _context10.stop(); } } }, _callee10, null, [[5, 17, 20, 23]]); }))); it('debounces payload with repeated different traits', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11() { var userId, key, payload1, payload2, debouncedPayload1, debouncedPayload2, debouncedPayload3, debouncedPayload4; return _regenerator.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: userId = 1; key = "".concat(userId, "-previousSegmentPayload"); payload1 = { userId: userId, traits: { color: 'blue' } }; payload2 = { userId: userId, traits: { color: 'orange' } }; _context11.next = 6; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 6: debouncedPayload1 = _context11.sent; _context11.next = 9; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 9: debouncedPayload2 = _context11.sent; _context11.next = 12; return (0, _server.debouncePayload)(payload1, fetchPayload, persistPayload); case 12: debouncedPayload3 = _context11.sent; _context11.next = 15; return (0, _server.debouncePayload)(payload2, fetchPayload, persistPayload); case 15: debouncedPayload4 = _context11.sent; expect(Object.keys(storage).length).toBe(1); expect(JSON.parse(storage[key])).toEqual(payload2); expect(debouncedPayload1).toEqual(payload1); expect(debouncedPayload2).toEqual(payload2); expect(debouncedPayload3).toEqual(payload1); expect(debouncedPayload4).toEqual(payload2); case 22: case "end": return _context11.stop(); } } }, _callee11); }))); });