From 5f61e043d387d159b73995d8d4d7e72a77d438e7 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 12 Mar 2025 22:15:46 +0530 Subject: [PATCH] fix: diff order --- .../store/modules/conversations/index.js | 4 +- .../specs/conversations/helpers.spec.js | 127 ++++++++++++++++++ 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/app/javascript/dashboard/store/modules/conversations/index.js b/app/javascript/dashboard/store/modules/conversations/index.js index 0da0de99b..034cf48fc 100644 --- a/app/javascript/dashboard/store/modules/conversations/index.js +++ b/app/javascript/dashboard/store/modules/conversations/index.js @@ -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); diff --git a/app/javascript/dashboard/store/modules/specs/conversations/helpers.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/helpers.spec.js index b0a3cdab3..1aab3f12a 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/helpers.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/helpers.spec.js @@ -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({}); + }); +});