@aller/blink
Version:
A library for tracking user behaviour.
214 lines (198 loc) • 5.23 kB
text/typescript
import videoReducer from '../video';
import { PAGE_INIT, VIDEO_PLAY, VIDEO_STOP } from '../../actions';
import { VideoTimeEvent } from '../../utils/video-event-time';
describe('Video reducer', () => {
it('should have correct default state', () => {
const action = {
type: 'INVALID_ACTION',
};
expect(videoReducer(undefined, action)).toEqual({
events: [],
});
});
describe('PAGE_INIT', () => {
it('should flush state', () => {
const existingEvents: VideoTimeEvent[] = [
{
time: new Date(5),
type: 'start',
videoId: 'test-video-id',
muted: false,
reason: 'autostart',
position: 10,
volume: 99,
playerId: 'test-player-id',
},
];
const action = {
type: PAGE_INIT,
payload: { url: 'https://dagbladet.no/' },
};
expect(videoReducer({ events: existingEvents }, action)).toEqual({
events: [],
});
});
});
describe('VIDEO_PLAY', () => {
it('should add a video start event to empty state', () => {
const action = {
type: VIDEO_PLAY,
payload: {
time: new Date(5),
videoId: 'test-video-id',
muted: false,
reason: 'autostart',
position: 10,
volume: 99,
playerId: 'test-player-id',
},
};
const expectedEvents: VideoTimeEvent[] = [
{
time: new Date(5),
type: 'start',
videoId: 'test-video-id',
muted: false,
reason: 'autostart',
position: 10,
volume: 99,
playerId: 'test-player-id',
},
];
expect(videoReducer({ events: [] }, action)).toEqual({
events: expectedEvents,
});
});
it('should add a video start event to existing state', () => {
const action = {
type: VIDEO_PLAY,
payload: {
time: new Date(5),
videoId: 'test-video-id',
reason: 'interaction',
muted: false,
volume: 90,
position: 50,
playerId: 'test-player-id',
},
};
const events: VideoTimeEvent[] = [
{
time: new Date(3),
type: 'stop',
videoId: 'test-video-id',
reason: 'complete',
muted: true,
volume: 50,
position: 20,
playerId: 'test-player-id',
},
];
expect(videoReducer({ events }, action)).toEqual({
events: [
{
time: new Date(3),
type: 'stop',
videoId: 'test-video-id',
reason: 'complete',
muted: true,
volume: 50,
position: 20,
playerId: 'test-player-id',
},
{
time: new Date(5),
type: 'start',
videoId: 'test-video-id',
reason: 'interaction',
muted: false,
volume: 90,
position: 50,
playerId: 'test-player-id',
},
],
});
});
});
describe('VIDEO_STOP', () => {
it('should add a video start event to empty state', () => {
const action = {
type: VIDEO_STOP,
payload: {
time: new Date(5),
videoId: 'test-video-id',
muted: false,
reason: 'pause',
position: 10,
volume: 99,
playerId: 'test-player-id',
},
};
const expectedEvents: VideoTimeEvent[] = [
{
time: new Date(5),
type: 'stop',
videoId: 'test-video-id',
muted: false,
reason: 'pause',
position: 10,
volume: 99,
playerId: 'test-player-id',
},
];
expect(videoReducer({ events: [] }, action)).toEqual({
events: expectedEvents,
});
});
it('should add a video start event to existing state', () => {
const action = {
type: VIDEO_STOP,
payload: {
time: new Date(5),
videoId: 'test-video-id',
reason: 'complete',
muted: false,
volume: 90,
position: 50,
playerId: 'test-player-id',
},
};
const events: VideoTimeEvent[] = [
{
time: new Date(3),
type: 'start',
videoId: 'test-video-id',
reason: 'autostart',
muted: true,
volume: 50,
position: 20,
playerId: 'test-player-id',
},
];
expect(videoReducer({ events }, action)).toEqual({
events: [
{
time: new Date(3),
type: 'start',
videoId: 'test-video-id',
reason: 'autostart',
muted: true,
volume: 50,
position: 20,
playerId: 'test-player-id',
},
{
time: new Date(5),
type: 'stop',
videoId: 'test-video-id',
reason: 'complete',
muted: false,
volume: 90,
position: 50,
playerId: 'test-player-id',
},
],
});
});
});
});