Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions __mocks__/unitOfWorkMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cartMetadata.setAttributeList([
new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'),
new Attribute('createdAt', 'createdAt', 'datetime'),
new Attribute('data', 'data', 'object'),
new Attribute('codeList', 'codeList', 'array'),
]);
cartMetadata.setRelationList([
new Relation(
Expand Down
172 changes: 129 additions & 43 deletions __tests__/UnitOfWork.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,49 +110,6 @@ describe('UnitOfWork', () => {
cartMetadata
)
).toEqual({ '@id': '/v12/carts/2', status: 'payed' });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
loo: 'baz',
},
},
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
},
},
cartMetadata
)
).toEqual({ data: { foo: 'bar', loo: 'baz' } });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
bad: 'baz',
},
},
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
bad: 'baz',
},
},
cartMetadata
)
).toEqual({});
});

test('get dirty data with more data', () => {
Expand Down Expand Up @@ -541,4 +498,133 @@ describe('UnitOfWork', () => {
},
});
});

test('get dirty data with object data', () => {
expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
loo: 'baz',
},
},
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
},
},
cartMetadata
)
).toEqual({ data: { foo: 'bar', loo: 'baz' } });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
bad: 'baz',
},
},
{
'@id': '/v12/carts/1',
status: 'payed',
data: {
foo: 'bar',
bad: 'baz',
},
},
cartMetadata
)
).toEqual({});
});

test('get dirty data with array data', () => {
expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: ['bar', 'baz'],
},
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: ['bar'],
},
cartMetadata
)
).toEqual({ codeList: ['bar', 'baz'] });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: ['bar', 'baz'],
},
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: ['bar', 'baz'],
},
cartMetadata
)
).toEqual({});
});

test('get dirty data with complex array data', () => {
expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }, { baz: 'baz' }],
},
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }],
},
cartMetadata
)
).toEqual({ codeList: [{ bar: 'bar' }, { baz: 'baz' }] });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }, { baz: 'baz' }],
},
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }, { baz: 'bad' }],
},
cartMetadata
)
).toEqual({ codeList: [{ bar: 'bar' }, { baz: 'baz' }] });

expect(
unitOfWork.getDirtyData(
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }, { baz: 'baz' }],
},
{
'@id': '/v12/carts/1',
status: 'payed',
codeList: [{ bar: 'bar' }, { baz: 'baz' }],
},
cartMetadata
)
).toEqual({});
});
});
2 changes: 1 addition & 1 deletion src/UnitOfWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class UnitOfWork {
): StringKeyObject {
const dirtyFields = dirtyFieldsParam;

if (attribute.type === 'object') {
if (attribute.type === 'object' || attribute.type === 'array') {
if (oldValue === undefined || objectDiffers(oldValue, newValue)) {
dirtyFields[key] = newValue;
}
Expand Down