This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEXT-32745 - Fix circular references
- Loading branch information
Showing
9 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Entity from '../../data/_internals/Entity'; | ||
import SerializerFactory from './index'; | ||
import { handle, send } from '../../channel'; | ||
import EntityCollection from '../../data/_internals/EntityCollection'; | ||
import Criteria from '../../data/Criteria'; | ||
|
||
const { serialize, deserialize } = SerializerFactory({ | ||
handle: handle, | ||
send: send, | ||
}) | ||
|
||
describe('index.ts', () => { | ||
it('should convert entities with circular reference', () => { | ||
// @ts-ignore | ||
const entity = new Entity('foo', 'jest', { | ||
// @ts-ignore | ||
lineItems: new EntityCollection('line_items', 'line_item', undefined, new Criteria(), [ | ||
// @ts-ignore | ||
new Entity('line_item_1', 'line_item', { | ||
id: 'line_item_1', | ||
quantity: 1, | ||
versionId: 1, | ||
price: { | ||
unitPrice: 100, | ||
totalPrice: 100, | ||
}, | ||
// @ts-ignore | ||
children: new EntityCollection('line_item', 'order_line_item', undefined, new Criteria(), [ | ||
// @ts-ignore | ||
new Entity('extension', 'order_line_item_foreign_keys_extension', { | ||
id: 'extension', | ||
key: 'value', | ||
parent: undefined, | ||
}), | ||
// @ts-ignore | ||
new Entity('extension', 'order_line_item_foreign_keys_extension', { | ||
id: 'extension2', | ||
key: 'value', | ||
}) | ||
]), | ||
}), | ||
// @ts-ignore | ||
new Entity('line_item_2', 'line_item', { | ||
id: 'line_item_2', | ||
quantity: 1, | ||
versionId: 1, | ||
price: { | ||
unitPrice: 100, | ||
totalPrice: 100, | ||
}, | ||
}), | ||
]), | ||
}); | ||
|
||
// @ts-ignore - Create circular reference | ||
entity.lineItems.first(0).children.getAt(0).parent = entity.lineItems.getAt(0); | ||
|
||
const messageData = { | ||
entity, | ||
}; | ||
|
||
const result = serialize(messageData); | ||
|
||
// this is a regression test for a bug that caused the serializer to fail | ||
// expect the circular reference to be converted | ||
expect(result.entity.__draft__.lineItems.__entities__[0].__draft__.children.__entities__[0] instanceof Entity).toBe(false); | ||
expect(result.entity.__draft__.lineItems.__entities__[0].__draft__.children.__entities__[1] instanceof Entity).toBe(false); | ||
expect(result.entity.__draft__.lineItems.__entities__[0].__draft__.children.__entities__[0].__draft__.parent.children instanceof EntityCollection).toBe(false); | ||
expect(result.entity.__draft__.lineItems.__entities__[0].__draft__.children.__entities__[0].__draft__.parent).toStrictEqual({ | ||
__$CR__: 'root.entity.lineItems.0' | ||
}); | ||
|
||
const deserialized = deserialize(result, new MessageEvent('message')); | ||
|
||
// expect the circular reference to be converted back | ||
expect(deserialized.entity.lineItems.getAt(0).children.getAt(0).parent).toBe(deserialized.entity.lineItems.getAt(0)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters