From 0d3c643b503a797fb8f9ae96f06a419518c3c158 Mon Sep 17 00:00:00 2001 From: Jonathan Hardy Date: Thu, 5 Sep 2024 15:50:04 +0100 Subject: [PATCH] moved getter functionality into new quals util --- .../models/workerAvailableQualifications.js | 12 ++------ .../unit/utils/qualificationsUtils.spec.js | 28 +++++++++++++++++++ backend/server/utils/qualificationsUtils.js | 13 +++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 backend/server/test/unit/utils/qualificationsUtils.spec.js create mode 100644 backend/server/utils/qualificationsUtils.js diff --git a/backend/server/models/workerAvailableQualifications.js b/backend/server/models/workerAvailableQualifications.js index 2619da3fb4..83a6dc7e0b 100644 --- a/backend/server/models/workerAvailableQualifications.js +++ b/backend/server/models/workerAvailableQualifications.js @@ -1,4 +1,5 @@ /* jshint indent: 2 */ +const { formatQualificationTitle } = require('../utils/qualificationsUtils'); module.exports = function(sequelize, DataTypes) { const WorkerQualifications = sequelize.define('workerAvailableQualifications', { @@ -27,16 +28,7 @@ module.exports = function(sequelize, DataTypes) { const titleValue = this.getDataValue('title'); const levelValue = this.getDataValue('level'); - if (levelValue) { - if (titleValue.endsWith(')')) { - const sub = titleValue.substring(0, titleValue.length - 1); - return `${sub}, level ${levelValue})`; - } else { - return `${titleValue} (level ${levelValue})`; - } - } - - return titleValue; + return formatQualificationTitle(titleValue, levelValue); } }, level: { diff --git a/backend/server/test/unit/utils/qualificationsUtils.spec.js b/backend/server/test/unit/utils/qualificationsUtils.spec.js new file mode 100644 index 0000000000..97c7bd4458 --- /dev/null +++ b/backend/server/test/unit/utils/qualificationsUtils.spec.js @@ -0,0 +1,28 @@ +const expect = require('chai').expect; + +const { formatQualificationTitle } = require('../../../utils/qualificationsUtils'); +const { registrationWithUser, convertedRegistrationResponse } = require('../mockdata/registration'); + +describe('qualificationsUtils', () => { + describe('formatQualificationTitle', () => { + it('should return the title unchanged when no level is provided', () => { + const title = 'This is a qual'; + const level = null; + + const returnedValue = formatQualificationTitle(title, level); + + expect(returnedValue).to.deep.equal(title); + }); + + describe('level is provided', () => { + it('should return the title with the level in brackets', () => { + const title = 'This is a qual'; + const level = '4'; + + const returnedValue = formatQualificationTitle(title, level); + + expect(returnedValue).to.deep.equal('This is a qual (level 4)'); + }); + }) + }); +}); diff --git a/backend/server/utils/qualificationsUtils.js b/backend/server/utils/qualificationsUtils.js new file mode 100644 index 0000000000..30da2d5055 --- /dev/null +++ b/backend/server/utils/qualificationsUtils.js @@ -0,0 +1,13 @@ + + +module.exports.formatQualificationTitle = (title, level) => { + if (level) { + if (title.endsWith(')')) { + const sub = title.substring(0, title.length - 1); + return `${sub}, level ${level})`; + } else { + return `${title} (level ${level})`; + } + } + return title; +} \ No newline at end of file