diff --git a/packages/node_modules/pouchdb-core/src/setup.js b/packages/node_modules/pouchdb-core/src/setup.js index b496fd8eff..64bf1a129c 100644 --- a/packages/node_modules/pouchdb-core/src/setup.js +++ b/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'; @@ -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; diff --git a/tests/integration/test.active_tasks.js b/tests/integration/test.active_tasks.js new file mode 100644 index 0000000000..15eb0c781c --- /dev/null +++ b/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) + }); + +});