Skip to content

Commit

Permalink
Adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina Ciocanu committed Dec 31, 2023
1 parent 4267145 commit dd6dfe4
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 30 deletions.
28 changes: 18 additions & 10 deletions src/components/MediaCollection/createMediaSessionCacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,42 @@ export default () => {
};

const saveHeartbeat = ({ playerId, heartbeatId }) => {
const mediaSession = mediaSessionCache[playerId];
if (!mediaSession) {
const sessionDetails = mediaSessionCache[playerId];

if (!sessionDetails) {
return;
}
mediaSession.heartbeatId = heartbeatId;

sessionDetails.heartbeatId = heartbeatId;
};

const stopHeartbeat = ({ playerId }) => {
const mediaSession = mediaSessionCache[playerId];
if (!mediaSession) {
const sessionDetails = mediaSessionCache[playerId];

if (!sessionDetails) {
return;
}
clearInterval(mediaSession.heartbeatId);
mediaSession.heartbeatId = null;

clearInterval(sessionDetails.heartbeatId);

sessionDetails.heartbeatId = null;
};

const updateLastTriggeredEventTS = ({ playerId }) => {
const player = mediaSessionCache[playerId];
if (!player) {
const sessionDetails = mediaSessionCache[playerId];

if (!sessionDetails) {
return;
}
player.latestTriggeredEvent = Date.now();

sessionDetails.latestTriggeredEvent = Date.now();
};

const storeSession = ({ playerId, sessionDetails }) => {
if (mediaSessionCache === undefined) {
mediaSessionCache = {};
}

mediaSessionCache[playerId] = sessionDetails;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ import MediaEvents from "./constants/eventTypes";

export default ({ mediaSessionCacheManager }) => {
return ({ playerId, eventType }) => {
if (!playerId) {
return;
}
if (
eventType === MediaEvents.SESSION_COMPLETE ||
eventType === MediaEvents.SESSION_END
) {
mediaSessionCacheManager.stopHeartbeat({ playerId });
}

mediaSessionCacheManager.updateLastTriggeredEventTS({
playerId
});
mediaSessionCacheManager.updateLastTriggeredEventTS({ playerId });
};
};
2 changes: 2 additions & 0 deletions src/components/MediaCollection/validateMediaEventOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default ({ options }) => {
mediaCollection: objectOf(anything())
}).required()
}).required(),

objectOf({
xdm: objectOf({
eventType: string().required(),
Expand All @@ -39,6 +40,7 @@ export default ({ options }) => {
}).required()
}).required()
],

"Error validating the sendMediaEvent command options."
);

Expand Down
2 changes: 2 additions & 0 deletions src/components/MediaCollection/validateMediaSessionOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default ({ options }) => {
})
})
}).required(),

objectOf({
xdm: objectOf({
mediaCollection: objectOf({
Expand All @@ -40,6 +41,7 @@ export default ({ options }) => {
})
}).required()
],

"Error validating the createMediaSession command options."
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ governing permissions and limitations under the License.

import createHeartbeatEngine from "../../../../../src/components/MediaCollection/createHeartbeatEngine";

describe("createHeartbeatEngine", () => {
describe("MediaCollection::createHeartbeatEngine", () => {
let config;
let mediaEventManager;
let mediaSessionCacheManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ governing permissions and limitations under the License.

import createMediaEventManager from "../../../../../src/components/MediaCollection/createMediaEventManager";

describe("createMediaEventManager", () => {
describe("MediaCollection::createMediaEventManager", () => {
let config;
let eventManager;
let consent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ governing permissions and limitations under the License.

import createMediaRequest from "../../../../../src/components/MediaCollection/createMediaRequest";

describe("createMediaRequest", () => {
describe("MediaCollection::createMediaRequest", () => {
it("should call createRequest with correct parameters", () => {
const mediaRequestPayload = {}; // replace with valid payload
const action = "testAction";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,70 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import createMediaSessionCacheManager from "../../../../../src/components/MediaCollection/createMediaSessionCacheManager";

describe("MediaCollection::createMediaSessionCacheManager", () => {
let mediaSessionCacheManager;

beforeEach(() => {
mediaSessionCacheManager = createMediaSessionCacheManager();
});

it("getSession should return correct session", () => {
const playerId = "player1";
const sessionDetails = { id: "session1" };
mediaSessionCacheManager.storeSession({ playerId, sessionDetails });

const result = mediaSessionCacheManager.getSession(playerId);

expect(result).toEqual(sessionDetails);
});

it("stopHeartbeat should stop the heartbeat", () => {
const playerId = "player1";
const sessionDetails = { id: "session1", heartbeatId: 1 };

mediaSessionCacheManager.storeSession({ playerId, sessionDetails });

const result = mediaSessionCacheManager.getSession(playerId);

mediaSessionCacheManager.stopHeartbeat({ playerId });

expect(result.heartbeatId).toEqual(null);
});

it("updateLastTriggeredEventTS should update the timestamp", () => {
const playerId = "player1";
const now = Date.now();
const sessionDetails = { id: "session1", heartbeatId: 1 };

mediaSessionCacheManager.storeSession({ playerId, sessionDetails });
mediaSessionCacheManager.updateLastTriggeredEventTS({ playerId });

const session = mediaSessionCacheManager.getSession(playerId);

expect(session.latestTriggeredEvent).toBeGreaterThanOrEqual(now);
});

it("storeSession should store the session", () => {
const playerId = "player1";
const sessionDetails = { id: "session1" };
mediaSessionCacheManager.storeSession({ playerId, sessionDetails });

const session = mediaSessionCacheManager.getSession(playerId);

expect(session).toEqual(sessionDetails);
});

it("saveHeartbeat should save the heartbeat", () => {
const playerId = "player1";
const sessionDetails = { id: "session1" };
mediaSessionCacheManager.storeSession({ playerId, sessionDetails });
mediaSessionCacheManager.saveHeartbeat({ playerId, heartbeatId: 1 });

const session = mediaSessionCacheManager.getSession(playerId);

expect(session.heartbeatId).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import createUpdateMediaSessionState from "../../../../../src/components/MediaCollection/createUpdateMediaSessionState";
import MediaEvents from "../../../../../src/components/MediaCollection/constants/eventTypes";

describe("MediaCollection::createUpdateMediaSessionState", () => {
let mediaSessionCacheManager;
let updateSessionState;

beforeEach(() => {
mediaSessionCacheManager = jasmine.createSpyObj(
"mediaSessionCacheManager",
["stopHeartbeat", "updateLastTriggeredEventTS"]
);
updateSessionState = createUpdateMediaSessionState({
mediaSessionCacheManager
});
});

it("should stop the heart beat when session completes", () => {
updateSessionState({
playerId: "playerId",
eventType: MediaEvents.SESSION_COMPLETE
});

expect(mediaSessionCacheManager.stopHeartbeat).toHaveBeenCalled();
expect(
mediaSessionCacheManager.updateLastTriggeredEventTS
).toHaveBeenCalled();
});

it("should update the last event when session is ongoing", () => {
updateSessionState({ playerId: "playerId", eventType: MediaEvents.PLAY });

expect(mediaSessionCacheManager.stopHeartbeat).not.toHaveBeenCalled();
expect(
mediaSessionCacheManager.updateLastTriggeredEventTS
).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,56 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import validateMediaEventOptions from "../../../../../src/components/MediaCollection/validateMediaEventOptions";

describe("MediaCollection::validateMediaEventOptions", () => {
it("should not fail when payerId and xdm are used", () => {
const options = {
playerId: "playerId",
xdm: {
eventType: "eventType",
mediaCollection: {
playhead: 0,
sessionID: "sessionID"
}
}
};

expect(() => {
validateMediaEventOptions({ options });
}).not.toThrowError();
});

it("should not fail when xdm with playhead is used", () => {
const options = {
xdm: {
eventType: "eventType",
mediaCollection: {
playhead: 0,
sessionID: "sessionID"
}
}
};

expect(() => {
validateMediaEventOptions({ options });
}).not.toThrowError();
});

it("should throw an error when invalid options are passed", () => {
const options = {
xdm: {
eventType: "eventType",
mediaCollection: {
playhead: "0",
sessionID: "sessionID"
}
}
};

expect(() => {
validateMediaEventOptions({ options });
}).toThrowError();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,56 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import validateMediaSessionOptions from "../../../../../src/components/MediaCollection/validateMediaSessionOptions";

describe("MediaCollection::validateMediaSessionOptions", () => {
it("should not fail when playerId, callback and xdm are used", () => {
const options = {
playerId: "playerId",
onBeforeMediaEvent: () => {},
xdm: {
eventType: "eventType",
mediaCollection: {
sessionDetails: {}
}
}
};

expect(() => {
validateMediaSessionOptions({ options });
}).not.toThrowError();
});

it("should not fail when playerId, callback and xdm are used", () => {
const options = {
xdm: {
eventType: "eventType",
mediaCollection: {
playhead: 0,
sessionDetails: {}
}
}
};

expect(() => {
validateMediaSessionOptions({ options });
}).not.toThrowError();
});

it("should throw an error when invalid options are passed", () => {
const options = {
xdm: {
eventType: "eventType",
mediaCollection: {
playhead: "0",
sessionID: "sessionID"
}
}
};

expect(() => {
validateMediaSessionOptions({ options });
}).toThrowError();
});
});

0 comments on commit dd6dfe4

Please sign in to comment.