fix: diff order

This commit is contained in:
Shivam Mishra
2025-03-12 22:15:46 +05:30
parent 902d08905d
commit 5f61e043d3
2 changed files with 129 additions and 2 deletions
@@ -223,7 +223,7 @@ export const mutations = {
scope.setContext('incoming_meta', conversation.meta);
scope.setContext('stored_meta', selectedConversation.meta);
const diff = deepObjectDiff(selectedConversation, conversation);
const diff = deepObjectDiff(conversation, selectedConversation);
scope.setContext('added', diff.added);
scope.setContext('removed', diff.removed);
scope.setContext('modified', diff.modified);
@@ -241,7 +241,7 @@ export const mutations = {
scope.setContext('incoming_meta', conversation.meta);
scope.setContext('stored_meta', selectedConversation.meta);
const diff = deepObjectDiff(selectedConversation, conversation);
const diff = deepObjectDiff(conversation, selectedConversation);
scope.setContext('added', diff.added);
scope.setContext('removed', diff.removed);
scope.setContext('modified', diff.modified);
@@ -5,6 +5,7 @@ import {
filterByTeam,
filterByLabel,
filterByUnattended,
deepObjectDiff,
} from '../../conversations/helpers';
const conversationList = [
@@ -172,3 +173,129 @@ describe('#filterByUnattended', () => {
expect(filterByUnattended(true, 'mentions', 123)).toEqual(true);
});
});
describe('#deepObjectDiff', () => {
it('should detect added properties', () => {
const original = { name: 'John', age: 30 };
const updated = { name: 'John', age: 30, email: 'john@example.com' };
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({ email: 'john@example.com' });
expect(result.removed).toEqual({});
expect(result.modified).toEqual({});
});
it('should detect removed properties', () => {
const original = { name: 'John', age: 30, email: 'john@example.com' };
const updated = { name: 'John', age: 30 };
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({});
expect(result.removed).toEqual({ email: 'john@example.com' });
expect(result.modified).toEqual({});
});
it('should detect modified properties', () => {
const original = { name: 'John', age: 30 };
const updated = { name: 'John', age: 31 };
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({});
expect(result.removed).toEqual({});
expect(result.modified).toEqual({ age: { from: 30, to: 31 } });
});
it('should handle nested objects', () => {
const original = {
name: 'John',
address: { city: 'New York', country: 'USA' },
};
const updated = {
name: 'John',
address: { city: 'Boston', country: 'USA' },
};
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({});
expect(result.removed).toEqual({});
expect(result.modified).toEqual({
'address.city': { from: 'New York', to: 'Boston' },
});
});
it('should respect maxDepth parameter', () => {
const original = {
user: {
details: {
address: { city: 'New York', country: 'USA' },
},
},
};
const updated = {
user: {
details: {
address: { city: 'Boston', country: 'USA' },
},
},
};
// With maxDepth of 2
const result2 = deepObjectDiff(original, updated, 2);
expect(result2.modified).toEqual({
'user.details': {
from: {
address: { city: 'New York', country: 'USA' },
},
to: {
address: { city: 'Boston', country: 'USA' },
},
},
});
// With maxDepth of 4 (enough to reach the deepest level)
const result4 = deepObjectDiff(original, updated, 4);
expect(result4.modified).toEqual({
'user.details.address.city': { from: 'New York', to: 'Boston' },
});
});
it('should handle arrays correctly', () => {
const original = { tags: ['important', 'urgent'] };
const updated = { tags: ['important', 'normal'] };
const result = deepObjectDiff(original, updated);
expect(result.modified).toEqual({
tags: {
from: ['important', 'urgent'],
to: ['important', 'normal'],
},
});
});
it('should handle empty objects', () => {
const original = {};
const updated = {};
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({});
expect(result.removed).toEqual({});
expect(result.modified).toEqual({});
});
it('should handle completely different objects', () => {
const original = { a: 1, b: 2 };
const updated = { c: 3, d: 4 };
const result = deepObjectDiff(original, updated);
expect(result.added).toEqual({ c: 3, d: 4 });
expect(result.removed).toEqual({ a: 1, b: 2 });
expect(result.modified).toEqual({});
});
});