From a6d87aaa9b5c49836613052679d77480d0f4faa6 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 20 Feb 2016 10:16:55 +0100 Subject: [PATCH] add tests for /export/history.csv --- .../dataexport/GET-export_history.csv.test.js | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/test/api/v3/integration/dataexport/GET-export_history.csv.test.js b/test/api/v3/integration/dataexport/GET-export_history.csv.test.js index 93b1e09e11..03b0a06832 100644 --- a/test/api/v3/integration/dataexport/GET-export_history.csv.test.js +++ b/test/api/v3/integration/dataexport/GET-export_history.csv.test.js @@ -1,3 +1,48 @@ // TODO how to test this route since it uses session authentication? +import { + generateUser, +} from '../../../../helpers/api-v3-integration.helper'; +import { + updateDocument, +} from '../../../../helpers/mongo'; +import moment from 'moment'; -describe('GET /export/history.csv', () => {}); +describe.only('GET /export/history.csv', () => { + it('should return a valid CSV file with tasks history data', async () => { + let user = await generateUser(); + let tasks = await user.post('/tasks/user', [ + {type: 'habit', text: 'habit 1'}, + {type: 'daily', text: 'daily 1'}, + {type: 'habit', text: 'habit 2'}, + {type: 'todo', text: 'todo 1'}, + ]); + + // score all the tasks twice + await Promise.all(tasks.map(task => { + return user.post(`/tasks/${task._id}/score/up`); + })); + await Promise.all(tasks.map(task => { + return user.post(`/tasks/${task._id}/score/up`); + })); + + // adding an history entry to daily 1 manually because cron didn't run yet + await updateDocument('tasks', tasks[1], { + history: {value: 3.2, date: Number(new Date())}, + }); + + // get updated tasks + tasks = await Promise.all(tasks.map(task => { + return user.get(`/tasks/${task._id}`); + })); + + let res = await user.get(`/export/history.csv`); + let splitRes = res.split('\n'); + expect(splitRes[0]).to.equal('Task Name,Task ID,Task Type,Date,Value'); + expect(splitRes[1]).to.equal(`habit 1,${tasks[0]._id},habit,${moment(tasks[0].history[0].date).format('YYYY-MM-DD HH:mm:ss')},${tasks[0].history[0].value}`); + expect(splitRes[2]).to.equal(`habit 1,${tasks[0]._id},habit,${moment(tasks[0].history[1].date).format('YYYY-MM-DD HH:mm:ss')},${tasks[0].history[1].value}`); + expect(splitRes[3]).to.equal(`daily 1,${tasks[1]._id},daily,${moment(tasks[1].history[0].date).format('YYYY-MM-DD HH:mm:ss')},${tasks[1].history[0].value}`); + expect(splitRes[4]).to.equal(`habit 2,${tasks[2]._id},habit,${moment(tasks[2].history[0].date).format('YYYY-MM-DD HH:mm:ss')},${tasks[2].history[0].value}`); + expect(splitRes[5]).to.equal(`habit 2,${tasks[2]._id},habit,${moment(tasks[2].history[1].date).format('YYYY-MM-DD HH:mm:ss')},${tasks[2].history[1].value}`); + expect(splitRes[6]).to.equal(''); + }); +});