@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
118 lines (92 loc) • 3.51 kB
JavaScript
import * as ReactNative from "react-native";
const mock_postEvent = jest.fn();
const mock_setPlugins = jest.fn();
const mock_postTimedEvent = jest.fn();
const mock_endTimedEvent = jest.fn();
const AnalyticsBridge = {
postEvent: mock_postEvent,
setPlugins: mock_setPlugins,
postTimedEvent: mock_postTimedEvent,
endTimedEvent: mock_endTimedEvent,
};
ReactNative.NativeModules.AnalyticsBridge = AnalyticsBridge;
const eventName = "event";
const payload = {};
const analyticsManager = require("../manager");
function clearAllMocks() {
mock_postEvent.mockClear();
mock_setPlugins.mockClear();
mock_postTimedEvent.mockClear();
mock_endTimedEvent.mockClear();
}
describe("AnalyticsManager", () => {
describe("isValidAnalyticsData", () => {
beforeEach(clearAllMocks);
it("returns false if eventName is not a string", () => {
expect(analyticsManager.isValidAnalyticsData(42)).toBe(false);
expect(analyticsManager.isValidAnalyticsData({})).toBe(false);
expect(analyticsManager.isValidAnalyticsData([])).toBe(false);
expect(analyticsManager.isValidAnalyticsData(() => ({}))).toBe(false);
});
it("returns false if payload is not an object", () => {
expect(analyticsManager.isValidAnalyticsData(eventName, "string")).toBe(
false
);
expect(analyticsManager.isValidAnalyticsData(eventName, 42)).toBe(false);
expect(analyticsManager.isValidAnalyticsData(eventName, () => ({}))).toBe(
false
);
});
it("returns true if eventName is a string and payload is null or an object", () => {
expect(analyticsManager.isValidAnalyticsData(eventName)).toBe(true);
expect(analyticsManager.isValidAnalyticsData(eventName, payload)).toBe(
true
);
});
});
describe("postAnalyticEvent", () => {
beforeEach(clearAllMocks);
it("calls the postEvent method of the analytic bridge", () => {
analyticsManager.postAnalyticEvent(eventName, payload);
expect(mock_postEvent).toHaveBeenCalledWith(eventName, payload);
});
it("skips the event if it is not valid", () => {
analyticsManager.postAnalyticEvent(42);
expect(mock_postTimedEvent).not.toHaveBeenCalled();
});
});
describe("providePlugins", () => {
beforeEach(clearAllMocks);
it("calls the setPlugins method of the analytic bridge", () => {
const plugins = [];
const plugin_configurations = {};
const callback = jest.fn();
analyticsManager.providePlugins(plugins, plugin_configurations, callback);
expect(mock_setPlugins).toHaveBeenCalledWith(
plugins,
plugin_configurations,
callback
);
});
});
describe("startAnalyticsTimedEvent", () => {
beforeEach(clearAllMocks);
it("skips the event if it is not valid", () => {
analyticsManager.startAnalyticsTimedEvent(42);
expect(mock_postTimedEvent).not.toHaveBeenCalled();
});
});
describe("endAnalyticsTimedEvent", () => {
beforeEach(clearAllMocks);
it("emits a time_on_screen event and adds duration to payload", () => {
analyticsManager.endAnalyticsTimedEvent(eventName, payload);
const newEventName = "time_on_screen";
payload.duration = "N/A";
expect(mock_postEvent).toHaveBeenCalledWith(newEventName, payload);
});
it("skips the event if it is not valid", () => {
analyticsManager.endAnalyticsTimedEvent(42);
expect(mock_postEvent).not.toHaveBeenCalled();
});
});
});