-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding tests for lib/utils/flattenProperties.js #146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with | ||
* the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
var flattenProperties = require('../../lib/utils/flattenProperties'); | ||
|
||
describe('utils', () => { | ||
describe('flattenProperties', () => { | ||
|
||
it('should return an empty array', () => { | ||
var ret = flattenProperties({}); | ||
expect(ret).toEqual([]); | ||
}); | ||
|
||
it('should return the same array', () => { | ||
var to_ret = []; | ||
var ret = flattenProperties({}, to_ret); | ||
expect(ret).toBe(ret); | ||
}); | ||
|
||
it('should return leaf node values as an array', () => { | ||
var properties = { | ||
'black': { | ||
'value': '#000000' | ||
}, | ||
'white': { | ||
'value': '#FFFFFF' | ||
} | ||
}; | ||
var expected_ret = [ | ||
properties.black, | ||
properties.white | ||
]; | ||
var sortFn = function (a, b) { | ||
if (a.value > b.value) return 1; | ||
if (b.value > a.value) return -1; | ||
return 0; | ||
} | ||
var sortedExpectedRet = expected_ret.sort(sortFn); | ||
var ret = flattenProperties(properties); | ||
var sortedRet = ret.sort(sortFn); | ||
expect(sortedRet).toEqual(sortedExpectedRet); | ||
}); | ||
|
||
it('should return nested leaf node values as an array', () => { | ||
var properties = { | ||
'color': { | ||
'black': { | ||
'value': '#000000' | ||
}, | ||
'white': { | ||
'value': '#FFFFFF' | ||
} | ||
} | ||
}; | ||
var expected_ret = [ | ||
properties.color.black, | ||
properties.color.white | ||
]; | ||
var sortFn = function (a, b) { | ||
if (a.value > b.value) return 1; | ||
if (b.value > a.value) return -1; | ||
return 0; | ||
} | ||
var sortedExpectedRet = expected_ret.sort(sortFn); | ||
var ret = flattenProperties(properties); | ||
var sortedRet = ret.sort(sortFn); | ||
expect(sortedRet).toEqual(sortedExpectedRet); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this last test, instead of testing the values of the value key, I think we should test the objects against each other. This will enable us to add in details to the objects later and make sure they land correctly. Maybe something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it makes sense! Shall I add this and update my pull request? |
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple of small things: I see the sortFn declared twice; could it be declared above (just after
describe('utils'
or grouping the last two tests in anotherdescribe
) and shared between tests? or alternatively, since I see in some tests we are using lodash, can we use some existing method of lodash?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also just use lodash's sortBy function instead of declaring a new function.