hmpo-countries-lib
Advanced tools
Comparing version 4.0.3 to 5.0.0
@@ -32,3 +32,5 @@ 'use strict'; | ||
this._countryCache.on('change', this._indexCountries.bind(this)); | ||
this.on('change', this._indexCountries.bind(this)); | ||
this.on('fail', this.onFail.bind(this)); | ||
this.on('error', this.onError.bind(this)); | ||
} | ||
@@ -137,4 +139,56 @@ | ||
} | ||
sortCountryList(list) { | ||
list = _.sortBy(list, 'displayName'); | ||
let gb = _.find(list, {countryCode: 'GB'}); | ||
if (gb) { | ||
list = _.reject(list, {countryCode: 'GB'}); | ||
list.unshift(gb); | ||
} | ||
return list; | ||
} | ||
dropdownList(list, isWelsh) { | ||
list = this.sortCountryList(list); | ||
return list.map(i => ({ | ||
value: i.countryCode, | ||
text: isWelsh ? i.displayNameWelsh : i.displayName, | ||
label: isWelsh ? i.displayNameWelsh : i.displayName | ||
})); | ||
} | ||
dropdownListBirthCountries(isWelsh) { | ||
return this.dropdownList(this.getBirthCountries(), isWelsh); | ||
} | ||
dropdownListOverseasBirthCountries(isWelsh) { | ||
return this.dropdownList(this.getOverseasBirthCountries(), isWelsh); | ||
} | ||
dropdownListResidenceCountries(isWelsh) { | ||
return this.dropdownList(this.getResidenceCountries(), isWelsh); | ||
} | ||
dropdownListOverseasResidenceCountries(isWelsh) { | ||
return this.dropdownList(this.getOverseasResidenceCountries(), isWelsh); | ||
} | ||
onFail(err, data, settings, statusCode, responseTime) { | ||
const errorText = (err && err.message) || (data && data.error) || ''; | ||
this._countryCache.logger.outbound('Countries CachedModel request failed :outVerb :outRequest :outResponseCode :outError', { | ||
outVerb: settings.method, | ||
outRequest: settings.url, | ||
outResponseCode: statusCode, | ||
outResponseTime: responseTime, | ||
outError: errorText, | ||
outErrorBody: this._countryCache.logger.trimHtml(err.body) | ||
}); | ||
} | ||
onError(err) { | ||
this._countryCache.logger.outbound('Countries CachedModel request error :err.message', err); | ||
} | ||
} | ||
module.exports = CountriesCachedModel; |
{ | ||
"name": "hmpo-countries-lib", | ||
"version": "4.0.3", | ||
"version": "5.0.0", | ||
"description": "Country lists", | ||
@@ -21,3 +21,3 @@ "main": "./lib", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -29,4 +29,4 @@ "bugs": { | ||
"dependencies": { | ||
"debug": "^4.3.1", | ||
"hmpo-cached-model": "^3.0.3", | ||
"debug": "^4.3.3", | ||
"hmpo-cached-model": "^4.0.0", | ||
"underscore": "^1.13.1" | ||
@@ -36,8 +36,8 @@ }, | ||
"chai": "^4.3.4", | ||
"eslint": "^7.26.0", | ||
"mocha": "^8.4.0", | ||
"eslint": "^8.3.0", | ||
"mocha": "^9.1.3", | ||
"nyc": "^15.1.0", | ||
"proxyquire": "^2.1.3", | ||
"sinon": "^10.0.0", | ||
"sinon-chai": "^3.6.0" | ||
"sinon": "^12.0.1", | ||
"sinon-chai": "^3.7.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "nyc": { |
@@ -11,5 +11,4 @@ 'use strict'; | ||
const CountriesCachedModel = require('../../lib'); | ||
const HmpoCachedModel = require('hmpo-cached-model'); | ||
let unitedKingdom = { | ||
const unitedKingdom = { | ||
countryCode: 'GB', | ||
@@ -20,2 +19,3 @@ countryNameSlug: 'united-kingdom', | ||
displayName: 'United Kingdom', | ||
displayNameWelsh: 'Welsh United Kingdom', | ||
channel: 'ONLINE', | ||
@@ -25,3 +25,3 @@ contentType: 1, | ||
}; | ||
let foo = { | ||
const foo = { | ||
countryCode: 'FO', | ||
@@ -32,2 +32,3 @@ countryNameSlug: 'foo', | ||
displayName: 'Foo', | ||
displayNameWelsh: 'Welsh Foo', | ||
channel: 'ONLINE', | ||
@@ -37,3 +38,3 @@ contentType: 2, | ||
}; | ||
let bar = { | ||
const bar = { | ||
countryCode: 'BA', | ||
@@ -44,2 +45,3 @@ countryNameSlug: 'bar', | ||
displayName: 'Bar', | ||
displayNameWelsh: 'Welsh Bar', | ||
channel: 'NA', | ||
@@ -49,3 +51,3 @@ contentType: 7, | ||
}; | ||
let narnia = { | ||
const narnia = { | ||
countryCode: 'NA', | ||
@@ -56,2 +58,3 @@ countryNameSlug: 'narnia', | ||
displayName: 'Narnia', | ||
displayNameWelsh: 'Welsh Narnia', | ||
channel: 'ONLINE', | ||
@@ -62,3 +65,3 @@ contentType: 7, | ||
let countries = [unitedKingdom, foo, bar, narnia]; | ||
const countries = [foo, unitedKingdom, bar, narnia]; | ||
@@ -70,6 +73,2 @@ describe('CountriesCachedModel', () => { | ||
clock = sinon.useFakeTimers(1234567890000); | ||
sinon.stub(HmpoCachedModel.prototype, 'start'); | ||
sinon.stub(HmpoCachedModel.prototype, 'stop'); | ||
sinon.stub(HmpoCachedModel.prototype, 'on'); | ||
sinon.stub(HmpoCachedModel.prototype, 'get'); | ||
@@ -87,2 +86,6 @@ stubs = { | ||
countryCache: { | ||
logger: { | ||
outbound: sinon.stub(), | ||
trimHtml: sinon.stub().returnsArg(0) | ||
}, | ||
start: sinon.stub(), | ||
@@ -116,6 +119,2 @@ stop: sinon.stub(), | ||
afterEach(() => { | ||
HmpoCachedModel.prototype.start.restore(); | ||
HmpoCachedModel.prototype.stop.restore(); | ||
HmpoCachedModel.prototype.on.restore(); | ||
HmpoCachedModel.prototype.get.restore(); | ||
clock.restore(); | ||
@@ -154,5 +153,7 @@ }); | ||
it('should attach indexers to cache change events', () => { | ||
stubs.countryCache.on.should.have.been.calledOnce; | ||
it('should attach loggers to failures, and indexers to cache change events', () => { | ||
stubs.countryCache.on.should.have.been.calledThrice; | ||
stubs.countryCache.on.should.have.been.calledWithExactly('change', sinon.match.func); | ||
stubs.countryCache.on.should.have.been.calledWithExactly('fail', sinon.match.func); | ||
stubs.countryCache.on.should.have.been.calledWithExactly('error', sinon.match.func); | ||
}); | ||
@@ -362,2 +363,3 @@ }); | ||
displayName: 'United Kingdom', | ||
displayNameWelsh: 'Welsh United Kingdom', | ||
status: 'ACTIVE', | ||
@@ -388,2 +390,3 @@ channel: 'ONLINE', | ||
displayName: 'United Kingdom', | ||
displayNameWelsh: 'Welsh United Kingdom', | ||
countryOfBirthFlag: true, | ||
@@ -475,3 +478,3 @@ channel: 'ONLINE', | ||
instance._residenceCountries.should.deep.equal([ | ||
unitedKingdom, foo | ||
foo, unitedKingdom | ||
]); | ||
@@ -531,2 +534,159 @@ }); | ||
}); | ||
describe('dropdownList', () => { | ||
it('turns a list of countries into sorted drop down select box options with GB at the top', () => { | ||
const items = instance.dropdownList(countries, false); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'United Kingdom', label: 'United Kingdom' }, | ||
{ value: 'BA', text: 'Bar', label: 'Bar' }, | ||
{ value: 'FO', text: 'Foo', label: 'Foo' }, | ||
{ value: 'NA', text: 'Narnia', label: 'Narnia' } | ||
]); | ||
}); | ||
it('turns a list of countries into sorted drop down select box options with GB at the top using Welsh displayNames', () => { | ||
const items = instance.dropdownList(countries, true); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'Welsh United Kingdom', label: 'Welsh United Kingdom' }, | ||
{ value: 'BA', text: 'Welsh Bar', label: 'Welsh Bar' }, | ||
{ value: 'FO', text: 'Welsh Foo', label: 'Welsh Foo' }, | ||
{ value: 'NA', text: 'Welsh Narnia', label: 'Welsh Narnia' } | ||
]); | ||
}); | ||
}); | ||
describe('dropdownListBirthCountries', () => { | ||
it('turns a list of countries into drop down select box options', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListBirthCountries(); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'United Kingdom', label: 'United Kingdom' }, | ||
{ value: 'BA', text: 'Bar', label: 'Bar' } | ||
]); | ||
}); | ||
it('turns a list of countries into drop down select box options in welsh', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListBirthCountries(true); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'Welsh United Kingdom', label: 'Welsh United Kingdom' }, | ||
{ value: 'BA', text: 'Welsh Bar', label: 'Welsh Bar' } | ||
]); | ||
}); | ||
}); | ||
describe('dropdownListOverseasBirthCountries', () => { | ||
it('turns a list of countries into drop down select box options', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListOverseasBirthCountries(); | ||
items.should.deep.equal([ | ||
{ value: 'BA', text: 'Bar', label: 'Bar' } | ||
]); | ||
}); | ||
it('turns a list of countries into drop down select box options in welsh', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListOverseasBirthCountries(true); | ||
items.should.deep.equal([ | ||
{ value: 'BA', text: 'Welsh Bar', label: 'Welsh Bar' } | ||
]); | ||
}); | ||
}); | ||
describe('dropdownListResidenceCountries', () => { | ||
it('turns a list of countries into drop down select box options', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListResidenceCountries(); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'United Kingdom', label: 'United Kingdom' }, | ||
{ value: 'FO', text: 'Foo', label: 'Foo' } | ||
]); | ||
}); | ||
it('turns a list of countries into drop down select box options in welsh', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListResidenceCountries(true); | ||
items.should.deep.equal([ | ||
{ value: 'GB', text: 'Welsh United Kingdom', label: 'Welsh United Kingdom' }, | ||
{ value: 'FO', text: 'Welsh Foo', label: 'Welsh Foo' } | ||
]); | ||
}); | ||
}); | ||
describe('dropdownListOverseasResidenceCountries', () => { | ||
it('turns a list of countries into drop down select box options', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListOverseasResidenceCountries(); | ||
items.should.deep.equal([ | ||
{ value: 'FO', text: 'Foo', label: 'Foo' } | ||
]); | ||
}); | ||
it('turns a list of countries into drop down select box options in welsh', () => { | ||
instance._indexCountries(); | ||
const items = instance.dropdownListOverseasResidenceCountries(true); | ||
items.should.deep.equal([ | ||
{ value: 'FO', text: 'Welsh Foo', label: 'Welsh Foo' } | ||
]); | ||
}); | ||
}); | ||
describe('onFail logger', () => { | ||
let settings; | ||
beforeEach(() => { | ||
settings = { | ||
method: 'GET', | ||
url: 'http://example.com/test/path' | ||
}; | ||
}); | ||
it('should log the returned error message', () => { | ||
const err = new Error('test'); | ||
err.body = '<body><tag>error text</tag></body>'; | ||
instance.onFail(err, {}, settings, 123, 1234567); | ||
stubs.countryCache.logger.outbound.should.have.been.calledWithExactly( | ||
'Countries CachedModel request failed :outVerb :outRequest :outResponseCode :outError', | ||
{ | ||
outVerb: 'GET', | ||
outRequest: 'http://example.com/test/path', | ||
outResponseCode: 123, | ||
outResponseTime: 1234567, | ||
outError: 'test', | ||
outErrorBody: '<body><tag>error text</tag></body>' | ||
} | ||
); | ||
}); | ||
it('should log the data error message', () => { | ||
instance.onFail({}, { error: 'test' }, settings, 123, 1234567); | ||
stubs.countryCache.logger.outbound.args[0][1].outError.should.equal('test'); | ||
}); | ||
it('should log an empty string if no error is available', () => { | ||
instance.onFail({}, {}, settings, 123, 1234567); | ||
stubs.countryCache.logger.outbound.args[0][1].outError.should.equal(''); | ||
}); | ||
}); | ||
describe('onError logger', () => { | ||
it('should log the returned error message', () => { | ||
let err = new Error('test'); | ||
instance.onError(err); | ||
stubs.countryCache.logger.outbound.should.have.been.calledWithExactly( | ||
'Countries CachedModel request error :err.message', | ||
err | ||
); | ||
}); | ||
}); | ||
}); |
33629
740
+ Added@sindresorhus/is@4.6.0(transitive)
+ Added@szmarczak/http-timer@4.0.6(transitive)
+ Added@tootallnate/once@2.0.0(transitive)
+ Added@types/cacheable-request@6.0.3(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Added@types/keyv@3.1.4(transitive)
+ Added@types/node@22.13.5(transitive)
+ Added@types/responselike@1.0.3(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedcacheable-lookup@5.0.4(transitive)
+ Addedcacheable-request@7.0.4(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedgot@11.8.6(transitive)
+ Addedhmpo-cached-model@4.0.5(transitive)
+ Addedhmpo-model@4.3.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp-proxy-agent@5.0.0(transitive)
+ Addedhttp2-wrapper@1.0.3(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addedmimic-response@1.0.13.1.0(transitive)
+ Addednormalize-url@6.1.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@2.1.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@2.0.1(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhmpo-cached-model@3.0.3(transitive)
- Removedhmpo-model@3.2.2(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
Updateddebug@^4.3.3
Updatedhmpo-cached-model@^4.0.0