fix(blocks-antd): Fix selector index comparison.

This commit is contained in:
SamTolmay 2021-05-28 10:28:03 +02:00
parent f10b7c919a
commit 3f06fcde4b
3 changed files with 13 additions and 3 deletions

View File

@ -12,7 +12,7 @@ module.exports = {
],
coverageReporters: [['lcov', { projectRoot: '../../..' }], 'text', 'clover'],
errorOnDeprecated: true,
testPathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/src/test'],
testPathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/es/', '<rootDir>/src/test'],
moduleNameMapper: {
'\\.(css|less)$': '<rootDir>/tests/__mocks__/styleMock.js',
'^antd/.*/style$': '<rootDir>/tests/__mocks__/styleMock.js',

View File

@ -14,7 +14,7 @@
limitations under the License.
*/
import { type } from '@lowdefy/helpers';
import { serializer, type } from '@lowdefy/helpers';
// eslint-disable-next-line consistent-return
const getIndex = (value, options, key = 'value') => {
@ -23,7 +23,11 @@ const getIndex = (value, options, key = 'value') => {
if (type.isPrimitive(options[i]) && options[i] === value) {
return i;
}
if (type.isObject(options[i]) && JSON.stringify(options[i][key]) === JSON.stringify(value)) {
if (
type.isObject(options[i]) &&
serializer.serializeToString(options[i][key], { stable: true }) ===
serializer.serializeToString(value, { stable: true })
) {
return i;
}
}

View File

@ -154,3 +154,9 @@ test('object.value mixed multiple', () => {
expect(getValueIndex(['x', [1, 2], 'x'], options, true)).toEqual([6, 1, 6]);
expect(getValueIndex([{ a: 1, b: { c: 'x' } }], options, true)).toEqual([7]);
});
test('object.value stable comparison,', () => {
const options = [{ value: { a: 1, b: 1 } }, { value: { a: 2, b: 2 } }];
expect(getValueIndex({ b: 1, a: 1 }, options)).toEqual(0);
expect(getValueIndex([{ b: 1, a: 1 }], options, true)).toEqual([0]);
});