Skip to content
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

Merged
merged 2 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions __tests__/utils/flattenProperties.test.js
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) {
Copy link
Contributor

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 another describe) and shared between tests? or alternatively, since I see in some tests we are using lodash, can we use some existing method of lodash?

Copy link
Member

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.

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);
});
Copy link
Collaborator

@chazzmoney chazzmoney Oct 4, 2018

Choose a reason for hiding this comment

The 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:

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;
            else if (b.value > a.value)
                  return -1;
            else
                  return 0;
      };
      var sortedExpectedValues = expected_ret.sort(sortFn);
      var ret = flattenProperties(properties);
      var sortedReturnedValues = ret.sort(sortFn);
      expect(JSON.stringify(sortedExpectedValues)).toEqual(JSON.stringify(sortedReturnedValues));
    });
  });

Copy link
Author

Choose a reason for hiding this comment

The 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?

});
});