Skip to content

Commit fb8362c

Browse files
committed
Added tests for NotFoundError
1 parent f0fd287 commit fb8362c

5 files changed

Lines changed: 30 additions & 5 deletions

File tree

.jshintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,4 @@
244244
"globals": {
245245
// "sample": false
246246
}
247-
}
247+
}

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function map(resultSet, maps, mapId, columnPrefix) {
4040
* @throws {NotFoundError} if object is not found and isRequired is true
4141
*/
4242
function mapOne(resultSet, maps, mapId, columnPrefix, isRequired) {
43+
44+
// Set up a default value for isRequired
4345
if (isRequired === undefined) {
4446
isRequired = true;
4547
}
@@ -68,8 +70,8 @@ function mapOne(resultSet, maps, mapId, columnPrefix, isRequired) {
6870
*/
6971
function injectResultInCollection(result, mappedCollection, maps, mapId, columnPrefix) {
7072

71-
// Make sure there is a columnPrefix
72-
if (!columnPrefix) {
73+
// Set up a default value for columnPrefix
74+
if (columnPrefix === undefined) {
7375
columnPrefix = '';
7476
}
7577

@@ -99,6 +101,11 @@ function injectResultInCollection(result, mappedCollection, maps, mapId, columnP
99101
*/
100102
function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix) {
101103

104+
// Set up a default value for columnPrefix
105+
if (columnPrefix === undefined) {
106+
columnPrefix = '';
107+
}
108+
102109
// Get the resultMap for this object
103110
let resultMap = _.find(maps, 'mapId', mapId);
104111

test/.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
// ----- Globals -----
243243
// false: variable as read-only
244244
"globals": {
245+
"assert": false,
245246
"expect": false
246247
}
247-
}
248+
}

test/setup/setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = function() {
2+
global.assert = global.chai.assert;
23
global.expect = global.chai.expect;
34

45
beforeEach(function() {

test/unit/mapper-test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ describe('Mapper', () => {
8484
expect(mappedResult.getFullName()).to.equal('Elvis Presley');
8585
});
8686

87+
it('should throw a NotFoundError when mapOne() does not find an object', () => {
88+
89+
let fn = function() {
90+
joinjs.mapOne([], domainMaps, 'userMap');
91+
};
92+
93+
expect(fn).to.throw(joinjs.NotFoundError);
94+
});
95+
96+
it('should not throw an Error when mapOne() does not find an object, but isRequired is set to false', () => {
97+
98+
var mappedResult = joinjs.mapOne([], domainMaps, 'userMap', '', false);
99+
100+
assert.isNull(mappedResult);
101+
});
102+
87103
it('should work when resultSet contains multiple top-level objects', () => {
88104
let resultSet = [
89105
{
@@ -97,7 +113,7 @@ describe('Mapper', () => {
97113
security_name: 'Ace Ltd',
98114
country_code: 'SZ',
99115
country_name: 'Switzerland'
100-
},
116+
}
101117
];
102118

103119
let expectedResult = [

0 commit comments

Comments
 (0)