diff --git a/test/helpers/helper_functions.js b/test/helpers/helper_functions.js index c441fcc..0cc69f9 100644 --- a/test/helpers/helper_functions.js +++ b/test/helpers/helper_functions.js @@ -221,7 +221,7 @@ function userDetailRequestValidator(originalDetails, details) { assert.equal(originalDetails.byear, user.byear); if (typeof originalDetails.custom !== 'undefined') { for (var key in originalDetails.custom) { - assert.equal(originalDetails.custom[key], user.custom[key]); + assert.deepStrictEqual(originalDetails.custom[key], user.custom[key]); } } } diff --git a/test/helpers/test_utils.js b/test/helpers/test_utils.js new file mode 100644 index 0000000..d5836e8 --- /dev/null +++ b/test/helpers/test_utils.js @@ -0,0 +1,62 @@ +var eventObj = { + key: "event_check", + count: 55, + sum: 3.14, + dur: 2000, + segmentation: { + string_value: "example", + number_value: 42, + boolean_value: true, + array_value: ["item1", "item2"], + object_value: { nested_key: "nested_value" }, + null_value: null, + undefined_value: undefined, + }, +}; + +var timedEventObj = { + key: "timed", + count: 1, + segmentation: { + app_version: "1.0", + country: "Turkey", + }, +}; + +var userDetailObj = { + name: "Alexandrina Jovovich", + username: "alex_jov", + email: "alex.jov@example.com", + organization: "TechNova", + phone: "+987654321", + picture: "https://example.com/images/profile_alex.jpg", + gender: "Female", + byear: 1992, // birth year + custom: { + string_value: "example", + number_value: 42, + boolean_value: true, + array_value: ["item1", "item2"], + object_value: { nested_key: "nested_value" }, + null_value: null, + undefined_value: undefined, + }, +}; + +var getUserDetailsObj = function() { + return userDetailObj; +}; + +var getEventObj = function() { + return eventObj; +}; + +var getTimedEventObj = function() { + return timedEventObj; +}; + +module.exports = { + getEventObj, + getUserDetailsObj, + getTimedEventObj, +}; \ No newline at end of file diff --git a/test/tests_bulk.js b/test/tests_bulk.js index a6b23df..26743d4 100644 --- a/test/tests_bulk.js +++ b/test/tests_bulk.js @@ -4,6 +4,7 @@ const assert = require("assert"); const CountlyBulk = require("../lib/countly-bulk"); var hp = require("./helpers/helper_functions"); var storage = require("../lib/countly-storage"); +var testUtils = require("./helpers/test_utils"); const { StorageTypes } = CountlyBulk; @@ -71,51 +72,15 @@ function validateUserDetails(actual, expected) { return isValid; } -var eventObj = { - key: "bulk_check", - count: 55, - sum: 3.14, - dur: 2000, - segmentation: { - string_value: "example", - number_value: 42, - boolean_value: true, - array_value: ["item1", "item2"], - object_value: { nested_key: "nested_value" }, - null_value: null, - undefined_value: undefined, - }, -}; - -var userDetailObj = { - name: "Alexandrina Jovovich", - username: "alex_jov", - email: "alex.jov@example.com", - organization: "TechNova", - phone: "+987654321", - picture: "https://example.com/images/profile_alex.jpg", - gender: "Female", - byear: 1992, // birth year - custom: { - string_value: "example", - number_value: 42, - boolean_value: true, - array_value: ["item1", "item2"], - object_value: { nested_key: "nested_value" }, - null_value: null, - undefined_value: undefined, - }, -}; - // Create bulk data function createBulkData(bulk) { // Add an event var user = bulk.add_user({ device_id: "testUser1" }); - user.add_event(eventObj); + user.add_event(testUtils.getEventObj()); // add user details var user2 = bulk.add_user({ device_id: "testUser2" }); - user2.user_details(userDetailObj); + user2.user_details(testUtils.getUserDetailsObj()); // add request bulk.add_request({ device_id: "TestUser3" }); @@ -152,11 +117,11 @@ function validateCreatedBulkData(bulk) { var deviceEvents = events.testUser1; // Access the events for the specific device var recordedEvent = deviceEvents[0]; // Access the first event - hp.eventValidator(eventObj, recordedEvent); + hp.eventValidator(testUtils.getEventObj(), recordedEvent); var req = reqQueue[0]; // read user details queue const actualUserDetails = req.user_details; // Extract the user_details from the actual request - const isValid = validateUserDetails(actualUserDetails, userDetailObj); + const isValid = validateUserDetails(actualUserDetails, testUtils.getUserDetailsObj()); assert.equal(true, isValid); var testUser3Request = reqQueue.find((request) => request.device_id === "TestUser3"); diff --git a/test/tests_events.js b/test/tests_events.js index 246063d..127c4d5 100644 --- a/test/tests_events.js +++ b/test/tests_events.js @@ -1,6 +1,7 @@ /* eslint-disable no-console */ var Countly = require("../lib/countly"); var hp = require("./helpers/helper_functions"); +var testUtils = require("./helpers/test_utils"); // init function function initMain() { @@ -11,26 +12,7 @@ function initMain() { max_events: -1, }); } -// an event object to use -var eventObj = { - key: "in_app_purchase", - count: 3, - sum: 2.97, - dur: 1000, - segmentation: { - app_version: "1.0", - country: "Turkey", - }, -}; -// a timed event object -var timedEventObj = { - key: "timed", - count: 1, - segmentation: { - app_version: "1.0", - country: "Turkey", - }, -}; + describe("Events tests", () => { beforeEach(async() => { await hp.clearStorage(); @@ -39,11 +21,11 @@ describe("Events tests", () => { // initialize SDK initMain(); // send custom event - Countly.add_event(eventObj); + Countly.add_event(testUtils.getEventObj()); // read event queue setTimeout(() => { var event = hp.readEventQueue()[0]; - hp.eventValidator(eventObj, event); + hp.eventValidator(testUtils.getEventObj(), event); done(); }, hp.mWait); }); @@ -53,11 +35,11 @@ describe("Events tests", () => { // send timed event Countly.start_event("timed"); setTimeout(() => { - Countly.end_event(timedEventObj); + Countly.end_event(testUtils.getTimedEventObj()); // read event queue setTimeout(() => { var event = hp.readEventQueue()[0]; - hp.eventValidator(timedEventObj, event, (hp.mWait / 1000)); + hp.eventValidator(testUtils.getTimedEventObj(), event, (hp.mWait / 1000)); done(); }, hp.sWait); }, hp.mWait); diff --git a/test/tests_storage.js b/test/tests_storage.js index 6a8350f..d2da2c0 100644 --- a/test/tests_storage.js +++ b/test/tests_storage.js @@ -7,36 +7,10 @@ var Countly = require("../lib/countly"); var storage = require("../lib/countly-storage"); var cc = require("../lib/countly-common"); var hp = require("./helpers/helper_functions"); +var testUtils = require("./helpers/test_utils"); const { StorageTypes } = Countly; -// example event object to use -var eventObj = { - key: "storage_check", - count: 5, - sum: 3.14, - dur: 2000, - segmentation: { - app_version: "1.0", - country: "Zambia", - }, -}; - -var userDetailObj = { - name: "Akira Kurosawa", - username: "a_kurosawa", - email: "akira.kurosawa@filmlegacy.com", - organization: "Toho Studios", - phone: "+81312345678", - picture: "https://example.com/profile_images/akira_kurosawa.jpg", - gender: "Male", - byear: 1910, - custom: { - "known for": "Film Director", - "notable works": "Seven Samurai, Rashomon, Ran", - }, -}; - // init function function initMain(device_id) { Countly.init({ @@ -196,13 +170,13 @@ describe("Storage Tests", () => { // event should be recorded stored and validated correctly it("4- Record event and validate storage", (done) => { initMain(); - Countly.add_event(eventObj); + Countly.add_event(testUtils.getEventObj()); setTimeout(() => { var storedEvents = hp.readEventQueue(); assert.equal(storedEvents.length, 1); var event = storedEvents[0]; - hp.eventValidator(eventObj, event); + hp.eventValidator(testUtils.getEventObj(), event); done(); }, hp.mWait); }); @@ -379,12 +353,12 @@ describe("Storage Tests", () => { hp.doesFileStoragePathsExist((exists) => { assert.equal(false, exists); }); - Countly.add_event(eventObj); + Countly.add_event(testUtils.getEventObj()); setTimeout(() => { const storedData = storage.storeGet("cly_queue", null); const eventArray = JSON.parse(storedData[0].events); const eventFromQueue = eventArray[0]; - hp.eventValidator(eventObj, eventFromQueue); + hp.eventValidator(testUtils.getEventObj(), eventFromQueue); done(); }, hp.mWait); }); @@ -402,10 +376,10 @@ describe("Storage Tests", () => { hp.doesFileStoragePathsExist((exists) => { assert.equal(false, exists); }); - Countly.user_details(userDetailObj); + Countly.user_details(testUtils.getUserDetailsObj); const storedData = storage.storeGet("cly_queue", null); const userDetailsReq = storedData[0]; - hp.userDetailRequestValidator(userDetailObj, userDetailsReq); + hp.userDetailRequestValidator(testUtils.getUserDetailsObj, userDetailsReq); done(); }); diff --git a/test/tests_user_details.js b/test/tests_user_details.js index dfb06cd..e7a3171 100644 --- a/test/tests_user_details.js +++ b/test/tests_user_details.js @@ -1,21 +1,8 @@ /* eslint-disable no-console */ var Countly = require("../lib/countly"); var hp = require("./helpers/helper_functions"); +var testUtils = require("./helpers/test_utils"); -var userDetailObj = { - name: "Barturiana Sosinsiava", - username: "bar2rawwen", - email: "test@test.com", - organization: "Dukely", - phone: "+123456789", - picture: "https://ps.timg.com/profile_images/52237/011_n_400x400.jpg", - gender: "Non-binary", - byear: 1987, // birth year - custom: { - "key1 segment": "value1 segment", - "key2 segment": "value2 segment", - }, -}; // init function function initMain() { Countly.init({ @@ -34,11 +21,11 @@ describe("User details tests", () => { // initialize SDK initMain(); // send user details - Countly.user_details(userDetailObj); + Countly.user_details(testUtils.getUserDetailsObj()); // read event queue setTimeout(() => { var req = hp.readRequestQueue()[0]; - hp.userDetailRequestValidator(userDetailObj, req); + hp.userDetailRequestValidator(testUtils.getUserDetailsObj(), req); done(); }, hp.sWait); });