diff --git a/__tests__/spec/data-storage.js b/__tests__/spec/data-storage.js new file mode 100644 index 0000000000..e140e385f2 --- /dev/null +++ b/__tests__/spec/data-storage.js @@ -0,0 +1,76 @@ +/* eslint-env jest */ +const request = require('supertest') +const app = require('../../server.js') +const path = require('path') +const fs = require('fs') +const utils = require('../../lib/utils.js') + +jest.setTimeout(30000) + +function readFile (pathFromRoot) { + return fs.readFileSync(path.join(__dirname, '../../' + pathFromRoot), 'utf8') +} +/** + * + */ +describe('The data storage', () => { + // check session-data defaults file exists + + it.skip('file should exist', (done) => { + const sessionDataDefaultsFile = readFile('app/data/session-data-defaults.js') + + request(app) + .get(sessionDataDefaultsFile) + .expect('Content-Type', /application\/javascript; charset=UTF-8/) + // .expect(200) + .end(function (err, res) { + if (err) { + done(err) + } else { + done() + } + }) + }) + + describe('storeData function', () => { + it('should add input data into session data', async () => { + let initialSessionData = { + 'driver-name': 'Dr Emmett Brown' + } + + const inputData = { + 'vehicle-registration': 'OUTATIME' + } + + utils.storeData(inputData, initialSessionData) + + expect(initialSessionData).toEqual({ + 'driver-name': 'Dr Emmett Brown', + 'vehicle-registration': 'OUTATIME' + }) + }) + + it('should merge object into object', async () => { + let initialSessionData = { + vehicle: { + 'driver-name': 'Dr Emmett Brown' + } + } + + const inputData = { + vehicle: { + registration: 'OUTATIME' + } + } + + utils.storeData(inputData, initialSessionData) + + expect(initialSessionData).toEqual({ + vehicle: { + 'driver-name': 'Dr Emmett Brown', + registration: 'OUTATIME' + } + }) + }) + }) +}) diff --git a/lib/utils.js b/lib/utils.js index 068fddf2c4..ac76bd25a0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -220,7 +220,7 @@ exports.matchMdRoutes = function (req, res) { } // Store data from POST body or GET query in session -var storeData = function (input, data) { +function storeData (input, data) { // Value we store to indicate unchecked checkboxes. See `auto-store-data.js` var uncheckedValue = '_unchecked' @@ -257,6 +257,8 @@ var storeData = function (input, data) { } } +exports.storeData = storeData + // Get session default data from file let sessionDataDefaults = {}