Skip to content

Commit

Permalink
feat(core): simple active tasks implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
espy authored and AlbaHerrerias committed May 4, 2022
1 parent d5bb7bd commit 0680c0a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/node_modules/pouchdb-core/src/setup.js
@@ -1,6 +1,7 @@
'use strict';

import PouchDB from './constructor';
import { v4 as uuidv4 } from 'uuid';
import EE from 'events';
import { fetch } from 'pouchdb-fetch';
import { createClass } from './utils';
Expand Down Expand Up @@ -121,4 +122,60 @@ PouchDB.fetch = function (url, opts) {
return fetch(url, opts);
};

function ActiveTasks() {
this.tasks = [];
}

ActiveTasks.prototype.list = function () {
return this.tasks;
};

ActiveTasks.prototype.add = function (task) {
var newTask = {
name: task.name,
total_items: task.total_items,
id: uuidv4(),
createdAt: new Date().toJSON()
};
this.tasks.push(newTask);
return newTask.id;
};

ActiveTasks.prototype.get = function (id) {
var index = this.tasks.findIndex(function (task) {
return task.id === id;
});
return this.tasks[index];
};

ActiveTasks.prototype.remove = function (id) {
var index = this.tasks.findIndex(function (task) {
return task.id === id;
});
if (index !== -1) {
this.tasks.splice(index, 1);
}
return this.tasks;
};

ActiveTasks.prototype.update = function (id, updatedTask) {
var index = this.tasks.findIndex(function (task) {
return task.id === id;
});
if (index !== -1) {
var mergedTask = {
id: this.tasks[index].id,
name: this.tasks[index].name,
createdAt: this.tasks[index].createdAt,
total_items: updatedTask.total_items || this.tasks[index].total_items,
completed_items: updatedTask.completed_items || this.tasks[index].completed_items,
updatedAt: new Date().toJSON()
};
this.tasks.splice(index, 1, mergedTask);
}
return this.tasks;
};

PouchDB.activeTasks = new ActiveTasks();

export default PouchDB;
64 changes: 64 additions & 0 deletions tests/integration/test.active_tasks.js
@@ -0,0 +1,64 @@
'use strict';

describe('test.active_tasks.js', function () {

afterEach(function (done) {
PouchDB.activeTasks.tasks = []
return done();
});

it('Can add a task', function () {
const task1 = {name: 'lol', total_items: 12}
const id1 = PouchDB.activeTasks.add(task1)
id1.should.be.a('string')
});

it('Can get tasks by id', function () {
const task2 = {name: 'wat', total_items: 546}
const id2 = PouchDB.activeTasks.add(task2)
const got2 = PouchDB.activeTasks.get(id2)
got2['id'].should.equal(id2)
});

it('Can get all tasks', function () {
const task1 = {name: 'lol', total_items: 12}
const task2 = {name: 'wat', total_items: 546}
const id1 = PouchDB.activeTasks.add(task1)
const id2 = PouchDB.activeTasks.add(task2)
PouchDB.activeTasks.update(id1, {"completed_items": 2})
PouchDB.activeTasks.update(id2, {"completed_items": 213})
const list = PouchDB.activeTasks.list()
const got2 = PouchDB.activeTasks.get(id2)
list.length.should.equal(2)
list[0]['id'].should.equal(id1)
list[1]['id'].should.equal(id2)
});

it('Can update a task', function () {
const task1 = {name: 'lol', total_items: 12}
const task2 = {name: 'wat', total_items: 546}
const id1 = PouchDB.activeTasks.add(task1)
const id2 = PouchDB.activeTasks.add(task2)
PouchDB.activeTasks.update(id1, {"completed_items": 2})
PouchDB.activeTasks.update(id2, {"completed_items": 213})
const got1 = PouchDB.activeTasks.get(id1)
const got2 = PouchDB.activeTasks.get(id2)
got1['completed_items'].should.equal(2)
got2['completed_items'].should.equal(213)
got2['updatedAt'].should.be.a('string')
});

it('Can remove a task', function () {
const task1 = {name: 'lol', total_items: 12}
const task2 = {name: 'wat', total_items: 546}
const id1 = PouchDB.activeTasks.add(task1)
const id2 = PouchDB.activeTasks.add(task2)
PouchDB.activeTasks.update(id1, {"completed_items": 2})
PouchDB.activeTasks.update(id2, {"completed_items": 213})
PouchDB.activeTasks.remove(id1)
const got2 = PouchDB.activeTasks.get(id2)
PouchDB.activeTasks.tasks.length.should.equal(1)
got2['id'].should.equal(id2)
});

});

0 comments on commit 0680c0a

Please sign in to comment.