Skip to content

Commit

Permalink
ONL-4029 fix: Not modify the existing DEFAULT properties (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesao authored Jan 17, 2020
1 parent 76ce99a commit 940cb2f
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 295 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.1] - 2020-01-16
### Fixes
- Fix to not modify the existing DEFAULT properties.

## [2.0.0] - 2020-01-03
### Fixes
- Upgrade vulnerable versions found on dependencies.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ebury/huha",
"description": "huha.js (Hyperactive Users Hint Analysis) is a JavaScript framework that measures the usability and user experience in an automated way, including the limitations of the model and the best practices.",
"version": "2.0.0",
"version": "2.0.1",
"repository": "github:Ebury/huha",
"license": "MIT",
"scripts": {
Expand Down
16 changes: 6 additions & 10 deletions src/huha.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class HuhaTask {
* - trackOnSegment (Boolean): Indicates if the task needs to be tracked on Segment
*/
constructor(props, options) {
const mergedOptions = Object.assign(DEFAULTS, options);
this.name = props.name;
this.label = props.label || props.name;
this.category = props.category || '';
Expand All @@ -47,9 +46,9 @@ class HuhaTask {
this.start = new Date().getTime();
this.end = null;
this.parentExecId = null;
this.trackOnGoogleAnalytics = mergedOptions.trackOnGoogleAnalytics;
this.trackOnIntercom = mergedOptions.trackOnIntercom;
this.trackOnSegment = mergedOptions.trackOnSegment;
this.trackOnGoogleAnalytics = options.trackOnGoogleAnalytics;
this.trackOnIntercom = options.trackOnIntercom;
this.trackOnSegment = options.trackOnSegment;
if (props.parentTask) {
this.parentTask = props.parentTask;
this.parentExecId = props.parentTask.execId;
Expand Down Expand Up @@ -251,8 +250,6 @@ class HuhaEvent {
* - trackOnSegment (Boolean): Indicates if the task needs to be tracked on Segment
*/
constructor(name, object, action, section, value, task, eventGroup, options) {
const mergedOptions = Object.assign(DEFAULTS, options || {});

this.name = name;
this.object = object;
this.action = action;
Expand All @@ -267,8 +264,8 @@ class HuhaEvent {
this.eventGroup = uuidv1();
}

this.trackOnGoogleAnalytics = mergedOptions.trackOnGoogleAnalytics;
this.trackOnSegment = mergedOptions.trackOnSegment;
this.trackOnGoogleAnalytics = options.trackOnGoogleAnalytics;
this.trackOnSegment = options.trackOnSegment;
}

/**
Expand Down Expand Up @@ -331,8 +328,7 @@ class Huha {
constructor(options) {
this.tasks = [];
this.events = [];

const mergedOptions = Object.assign(DEFAULTS, options || {});
const mergedOptions = { ...DEFAULTS, ...options };
this.trackOnGoogleAnalytics = mergedOptions.trackOnGoogleAnalytics;
this.trackOnIntercom = mergedOptions.trackOnIntercom;
this.trackOnSegment = mergedOptions.trackOnSegment;
Expand Down
308 changes: 308 additions & 0 deletions src/huha.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
import Huha from './huha';

describe('Huha', () => {
it('should be instantiated', () => {
const huha = new Huha();
expect(huha).toBeDefined();
});

it('should be instantiated with the properly options', () => {
const huha = new Huha({
trackOnGoogleAnalytics: false,
trackOnIntercom: false,
trackOnSegment: false,
});
expect(huha.trackOnGoogleAnalytics).toBeFalsy();
expect(huha.trackOnIntercom).toBeFalsy();
expect(huha.trackOnSegment).toBeFalsy();
});

it('should be instantiated with the default properties, passing an empty object as options', () => {
const huha = new Huha({});
expect(huha.trackOnGoogleAnalytics).toBeFalsy();
expect(huha.trackOnIntercom).toBeFalsy();
expect(huha.trackOnSegment).toBeTruthy();
});

describe('HuhaTask', () => {
it('should create a task with the parameters defined', () => {
const huha = new Huha();

// Check the number of the tasks
expect(huha.tasks.length).toBe(0);

const taskProperties = {
name: 'mockTask-1',
label: 'mockLabel',
category: 'mockCategory',
value: 'mockValue',
};
const task = huha.createTask(taskProperties);

// Checking results
expect(huha.tasks.length).toBe(1);
expect(task).toBeDefined();
expect(task.name).toBe('mockTask-1');
expect(task.label).toBe('mockLabel');
expect(task.category).toBe('mockCategory');
expect(task.value).toBe('mockValue');
expect(task.parentExecId).toBeNull();
expect(task.persistent).toBeFalsy();
expect(task.status).toBe('In progress');
});

it('should create a task with the label equal to the task name if it is not defined', () => {
const huha = new Huha();

// Check the number of the tasks
expect(huha.tasks.length).toBe(0);

const taskProperties = {
name: 'mockTask-1',
};
const task = huha.createTask(taskProperties);

// Checking results
expect(huha.tasks.length).toBe(1);
expect(task).toBeDefined();
expect(task.label).toBe('mockTask-1');
});

it('should add one interaction', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask-1',
category: 'mockCategory',
value: 'mockValue',
};
const task = huha.createTask(taskProperties);

// Checking results
expect(task).toBeDefined();
expect(task.effort).toBe(0);
expect(task.errors).toBe(0);

task.addInteraction();

// Checking results
expect(task.effort).toBe(1);
expect(task.errors).toBe(0);
});

it('should add one error', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask-1',
category: 'mockCategory',
value: 'mockValue',
};
const task = huha.createTask(taskProperties);

// Checking results
expect(task).toBeDefined();
expect(task.effort).toBe(0);
expect(task.errors).toBe(0);

task.addError();

// Checking results
expect(task.effort).toBe(0);
expect(task.errors).toBe(1);
});

it('should be completed', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask-1',
category: 'mockCategory',
value: 'mockValue',
};
const task = huha.createTask(taskProperties);

// Mock track function
const mockTrack = jest.fn();
task.track = mockTrack;

expect(task.status).toBe('In progress');

// Completing the task
task.complete();

// Checking results
expect(task.status).toBe('Completed');
expect(mockTrack).toHaveBeenCalled();
});

it('should be abandoned', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask-1',
category: 'mockCategory',
value: 'mockValue',
};
const task = huha.createTask(taskProperties);

// Mock track function
const mockTrack = jest.fn();
task.track = mockTrack;

expect(task.status).toBe('In progress');

// Abandoning the task
task.abandon();

// Checking results
expect(task.status).toBe('Abandoned');
expect(mockTrack).toHaveBeenCalled();
});

it('should create a new persistent root task if it does not exist', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask',
execId: 'id-1',
persistent: true,
};

const task = huha.createTask(taskProperties);

// Checking results
expect(task).toBeDefined();
expect(task.name).toBe('mockTask');
expect(task.category).toBe('');
expect(task.value).toBeUndefined();
expect(task.parentExecId).toBeNull();
expect(task.execId).toBe('id-1');
expect(task.persistent).toBeTruthy();
expect(task.status).toBe('In progress');
expect(task.trackOnGoogleAnalytics).toBeFalsy();
expect(task.trackOnIntercom).toBeFalsy();
expect(task.trackOnSegment).toBeTruthy();
});

it('should create a child task', () => {
const huha = new Huha();

// parentTask
const parentTaskProperties = {
name: 'parentTask',
execId: 'id-1',
persistent: true,
};
const parentTask = huha.createTask(parentTaskProperties);

// childTask
const childTaskProperties = {
name: 'childTask',
parentTask,
execId: 'id-2',
persistent: true,
};
const childTask = huha.createTask(childTaskProperties);

// Checking results
expect(parentTask).toBeDefined();
expect(parentTask.parentExecId).toBeNull();
expect(childTask).toBeDefined();
expect(childTask.parentExecId).toBe('id-1');
});

it('should create a child task and add interaction and errors', () => {
const huha = new Huha();

// parentTask
const parentTaskProperties = {
name: 'parentTask',
execId: 'id-1',
persistent: true,
};
const parentTask = huha.createTask(parentTaskProperties);

// childTask
const childTaskProperties = {
name: 'childTask',
parentTask,
execId: 'id-2',
persistent: true,
};
const childTask = huha.createTask(childTaskProperties);

// Checking results
expect(parentTask).toBeDefined();
expect(childTask).toBeDefined();
expect(childTask.effort).toBe(0);
expect(childTask.parentTask.effort).toBe(0);
expect(childTask.errors).toBe(0);
expect(childTask.parentTask.errors).toBe(0);

childTask.addInteraction();
childTask.addError();

// Checking results
expect(childTask.effort).toBe(1);
expect(childTask.parentTask.effort).toBe(1);
expect(childTask.errors).toBe(1);
expect(childTask.parentTask.errors).toBe(1);
});
});

describe('HuhaEvent', () => {
it('should create an event with the parameters defined', () => {
const huha = new Huha();

// Check the number of the events
expect(huha.events.length).toBe(0);

const event = huha.createEvent(
'mockEvent',
'mockObject',
'mockAction',
'mockSection',
'mockValue',
null,
'id-1',
);

// Checking results
expect(huha.events.length).toBe(1);
expect(event).toBeDefined();
expect(event.name).toBe('mockEvent');
expect(event.object).toBe('mockObject');
expect(event.action).toBe('mockAction');
expect(event.section).toBe('mockSection');
expect(event.value).toBe('mockValue');
expect(event.task).toBeNull();
expect(event.eventGroup).toBe('id-1');
});

it('should create an event with a task associated', () => {
const huha = new Huha();
const taskProperties = {
name: 'mockTask-1',
category: 'mockCategory',
value: 'mockValue',
execId: 'id-1',
};
const task = huha.createTask(taskProperties);
const event = huha.createEvent(
'mockEvent',
'mockObject',
'mockAction',
'mockSection',
'mockValue',
task,
);

// Checking results
expect(event).toBeDefined();
expect(event.name).toBe('mockEvent');
expect(event.object).toBe('mockObject');
expect(event.action).toBe('mockAction');
expect(event.section).toBe('mockSection');
expect(event.value).toBe('mockValue');
expect(event.task).toBeDefined();
expect(task.execId).toBe('id-1');
expect(event.eventGroup).toBe('id-1');
});
});
});
Loading

0 comments on commit 940cb2f

Please sign in to comment.