@aller/blink
Version:
A library for tracking user behaviour.
170 lines (161 loc) • 4.87 kB
text/typescript
import activeTimeReducer from '../active-time';
import {
PAGE_INIT,
ARTICLE_ACTIVITY_START,
ARTICLE_ACTIVITY_STOP,
} from '../../actions';
describe('activeTime reducer', () => {
describe('pageInit', () => {
it('should flush state', () => {
const state = {
'dagbladet.no/345': {
activity: [{ type: 'start', time: new Date(5) }],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
};
const action = {
type: PAGE_INIT,
payload: { url: 'https://dagbladet.no/' },
};
expect(activeTimeReducer(state, action)).toEqual({});
});
});
describe('pageActivityStart', () => {
it('should start event into empty state', () => {
const action = {
type: ARTICLE_ACTIVITY_START,
payload: {
id: 'dagbladet.no/345',
time: new Date(5),
url: 'https://www.dagbladet.no/a/345',
},
};
expect(activeTimeReducer({}, action)).toEqual({
'dagbladet.no/345': {
activity: [
{
type: 'start',
time: new Date(5),
},
],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
});
});
it('should add start event at end of prev state if last element is start', () => {
const action = {
type: ARTICLE_ACTIVITY_START,
payload: {
id: 'dagbladet.no/345',
time: new Date(10),
url: 'https://www.dagbladet.no/a/345',
},
};
const prevState = {
'dagbladet.no/345': {
activity: [{ type: 'start', time: new Date(3) }],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
};
const expected = {
'dagbladet.no/345': {
activity: [
{ type: 'start', time: new Date(3) },
{ type: 'start', time: new Date(10) },
],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
};
expect(activeTimeReducer(prevState, action)).toEqual(expected);
});
it('should add start event at end of prev state if last element is stop', () => {
const action = {
type: ARTICLE_ACTIVITY_START,
payload: {
id: 'dagbladet.no/345',
time: new Date(10),
url: 'https://www.dagbladet.no/a/345',
},
};
const prevState = {
'dagbladet.no/345': { activity: [{ type: 'stop', time: new Date(3) }] },
};
expect(activeTimeReducer(prevState, action)).toEqual({
'dagbladet.no/345': {
activity: [
{ type: 'stop', time: new Date(3) },
{ type: 'start', time: new Date(10) },
],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
});
});
});
describe('pageActivityStop', () => {
it('should start event into empty state', () => {
const action = {
type: ARTICLE_ACTIVITY_STOP,
payload: {
id: 'dagbladet.no/345',
time: new Date(5),
url: 'https://www.dagbladet.no/a/345',
},
};
expect(activeTimeReducer({}, action)).toEqual({
'dagbladet.no/345': {
activity: [{ type: 'stop', time: new Date(5) }],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
});
});
it('should not add stop event at end of prev state if last element is stop', () => {
const action = {
type: ARTICLE_ACTIVITY_STOP,
payload: {
id: 'dagbladet.no/345',
time: new Date(10),
url: 'https://www.dagbladet.no/a/345',
},
};
const prevState = {
'dagbladet.no/345': {
activity: [{ type: 'stop', time: new Date(3) }],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
};
expect(activeTimeReducer(prevState, action)).toEqual(prevState);
});
it('should add start event at end of prev state if last element is stop', () => {
const action = {
type: ARTICLE_ACTIVITY_STOP,
payload: {
id: 'dagbladet.no/345',
time: new Date(10),
url: 'https://www.dagbladet.no/a/345',
},
};
const prevState = {
'dagbladet.no/345': {
activity: [{ type: 'start', time: new Date(3) }],
},
};
expect(activeTimeReducer(prevState, action)).toEqual({
'dagbladet.no/345': {
activity: [
{ type: 'start', time: new Date(3) },
{ type: 'stop', time: new Date(10) },
],
url: 'https://www.dagbladet.no/a/345',
id: 'dagbladet.no/345',
},
});
});
});
});